@etsoo/materialui 1.4.52 → 1.4.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,10 @@
1
- import { ApiRefreshTokenDto, AppLoginParams, AppTryLoginParams, ExternalEndpoint, IApi } from "@etsoo/appscript";
1
+ import { ApiRefreshTokenDto, AppLoginParams, AppTryLoginParams, ExternalEndpoint, IApi, IApiPayload } from "@etsoo/appscript";
2
2
  import { IServiceApp } from "./IServiceApp";
3
3
  import { IServiceAppSettings } from "./IServiceAppSettings";
4
4
  import { IServicePageData } from "./IServicePage";
5
5
  import { IServiceUser, ServiceUserToken } from "./IServiceUser";
6
6
  import { ReactApp } from "./ReactApp";
7
+ import { IActionResult } from "@etsoo/shared";
7
8
  /**
8
9
  * Core Service App
9
10
  * Service login to core system, get the refesh token and access token
@@ -23,6 +24,7 @@ export declare class ServiceApp<U extends IServiceUser = IServiceUser, P extends
23
24
  * Core system origin
24
25
  */
25
26
  readonly coreOrigin: string;
27
+ private coreAccessToken;
26
28
  /**
27
29
  * Constructor
28
30
  * @param settings Settings
@@ -61,10 +63,17 @@ export declare class ServiceApp<U extends IServiceUser = IServiceUser, P extends
61
63
  */
62
64
  userLoginEx(user: U & ServiceUserToken, core?: ApiRefreshTokenDto, dispatch?: boolean): void;
63
65
  /**
64
- * Save core system refresh token
65
- * @param token New refresh token
66
+ * Save core system data
67
+ * @param data Data
66
68
  */
67
- protected saveCoreToken(token: string): void;
69
+ protected saveCoreToken(data: ApiRefreshTokenDto): void;
70
+ /**
71
+ * Switch organization
72
+ * @param organizationId Organization ID
73
+ * @param fromOrganizationId From organization ID
74
+ * @param payload Payload
75
+ */
76
+ switchOrg(organizationId: number, fromOrganizationId?: number, payload?: IApiPayload<IActionResult<U & ServiceUserToken>>): Promise<void>;
68
77
  /**
69
78
  * Try login
70
79
  * @param params Login parameters
@@ -22,6 +22,7 @@ export class ServiceApp extends ReactApp {
22
22
  * Core system origin
23
23
  */
24
24
  coreOrigin;
25
+ coreAccessToken;
25
26
  /**
26
27
  * Constructor
27
28
  * @param settings Settings
@@ -141,16 +142,43 @@ export class ServiceApp extends ReactApp {
141
142
  tokenType: user.tokenScheme ?? "Bearer",
142
143
  expiresIn: user.seconds
143
144
  };
145
+ // Cache the core system data
146
+ this.saveCoreToken(core);
147
+ }
148
+ /**
149
+ * Save core system data
150
+ * @param data Data
151
+ */
152
+ saveCoreToken(data) {
153
+ // Hold the core system access token
154
+ this.coreAccessToken = data.accessToken;
144
155
  // Cache the core system refresh token
145
- this.saveCoreToken(core.refreshToken);
146
- this.exchangeTokenAll(core, coreName);
156
+ this.storage.setData(coreTokenKey, this.encrypt(data.refreshToken));
157
+ // Exchange tokens
158
+ this.exchangeTokenAll(data, coreName);
147
159
  }
148
160
  /**
149
- * Save core system refresh token
150
- * @param token New refresh token
161
+ * Switch organization
162
+ * @param organizationId Organization ID
163
+ * @param fromOrganizationId From organization ID
164
+ * @param payload Payload
151
165
  */
152
- saveCoreToken(token) {
153
- this.storage.setData(coreTokenKey, this.encrypt(token));
166
+ async switchOrg(organizationId, fromOrganizationId, payload) {
167
+ if (!this.coreAccessToken) {
168
+ throw new Error("Core access token is required to switch organization.");
169
+ }
170
+ const [result, refreshToken] = await new AuthApi(this, this.coreApi).switchOrg({ organizationId, fromOrganizationId, token: this.coreAccessToken }, payload);
171
+ if (result == null || result.data == null)
172
+ return;
173
+ let core;
174
+ if ("core" in result.data && typeof result.data.core === "string") {
175
+ core = JSON.parse(result.data.core);
176
+ delete result.data.core;
177
+ }
178
+ // Override the user data's refresh token
179
+ const user = refreshToken ? { ...result.data, refreshToken } : result.data;
180
+ // User login
181
+ this.userLoginEx(user, core, true);
154
182
  }
155
183
  /**
156
184
  * Try login
@@ -185,8 +213,7 @@ export class ServiceApp extends ReactApp {
185
213
  if (data == null)
186
214
  return;
187
215
  // Cache the core system refresh token
188
- this.saveCoreToken(data.refreshToken);
189
- this.exchangeTokenAll(data, coreName);
216
+ this.saveCoreToken(data);
190
217
  onSuccess?.();
191
218
  });
192
219
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/materialui",
3
- "version": "1.4.52",
3
+ "version": "1.4.53",
4
4
  "description": "TypeScript Material-UI Implementation",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -35,13 +35,13 @@
35
35
  "@emotion/css": "^11.13.5",
36
36
  "@emotion/react": "^11.14.0",
37
37
  "@emotion/styled": "^11.14.0",
38
- "@etsoo/appscript": "^1.5.81",
38
+ "@etsoo/appscript": "^1.5.82",
39
39
  "@etsoo/notificationbase": "^1.1.54",
40
- "@etsoo/react": "^1.8.14",
40
+ "@etsoo/react": "^1.8.15",
41
41
  "@etsoo/shared": "^1.2.55",
42
- "@mui/icons-material": "^6.2.0",
43
- "@mui/material": "^6.2.0",
44
- "@mui/x-data-grid": "^7.23.2",
42
+ "@mui/icons-material": "^6.3.0",
43
+ "@mui/material": "^6.3.0",
44
+ "@mui/x-data-grid": "^7.23.5",
45
45
  "chart.js": "^4.4.7",
46
46
  "chartjs-plugin-datalabels": "^2.2.0",
47
47
  "eventemitter3": "^5.0.1",
@@ -49,7 +49,7 @@
49
49
  "pulltorefreshjs": "^0.1.22",
50
50
  "react": "^18.3.1",
51
51
  "react-avatar-editor": "^13.0.2",
52
- "react-chartjs-2": "^5.2.0",
52
+ "react-chartjs-2": "^5.3.0",
53
53
  "react-dom": "^18.3.1",
54
54
  "react-draggable": "^4.4.6",
55
55
  "react-imask": "7.6.1"
@@ -63,11 +63,11 @@
63
63
  "@babel/preset-typescript": "^7.26.0",
64
64
  "@babel/runtime-corejs3": "^7.26.0",
65
65
  "@testing-library/react": "^16.1.0",
66
- "@types/pica": "^9.0.4",
66
+ "@types/pica": "^9.0.5",
67
67
  "@types/pulltorefreshjs": "^0.1.7",
68
- "@types/react": "^18.3.13",
68
+ "@types/react": "^18.3.18",
69
69
  "@types/react-avatar-editor": "^13.0.3",
70
- "@types/react-dom": "^18.3.1",
70
+ "@types/react-dom": "^18.3.5",
71
71
  "@types/react-input-mask": "^3.0.6",
72
72
  "@types/react-window": "^1.8.8",
73
73
  "@vitejs/plugin-react": "^4.3.4",
@@ -5,13 +5,15 @@ import {
5
5
  AuthApi,
6
6
  BridgeUtils,
7
7
  ExternalEndpoint,
8
- IApi
8
+ IApi,
9
+ IApiPayload
9
10
  } from "@etsoo/appscript";
10
11
  import { IServiceApp } from "./IServiceApp";
11
12
  import { IServiceAppSettings } from "./IServiceAppSettings";
12
13
  import { IServicePageData } from "./IServicePage";
13
14
  import { IServiceUser, ServiceUserToken } from "./IServiceUser";
14
15
  import { ReactApp } from "./ReactApp";
16
+ import { IActionResult } from "@etsoo/shared";
15
17
 
16
18
  const coreName = "core";
17
19
  const coreTokenKey = "core-refresh-token";
@@ -46,6 +48,8 @@ export class ServiceApp<
46
48
  */
47
49
  readonly coreOrigin: string;
48
50
 
51
+ private coreAccessToken: string | undefined;
52
+
49
53
  /**
50
54
  * Constructor
51
55
  * @param settings Settings
@@ -182,18 +186,61 @@ export class ServiceApp<
182
186
  expiresIn: user.seconds
183
187
  };
184
188
 
189
+ // Cache the core system data
190
+ this.saveCoreToken(core);
191
+ }
192
+
193
+ /**
194
+ * Save core system data
195
+ * @param data Data
196
+ */
197
+ protected saveCoreToken(data: ApiRefreshTokenDto) {
198
+ // Hold the core system access token
199
+ this.coreAccessToken = data.accessToken;
200
+
185
201
  // Cache the core system refresh token
186
- this.saveCoreToken(core.refreshToken);
202
+ this.storage.setData(coreTokenKey, this.encrypt(data.refreshToken));
187
203
 
188
- this.exchangeTokenAll(core, coreName);
204
+ // Exchange tokens
205
+ this.exchangeTokenAll(data, coreName);
189
206
  }
190
207
 
191
208
  /**
192
- * Save core system refresh token
193
- * @param token New refresh token
209
+ * Switch organization
210
+ * @param organizationId Organization ID
211
+ * @param fromOrganizationId From organization ID
212
+ * @param payload Payload
194
213
  */
195
- protected saveCoreToken(token: string) {
196
- this.storage.setData(coreTokenKey, this.encrypt(token));
214
+ async switchOrg(
215
+ organizationId: number,
216
+ fromOrganizationId?: number,
217
+ payload?: IApiPayload<IActionResult<U & ServiceUserToken>>
218
+ ) {
219
+ if (!this.coreAccessToken) {
220
+ throw new Error("Core access token is required to switch organization.");
221
+ }
222
+
223
+ const [result, refreshToken] = await new AuthApi(
224
+ this,
225
+ this.coreApi
226
+ ).switchOrg(
227
+ { organizationId, fromOrganizationId, token: this.coreAccessToken },
228
+ payload
229
+ );
230
+
231
+ if (result == null || result.data == null) return;
232
+
233
+ let core: ApiRefreshTokenDto | undefined;
234
+ if ("core" in result.data && typeof result.data.core === "string") {
235
+ core = JSON.parse(result.data.core);
236
+ delete result.data.core;
237
+ }
238
+
239
+ // Override the user data's refresh token
240
+ const user = refreshToken ? { ...result.data, refreshToken } : result.data;
241
+
242
+ // User login
243
+ this.userLoginEx(user, core, true);
197
244
  }
198
245
 
199
246
  /**
@@ -233,9 +280,7 @@ export class ServiceApp<
233
280
  if (data == null) return;
234
281
 
235
282
  // Cache the core system refresh token
236
- this.saveCoreToken(data.refreshToken);
237
-
238
- this.exchangeTokenAll(data, coreName);
283
+ this.saveCoreToken(data);
239
284
 
240
285
  onSuccess?.();
241
286
  });