@etsoo/react 1.3.39 → 1.3.43

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.
@@ -6,6 +6,10 @@ export declare type RefreshTokenRQ = {
6
6
  * Service id
7
7
  */
8
8
  serviceId?: number;
9
+ /**
10
+ * Service Uid
11
+ */
12
+ serviceUid?: string;
9
13
  /**
10
14
  * Time zone
11
15
  */
@@ -1,10 +1,10 @@
1
- import { IActionResult, IApi } from '@etsoo/appscript';
2
- import { ApiDataError } from '@etsoo/restclient';
1
+ import { IApi, RefreshTokenProps } from '@etsoo/appscript';
3
2
  import { IServiceAppSettings } from './IServiceAppSettings';
4
3
  import { IServicePageData } from './IServicePage';
5
4
  import { IServiceUser } from './IServiceUser';
6
5
  import { ISmartERPUser } from './ISmartERPUser';
7
6
  import { ReactApp } from './ReactApp';
7
+ import { RefreshTokenRQ } from './RefreshTokenRQ';
8
8
  /**
9
9
  * Core Service App
10
10
  * Service login to core system, get the refesh token and access token
@@ -31,10 +31,10 @@ export declare class ServiceApp<P extends IServicePageData = IServicePageData, U
31
31
  */
32
32
  toLoginPage(): void;
33
33
  /**
34
- * Try login result processing
35
- * @param result Result
34
+ * Refresh token
35
+ * @param props Props
36
36
  */
37
- protected tryLoginResult(result: string | ApiDataError | IActionResult): void;
37
+ refreshToken<D = RefreshTokenRQ>(props?: RefreshTokenProps<D>): Promise<boolean>;
38
38
  /**
39
39
  * Try login
40
40
  */
@@ -1,4 +1,5 @@
1
- import { createClient } from '@etsoo/appscript';
1
+ import { ActionResultError, createClient } from '@etsoo/appscript';
2
+ import { ApiDataError } from '@etsoo/restclient';
2
3
  import { DomUtils } from '@etsoo/shared';
3
4
  import { CoreConstants } from './CoreConstants';
4
5
  import { ReactApp } from './ReactApp';
@@ -30,7 +31,7 @@ export class ServiceApp extends ReactApp {
30
31
  * Go to the login page
31
32
  */
32
33
  toLoginPage() {
33
- const coreUrl = this.settings.coreApi;
34
+ const coreUrl = this.settings.coreUrl;
34
35
  window.location.href =
35
36
  coreUrl +
36
37
  '?serviceId=' +
@@ -41,45 +42,50 @@ export class ServiceApp extends ReactApp {
41
42
  this.culture;
42
43
  }
43
44
  /**
44
- * Try login result processing
45
- * @param result Result
45
+ * Refresh token
46
+ * @param props Props
46
47
  */
47
- tryLoginResult(result) { }
48
- /**
49
- * Try login
50
- */
51
- async tryLogin() {
48
+ async refreshToken(props) {
49
+ // Destruct
50
+ const { callback, data, showLoading = false } = props !== null && props !== void 0 ? props : {};
52
51
  // Token
53
52
  const token = this.getCacheToken();
54
53
  if (token == null || token === '') {
55
- this.tryLoginResult('');
54
+ if (callback)
55
+ callback(false);
56
56
  return false;
57
57
  }
58
58
  // Reqest data
59
- const data = {
60
- timezone: this.getTimeZone()
59
+ // Merge additional data passed
60
+ const rq = {
61
+ timezone: this.getTimeZone(),
62
+ ...data
61
63
  };
62
64
  // Payload
63
65
  const payload = {
66
+ showLoading,
64
67
  config: { headers: { [CoreConstants.TokenHeaderRefresh]: token } },
65
68
  onError: (error) => {
66
- this.tryLoginResult(error);
69
+ if (callback)
70
+ callback(error);
67
71
  // Prevent further processing
68
72
  return false;
69
73
  }
70
74
  };
71
75
  // Call API
72
- const result = await this.coreApi.put('Auth/RefreshToken', data, payload);
76
+ const result = await this.coreApi.put('Auth/RefreshToken', rq, payload);
73
77
  if (result == null)
74
78
  return false;
75
79
  if (!result.ok) {
76
- this.tryLoginResult(result);
80
+ if (callback)
81
+ callback(result);
77
82
  return false;
78
83
  }
79
84
  // Token
80
85
  const refreshToken = this.getResponseToken(payload.response);
81
86
  if (refreshToken == null || result.data == null) {
82
- this.tryLoginResult(this.get('noData'));
87
+ if (callback)
88
+ callback(this.get('noData'));
83
89
  return false;
84
90
  }
85
91
  // User data
@@ -88,9 +94,10 @@ export class ServiceApp extends ReactApp {
88
94
  const serviceResult = await this.api.put('Auth/ExchangeToken', {
89
95
  token: userData.token
90
96
  }, {
97
+ showLoading,
91
98
  onError: (error) => {
92
- // Set message
93
- this.tryLoginResult(error);
99
+ if (callback)
100
+ callback(error);
94
101
  // Prevent further processing
95
102
  return false;
96
103
  }
@@ -98,16 +105,45 @@ export class ServiceApp extends ReactApp {
98
105
  if (serviceResult == null)
99
106
  return false;
100
107
  if (!serviceResult.ok) {
101
- this.tryLoginResult(result);
108
+ if (callback)
109
+ callback(serviceResult);
102
110
  return false;
103
111
  }
104
112
  if (serviceResult.data == null) {
105
- this.tryLoginResult(this.get('noData'));
113
+ if (callback)
114
+ callback(this.get('noData'));
106
115
  return false;
107
116
  }
108
117
  this.userLoginEx(serviceResult.data, refreshToken, userData);
109
118
  return true;
110
119
  }
120
+ /**
121
+ * Try login
122
+ */
123
+ async tryLogin() {
124
+ // Reset user state
125
+ const result = await super.tryLogin();
126
+ if (!result)
127
+ return false;
128
+ // Refresh token
129
+ return await this.refreshToken({
130
+ callback: (result) => {
131
+ if (typeof result === 'boolean') {
132
+ this.toLoginPage();
133
+ return;
134
+ }
135
+ const message = result instanceof ApiDataError
136
+ ? this.formatError(result)
137
+ : typeof result !== 'string'
138
+ ? ActionResultError.format(result)
139
+ : result;
140
+ this.notifier.alert(message, () => {
141
+ this.toLoginPage();
142
+ });
143
+ },
144
+ showLoading: true
145
+ });
146
+ }
111
147
  /**
112
148
  * User login extended
113
149
  * @param user New user
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.3.39",
3
+ "version": "1.3.43",
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.53",
53
+ "@etsoo/appscript": "^1.1.56",
54
54
  "@etsoo/notificationbase": "^1.0.94",
55
55
  "@etsoo/shared": "^1.0.75",
56
56
  "@mui/icons-material": "^5.2.0",
@@ -83,7 +83,7 @@
83
83
  "@types/react-test-renderer": "^17.0.1",
84
84
  "@typescript-eslint/eslint-plugin": "^5.5.0",
85
85
  "@typescript-eslint/parser": "^5.5.0",
86
- "eslint": "^8.3.0",
86
+ "eslint": "^8.4.0",
87
87
  "eslint-config-airbnb-base": "^15.0.0",
88
88
  "eslint-plugin-import": "^2.25.3",
89
89
  "eslint-plugin-react": "^7.27.1",
@@ -7,6 +7,11 @@ export type RefreshTokenRQ = {
7
7
  */
8
8
  serviceId?: number;
9
9
 
10
+ /**
11
+ * Service Uid
12
+ */
13
+ serviceUid?: string;
14
+
10
15
  /**
11
16
  * Time zone
12
17
  */
@@ -1,8 +1,9 @@
1
1
  import {
2
+ ActionResultError,
2
3
  createClient,
3
- IActionResult,
4
4
  IApi,
5
- IApiPayload
5
+ IApiPayload,
6
+ RefreshTokenProps
6
7
  } from '@etsoo/appscript';
7
8
  import { ApiDataError } from '@etsoo/restclient';
8
9
  import { DomUtils } from '@etsoo/shared';
@@ -60,7 +61,7 @@ export class ServiceApp<
60
61
  * Go to the login page
61
62
  */
62
63
  override toLoginPage() {
63
- const coreUrl = this.settings.coreApi;
64
+ const coreUrl = this.settings.coreUrl;
64
65
  window.location.href =
65
66
  coreUrl +
66
67
  '?serviceId=' +
@@ -72,32 +73,35 @@ export class ServiceApp<
72
73
  }
73
74
 
74
75
  /**
75
- * Try login result processing
76
- * @param result Result
76
+ * Refresh token
77
+ * @param props Props
77
78
  */
78
- protected tryLoginResult(result: string | ApiDataError | IActionResult) {}
79
+ override async refreshToken<D = RefreshTokenRQ>(
80
+ props?: RefreshTokenProps<D>
81
+ ) {
82
+ // Destruct
83
+ const { callback, data, showLoading = false } = props ?? {};
79
84
 
80
- /**
81
- * Try login
82
- */
83
- override async tryLogin() {
84
85
  // Token
85
86
  const token = this.getCacheToken();
86
87
  if (token == null || token === '') {
87
- this.tryLoginResult('');
88
+ if (callback) callback(false);
88
89
  return false;
89
90
  }
90
91
 
91
92
  // Reqest data
92
- const data: RefreshTokenRQ = {
93
- timezone: this.getTimeZone()
93
+ // Merge additional data passed
94
+ const rq: RefreshTokenRQ = {
95
+ timezone: this.getTimeZone(),
96
+ ...data
94
97
  };
95
98
 
96
99
  // Payload
97
100
  const payload: IApiPayload<SmartERPLoginResult, any> = {
101
+ showLoading,
98
102
  config: { headers: { [CoreConstants.TokenHeaderRefresh]: token } },
99
103
  onError: (error) => {
100
- this.tryLoginResult(error);
104
+ if (callback) callback(error);
101
105
 
102
106
  // Prevent further processing
103
107
  return false;
@@ -107,20 +111,20 @@ export class ServiceApp<
107
111
  // Call API
108
112
  const result = await this.coreApi.put<SmartERPLoginResult>(
109
113
  'Auth/RefreshToken',
110
- data,
114
+ rq,
111
115
  payload
112
116
  );
113
117
  if (result == null) return false;
114
118
 
115
119
  if (!result.ok) {
116
- this.tryLoginResult(result);
120
+ if (callback) callback(result);
117
121
  return false;
118
122
  }
119
123
 
120
124
  // Token
121
125
  const refreshToken = this.getResponseToken(payload.response);
122
126
  if (refreshToken == null || result.data == null) {
123
- this.tryLoginResult(this.get('noData')!);
127
+ if (callback) callback(this.get('noData')!);
124
128
  return false;
125
129
  }
126
130
 
@@ -134,9 +138,9 @@ export class ServiceApp<
134
138
  token: userData.token
135
139
  },
136
140
  {
141
+ showLoading,
137
142
  onError: (error) => {
138
- // Set message
139
- this.tryLoginResult(error);
143
+ if (callback) callback(error);
140
144
 
141
145
  // Prevent further processing
142
146
  return false;
@@ -147,12 +151,12 @@ export class ServiceApp<
147
151
  if (serviceResult == null) return false;
148
152
 
149
153
  if (!serviceResult.ok) {
150
- this.tryLoginResult(result);
154
+ if (callback) callback(serviceResult);
151
155
  return false;
152
156
  }
153
157
 
154
158
  if (serviceResult.data == null) {
155
- this.tryLoginResult(this.get('noData')!);
159
+ if (callback) callback(this.get('noData')!);
156
160
  return false;
157
161
  }
158
162
 
@@ -161,6 +165,37 @@ export class ServiceApp<
161
165
  return true;
162
166
  }
163
167
 
168
+ /**
169
+ * Try login
170
+ */
171
+ override async tryLogin() {
172
+ // Reset user state
173
+ const result = await super.tryLogin();
174
+ if (!result) return false;
175
+
176
+ // Refresh token
177
+ return await this.refreshToken({
178
+ callback: (result) => {
179
+ if (typeof result === 'boolean') {
180
+ this.toLoginPage();
181
+ return;
182
+ }
183
+
184
+ const message =
185
+ result instanceof ApiDataError
186
+ ? this.formatError(result)
187
+ : typeof result !== 'string'
188
+ ? ActionResultError.format(result)
189
+ : result;
190
+
191
+ this.notifier.alert(message, () => {
192
+ this.toLoginPage();
193
+ });
194
+ },
195
+ showLoading: true
196
+ });
197
+ }
198
+
164
199
  /**
165
200
  * User login extended
166
201
  * @param user New user