@etsoo/react 1.3.35 → 1.3.39

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,8 +1,9 @@
1
- import { IApi } from '@etsoo/appscript';
2
- import { ISmartERPUser } from '..';
1
+ import { IActionResult, IApi } from '@etsoo/appscript';
2
+ import { ApiDataError } from '@etsoo/restclient';
3
3
  import { IServiceAppSettings } from './IServiceAppSettings';
4
4
  import { IServicePageData } from './IServicePage';
5
5
  import { IServiceUser } from './IServiceUser';
6
+ import { ISmartERPUser } from './ISmartERPUser';
6
7
  import { ReactApp } from './ReactApp';
7
8
  /**
8
9
  * Core Service App
@@ -25,6 +26,19 @@ export declare class ServiceApp<P extends IServicePageData = IServicePageData, U
25
26
  * @param name Application name
26
27
  */
27
28
  constructor(settings: S, name: string);
29
+ /**
30
+ * Go to the login page
31
+ */
32
+ toLoginPage(): void;
33
+ /**
34
+ * Try login result processing
35
+ * @param result Result
36
+ */
37
+ protected tryLoginResult(result: string | ApiDataError | IActionResult): void;
38
+ /**
39
+ * Try login
40
+ */
41
+ tryLogin(): Promise<boolean>;
28
42
  /**
29
43
  * User login extended
30
44
  * @param user New user
@@ -1,4 +1,6 @@
1
1
  import { createClient } from '@etsoo/appscript';
2
+ import { DomUtils } from '@etsoo/shared';
3
+ import { CoreConstants } from './CoreConstants';
2
4
  import { ReactApp } from './ReactApp';
3
5
  /**
4
6
  * Core Service App
@@ -24,6 +26,88 @@ export class ServiceApp extends ReactApp {
24
26
  this.setApi(api);
25
27
  this.coreApi = api;
26
28
  }
29
+ /**
30
+ * Go to the login page
31
+ */
32
+ toLoginPage() {
33
+ const coreUrl = this.settings.coreApi;
34
+ window.location.href =
35
+ coreUrl +
36
+ '?serviceId=' +
37
+ this.settings.serviceId +
38
+ '&' +
39
+ DomUtils.Culture +
40
+ '=' +
41
+ this.culture;
42
+ }
43
+ /**
44
+ * Try login result processing
45
+ * @param result Result
46
+ */
47
+ tryLoginResult(result) { }
48
+ /**
49
+ * Try login
50
+ */
51
+ async tryLogin() {
52
+ // Token
53
+ const token = this.getCacheToken();
54
+ if (token == null || token === '') {
55
+ this.tryLoginResult('');
56
+ return false;
57
+ }
58
+ // Reqest data
59
+ const data = {
60
+ timezone: this.getTimeZone()
61
+ };
62
+ // Payload
63
+ const payload = {
64
+ config: { headers: { [CoreConstants.TokenHeaderRefresh]: token } },
65
+ onError: (error) => {
66
+ this.tryLoginResult(error);
67
+ // Prevent further processing
68
+ return false;
69
+ }
70
+ };
71
+ // Call API
72
+ const result = await this.coreApi.put('Auth/RefreshToken', data, payload);
73
+ if (result == null)
74
+ return false;
75
+ if (!result.ok) {
76
+ this.tryLoginResult(result);
77
+ return false;
78
+ }
79
+ // Token
80
+ const refreshToken = this.getResponseToken(payload.response);
81
+ if (refreshToken == null || result.data == null) {
82
+ this.tryLoginResult(this.get('noData'));
83
+ return false;
84
+ }
85
+ // User data
86
+ const userData = result.data;
87
+ // Use core system access token to service api to exchange service access token
88
+ const serviceResult = await this.api.put('Auth/ExchangeToken', {
89
+ token: userData.token
90
+ }, {
91
+ onError: (error) => {
92
+ // Set message
93
+ this.tryLoginResult(error);
94
+ // Prevent further processing
95
+ return false;
96
+ }
97
+ });
98
+ if (serviceResult == null)
99
+ return false;
100
+ if (!serviceResult.ok) {
101
+ this.tryLoginResult(result);
102
+ return false;
103
+ }
104
+ if (serviceResult.data == null) {
105
+ this.tryLoginResult(this.get('noData'));
106
+ return false;
107
+ }
108
+ this.userLoginEx(serviceResult.data, refreshToken, userData);
109
+ return true;
110
+ }
27
111
  /**
28
112
  * User login extended
29
113
  * @param user New user
@@ -36,6 +120,6 @@ export class ServiceApp extends ReactApp {
36
120
  // Core system user
37
121
  this.coreUser = coreUser;
38
122
  // Core system API token
39
- this.api.authorize(this.settings.authScheme, coreUser.token);
123
+ this.coreApi.authorize(this.settings.authScheme, coreUser.token);
40
124
  }
41
125
  }
@@ -350,7 +350,7 @@ export class NotifierMU extends NotifierReact {
350
350
  */
351
351
  addRaw(data, modal) {
352
352
  // Destruct
353
- const { type, content, title, align, timespan, ...rest } = data;
353
+ const { type, content, title, align, timespan = modal ? 0 : undefined, ...rest } = data;
354
354
  // Setup
355
355
  const n = new NotificationMU(type, content, title, align, timespan);
356
356
  // Assign other properties
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.3.35",
3
+ "version": "1.3.39",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -50,7 +50,7 @@
50
50
  "@emotion/react": "^11.7.0",
51
51
  "@emotion/style": "^0.8.0",
52
52
  "@emotion/styled": "^11.6.0",
53
- "@etsoo/appscript": "^1.1.50",
53
+ "@etsoo/appscript": "^1.1.53",
54
54
  "@etsoo/notificationbase": "^1.0.94",
55
55
  "@etsoo/shared": "^1.0.75",
56
56
  "@mui/icons-material": "^5.2.0",
@@ -1,9 +1,18 @@
1
- import { createClient, IApi } from '@etsoo/appscript';
2
- import { ISmartERPUser } from '..';
1
+ import {
2
+ createClient,
3
+ IActionResult,
4
+ IApi,
5
+ IApiPayload
6
+ } from '@etsoo/appscript';
7
+ import { ApiDataError } from '@etsoo/restclient';
8
+ import { DomUtils } from '@etsoo/shared';
9
+ import { CoreConstants } from './CoreConstants';
3
10
  import { IServiceAppSettings } from './IServiceAppSettings';
4
11
  import { IServicePageData } from './IServicePage';
5
- import { IServiceUser } from './IServiceUser';
12
+ import { IServiceUser, ServiceLoginResult } from './IServiceUser';
13
+ import { ISmartERPUser, SmartERPLoginResult } from './ISmartERPUser';
6
14
  import { ReactApp } from './ReactApp';
15
+ import { RefreshTokenRQ } from './RefreshTokenRQ';
7
16
 
8
17
  /**
9
18
  * Core Service App
@@ -47,6 +56,111 @@ export class ServiceApp<
47
56
  this.coreApi = api;
48
57
  }
49
58
 
59
+ /**
60
+ * Go to the login page
61
+ */
62
+ override toLoginPage() {
63
+ const coreUrl = this.settings.coreApi;
64
+ window.location.href =
65
+ coreUrl +
66
+ '?serviceId=' +
67
+ this.settings.serviceId +
68
+ '&' +
69
+ DomUtils.Culture +
70
+ '=' +
71
+ this.culture;
72
+ }
73
+
74
+ /**
75
+ * Try login result processing
76
+ * @param result Result
77
+ */
78
+ protected tryLoginResult(result: string | ApiDataError | IActionResult) {}
79
+
80
+ /**
81
+ * Try login
82
+ */
83
+ override async tryLogin() {
84
+ // Token
85
+ const token = this.getCacheToken();
86
+ if (token == null || token === '') {
87
+ this.tryLoginResult('');
88
+ return false;
89
+ }
90
+
91
+ // Reqest data
92
+ const data: RefreshTokenRQ = {
93
+ timezone: this.getTimeZone()
94
+ };
95
+
96
+ // Payload
97
+ const payload: IApiPayload<SmartERPLoginResult, any> = {
98
+ config: { headers: { [CoreConstants.TokenHeaderRefresh]: token } },
99
+ onError: (error) => {
100
+ this.tryLoginResult(error);
101
+
102
+ // Prevent further processing
103
+ return false;
104
+ }
105
+ };
106
+
107
+ // Call API
108
+ const result = await this.coreApi.put<SmartERPLoginResult>(
109
+ 'Auth/RefreshToken',
110
+ data,
111
+ payload
112
+ );
113
+ if (result == null) return false;
114
+
115
+ if (!result.ok) {
116
+ this.tryLoginResult(result);
117
+ return false;
118
+ }
119
+
120
+ // Token
121
+ const refreshToken = this.getResponseToken(payload.response);
122
+ if (refreshToken == null || result.data == null) {
123
+ this.tryLoginResult(this.get('noData')!);
124
+ return false;
125
+ }
126
+
127
+ // User data
128
+ const userData = result.data;
129
+
130
+ // Use core system access token to service api to exchange service access token
131
+ const serviceResult = await this.api.put<ServiceLoginResult>(
132
+ 'Auth/ExchangeToken',
133
+ {
134
+ token: userData.token
135
+ },
136
+ {
137
+ onError: (error) => {
138
+ // Set message
139
+ this.tryLoginResult(error);
140
+
141
+ // Prevent further processing
142
+ return false;
143
+ }
144
+ }
145
+ );
146
+
147
+ if (serviceResult == null) return false;
148
+
149
+ if (!serviceResult.ok) {
150
+ this.tryLoginResult(result);
151
+ return false;
152
+ }
153
+
154
+ if (serviceResult.data == null) {
155
+ this.tryLoginResult(this.get('noData')!);
156
+ return false;
157
+ }
158
+
159
+ this.userLoginEx(serviceResult.data, refreshToken, userData);
160
+
161
+ return true;
162
+ }
163
+
50
164
  /**
51
165
  * User login extended
52
166
  * @param user New user
@@ -65,6 +179,6 @@ export class ServiceApp<
65
179
  this.coreUser = coreUser;
66
180
 
67
181
  // Core system API token
68
- this.api.authorize(this.settings.authScheme, coreUser.token);
182
+ this.coreApi.authorize(this.settings.authScheme, coreUser.token);
69
183
  }
70
184
  }
@@ -581,7 +581,14 @@ export class NotifierMU extends NotifierReact {
581
581
  modal?: boolean
582
582
  ): INotificationReact {
583
583
  // Destruct
584
- const { type, content, title, align, timespan, ...rest } = data;
584
+ const {
585
+ type,
586
+ content,
587
+ title,
588
+ align,
589
+ timespan = modal ? 0 : undefined,
590
+ ...rest
591
+ } = data;
585
592
 
586
593
  // Setup
587
594
  const n = new NotificationMU(type, content, title, align, timespan);