/
events.ts
144 lines (131 loc) · 2.71 KB
/
events.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { LocaleCode } from "../locales";
import { Values } from "./helpers";
export type Version = 1;
export const EventType = {
handshake: "handshake",
response: "response",
redirect: "redirect",
theme: "theme",
localeChanged: "localeChanged",
tokenRefresh: "tokenRefresh",
} as const;
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;
saleorVersion?: string;
dashboardVersion?: string;
}
>;
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;
}
>;
export type LocaleChangedEvent = Event<
"localeChanged",
{
locale: LocaleCode;
}
>;
export type TokenRefreshEvent = Event<
"tokenRefresh",
{
token: string;
}
>;
export type Events =
| HandshakeEvent
| DispatchResponseEvent
| RedirectEvent
| ThemeEvent
| LocaleChangedEvent
| TokenRefreshEvent;
export type PayloadOfEvent<
TEventType extends EventType,
TEvent extends Events = Events
// @ts-ignore TODO - why this is not working with this tsconfig? Fixme
> = TEvent extends Event<TEventType, unknown> ? TEvent["payload"] : never;
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,
},
};
},
createHandshakeEvent(
token: string,
// eslint-disable-next-line default-param-last
version: Version = 1,
saleorVersions?: {
dashboard: string;
core: string;
}
): HandshakeEvent {
return {
type: "handshake",
payload: {
token,
version,
saleorVersion: saleorVersions?.core,
dashboardVersion: saleorVersions?.dashboard,
},
};
},
createLocaleChangedEvent(newLocale: LocaleCode): LocaleChangedEvent {
return {
type: "localeChanged",
payload: {
locale: newLocale,
},
};
},
createTokenRefreshEvent(newToken: string): TokenRefreshEvent {
return {
type: "tokenRefresh",
payload: {
token: newToken,
},
};
},
};