2022-09-13 13:15:52 +00:00
|
|
|
import { LocaleCode } from "../locales";
|
2022-08-11 12:03:51 +00:00
|
|
|
import { Values } from "./helpers";
|
|
|
|
|
|
|
|
export type Version = 1;
|
|
|
|
|
|
|
|
export const EventType = {
|
|
|
|
handshake: "handshake",
|
|
|
|
response: "response",
|
|
|
|
redirect: "redirect",
|
|
|
|
theme: "theme",
|
2022-09-13 09:59:30 +00:00
|
|
|
localeChanged: "localeChanged",
|
2023-05-22 17:26:03 +00:00
|
|
|
tokenRefresh: "tokenRefresh",
|
2022-08-11 12:03:51 +00:00
|
|
|
} as const;
|
2022-08-11 21:14:41 +00:00
|
|
|
|
2022-08-11 12:03:51 +00:00
|
|
|
export type EventType = Values<typeof EventType>;
|
|
|
|
|
|
|
|
type Event<Name extends EventType, Payload extends {}> = {
|
|
|
|
payload: Payload;
|
|
|
|
type: Name;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type HandshakeEvent = Event<
|
|
|
|
"handshake",
|
|
|
|
{
|
|
|
|
token: string;
|
|
|
|
version: Version;
|
2023-06-22 13:46:16 +00:00
|
|
|
saleorVersion?: string;
|
|
|
|
dashboardVersion?: string;
|
2022-08-11 12:03:51 +00:00
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type DispatchResponseEvent = Event<
|
|
|
|
"response",
|
|
|
|
{
|
|
|
|
actionId: string;
|
|
|
|
ok: boolean;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type RedirectEvent = Event<
|
|
|
|
"redirect",
|
|
|
|
{
|
|
|
|
path: string;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type ThemeType = "light" | "dark";
|
|
|
|
export type ThemeEvent = Event<
|
|
|
|
"theme",
|
|
|
|
{
|
|
|
|
theme: ThemeType;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
2022-09-13 09:59:30 +00:00
|
|
|
export type LocaleChangedEvent = Event<
|
|
|
|
"localeChanged",
|
|
|
|
{
|
2022-09-13 13:15:52 +00:00
|
|
|
locale: LocaleCode;
|
2022-09-13 09:59:30 +00:00
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
2023-05-22 17:26:03 +00:00
|
|
|
export type TokenRefreshEvent = Event<
|
|
|
|
"tokenRefresh",
|
|
|
|
{
|
|
|
|
token: string;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
2022-09-13 09:59:30 +00:00
|
|
|
export type Events =
|
|
|
|
| HandshakeEvent
|
|
|
|
| DispatchResponseEvent
|
|
|
|
| RedirectEvent
|
|
|
|
| ThemeEvent
|
2023-05-22 17:26:03 +00:00
|
|
|
| LocaleChangedEvent
|
|
|
|
| TokenRefreshEvent;
|
2022-08-11 12:03:51 +00:00
|
|
|
|
|
|
|
export type PayloadOfEvent<
|
|
|
|
TEventType extends EventType,
|
|
|
|
TEvent extends Events = Events
|
|
|
|
// @ts-ignore TODO - why this is not working with this tsconfig? Fixme
|
2022-09-02 14:53:40 +00:00
|
|
|
> = TEvent extends Event<TEventType, unknown> ? TEvent["payload"] : never;
|
2022-09-13 10:00:02 +00:00
|
|
|
|
|
|
|
export const DashboardEventFactory = {
|
|
|
|
createThemeChangeEvent(theme: ThemeType): ThemeEvent {
|
|
|
|
return {
|
|
|
|
payload: {
|
|
|
|
theme,
|
|
|
|
},
|
|
|
|
type: "theme",
|
|
|
|
};
|
|
|
|
},
|
|
|
|
createRedirectEvent(path: string): RedirectEvent {
|
|
|
|
return {
|
|
|
|
type: "redirect",
|
|
|
|
payload: {
|
|
|
|
path,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
createDispatchResponseEvent(actionId: string, ok: boolean): DispatchResponseEvent {
|
|
|
|
return {
|
|
|
|
type: "response",
|
|
|
|
payload: {
|
|
|
|
actionId,
|
|
|
|
ok,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2023-06-22 13:46:16 +00:00
|
|
|
createHandshakeEvent(
|
|
|
|
token: string,
|
|
|
|
// eslint-disable-next-line default-param-last
|
|
|
|
version: Version = 1,
|
|
|
|
saleorVersions?: {
|
|
|
|
dashboard: string;
|
|
|
|
core: string;
|
|
|
|
}
|
|
|
|
): HandshakeEvent {
|
2022-09-13 10:00:02 +00:00
|
|
|
return {
|
|
|
|
type: "handshake",
|
|
|
|
payload: {
|
|
|
|
token,
|
|
|
|
version,
|
2023-06-22 13:46:16 +00:00
|
|
|
saleorVersion: saleorVersions?.core,
|
|
|
|
dashboardVersion: saleorVersions?.dashboard,
|
2022-09-13 10:00:02 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2022-09-14 12:21:53 +00:00
|
|
|
createLocaleChangedEvent(newLocale: LocaleCode): LocaleChangedEvent {
|
|
|
|
return {
|
|
|
|
type: "localeChanged",
|
|
|
|
payload: {
|
|
|
|
locale: newLocale,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2023-05-22 17:26:03 +00:00
|
|
|
createTokenRefreshEvent(newToken: string): TokenRefreshEvent {
|
|
|
|
return {
|
|
|
|
type: "tokenRefresh",
|
|
|
|
payload: {
|
|
|
|
token: newToken,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2022-09-13 10:00:02 +00:00
|
|
|
};
|