@etsoo/react 1.3.41 → 1.3.42

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,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
  */
@@ -41,45 +41,50 @@ export class ServiceApp extends ReactApp {
41
41
  this.culture;
42
42
  }
43
43
  /**
44
- * Try login result processing
45
- * @param result Result
44
+ * Refresh token
45
+ * @param props Props
46
46
  */
47
- tryLoginResult(result) { }
48
- /**
49
- * Try login
50
- */
51
- async tryLogin() {
47
+ async refreshToken(props) {
48
+ // Destruct
49
+ const { callback, data, showLoading = false } = props !== null && props !== void 0 ? props : {};
52
50
  // Token
53
51
  const token = this.getCacheToken();
54
52
  if (token == null || token === '') {
55
- this.tryLoginResult('');
53
+ if (callback)
54
+ callback(false);
56
55
  return false;
57
56
  }
58
57
  // Reqest data
59
- const data = {
60
- timezone: this.getTimeZone()
58
+ // Merge additional data passed
59
+ const rq = {
60
+ timezone: this.getTimeZone(),
61
+ ...data
61
62
  };
62
63
  // Payload
63
64
  const payload = {
65
+ showLoading,
64
66
  config: { headers: { [CoreConstants.TokenHeaderRefresh]: token } },
65
67
  onError: (error) => {
66
- this.tryLoginResult(error);
68
+ if (callback)
69
+ callback(error);
67
70
  // Prevent further processing
68
71
  return false;
69
72
  }
70
73
  };
71
74
  // Call API
72
- const result = await this.coreApi.put('Auth/RefreshToken', data, payload);
75
+ const result = await this.coreApi.put('Auth/RefreshToken', rq, payload);
73
76
  if (result == null)
74
77
  return false;
75
78
  if (!result.ok) {
76
- this.tryLoginResult(result);
79
+ if (callback)
80
+ callback(result);
77
81
  return false;
78
82
  }
79
83
  // Token
80
84
  const refreshToken = this.getResponseToken(payload.response);
81
85
  if (refreshToken == null || result.data == null) {
82
- this.tryLoginResult(this.get('noData'));
86
+ if (callback)
87
+ callback(this.get('noData'));
83
88
  return false;
84
89
  }
85
90
  // User data
@@ -88,9 +93,10 @@ export class ServiceApp extends ReactApp {
88
93
  const serviceResult = await this.api.put('Auth/ExchangeToken', {
89
94
  token: userData.token
90
95
  }, {
96
+ showLoading,
91
97
  onError: (error) => {
92
- // Set message
93
- this.tryLoginResult(error);
98
+ if (callback)
99
+ callback(error);
94
100
  // Prevent further processing
95
101
  return false;
96
102
  }
@@ -98,16 +104,33 @@ export class ServiceApp extends ReactApp {
98
104
  if (serviceResult == null)
99
105
  return false;
100
106
  if (!serviceResult.ok) {
101
- this.tryLoginResult(result);
107
+ if (callback)
108
+ callback(serviceResult);
102
109
  return false;
103
110
  }
104
111
  if (serviceResult.data == null) {
105
- this.tryLoginResult(this.get('noData'));
112
+ if (callback)
113
+ callback(this.get('noData'));
106
114
  return false;
107
115
  }
108
116
  this.userLoginEx(serviceResult.data, refreshToken, userData);
109
117
  return true;
110
118
  }
119
+ /**
120
+ * Try login
121
+ */
122
+ async tryLogin() {
123
+ // Reset user state
124
+ const result = await super.tryLogin();
125
+ if (!result)
126
+ return false;
127
+ // Refresh token
128
+ return await this.refreshToken({
129
+ callback: (_result) => {
130
+ this.toLoginPage();
131
+ }
132
+ });
133
+ }
111
134
  /**
112
135
  * User login extended
113
136
  * @param user New user
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.3.41",
3
+ "version": "1.3.42",
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",
@@ -1,10 +1,9 @@
1
1
  import {
2
2
  createClient,
3
- IActionResult,
4
3
  IApi,
5
- IApiPayload
4
+ IApiPayload,
5
+ RefreshTokenProps
6
6
  } from '@etsoo/appscript';
7
- import { ApiDataError } from '@etsoo/restclient';
8
7
  import { DomUtils } from '@etsoo/shared';
9
8
  import { CoreConstants } from './CoreConstants';
10
9
  import { IServiceAppSettings } from './IServiceAppSettings';
@@ -72,32 +71,35 @@ export class ServiceApp<
72
71
  }
73
72
 
74
73
  /**
75
- * Try login result processing
76
- * @param result Result
74
+ * Refresh token
75
+ * @param props Props
77
76
  */
78
- protected tryLoginResult(result: string | ApiDataError | IActionResult) {}
77
+ override async refreshToken<D = RefreshTokenRQ>(
78
+ props?: RefreshTokenProps<D>
79
+ ) {
80
+ // Destruct
81
+ const { callback, data, showLoading = false } = props ?? {};
79
82
 
80
- /**
81
- * Try login
82
- */
83
- override async tryLogin() {
84
83
  // Token
85
84
  const token = this.getCacheToken();
86
85
  if (token == null || token === '') {
87
- this.tryLoginResult('');
86
+ if (callback) callback(false);
88
87
  return false;
89
88
  }
90
89
 
91
90
  // Reqest data
92
- const data: RefreshTokenRQ = {
93
- timezone: this.getTimeZone()
91
+ // Merge additional data passed
92
+ const rq: RefreshTokenRQ = {
93
+ timezone: this.getTimeZone(),
94
+ ...data
94
95
  };
95
96
 
96
97
  // Payload
97
98
  const payload: IApiPayload<SmartERPLoginResult, any> = {
99
+ showLoading,
98
100
  config: { headers: { [CoreConstants.TokenHeaderRefresh]: token } },
99
101
  onError: (error) => {
100
- this.tryLoginResult(error);
102
+ if (callback) callback(error);
101
103
 
102
104
  // Prevent further processing
103
105
  return false;
@@ -107,20 +109,20 @@ export class ServiceApp<
107
109
  // Call API
108
110
  const result = await this.coreApi.put<SmartERPLoginResult>(
109
111
  'Auth/RefreshToken',
110
- data,
112
+ rq,
111
113
  payload
112
114
  );
113
115
  if (result == null) return false;
114
116
 
115
117
  if (!result.ok) {
116
- this.tryLoginResult(result);
118
+ if (callback) callback(result);
117
119
  return false;
118
120
  }
119
121
 
120
122
  // Token
121
123
  const refreshToken = this.getResponseToken(payload.response);
122
124
  if (refreshToken == null || result.data == null) {
123
- this.tryLoginResult(this.get('noData')!);
125
+ if (callback) callback(this.get('noData')!);
124
126
  return false;
125
127
  }
126
128
 
@@ -134,9 +136,9 @@ export class ServiceApp<
134
136
  token: userData.token
135
137
  },
136
138
  {
139
+ showLoading,
137
140
  onError: (error) => {
138
- // Set message
139
- this.tryLoginResult(error);
141
+ if (callback) callback(error);
140
142
 
141
143
  // Prevent further processing
142
144
  return false;
@@ -147,12 +149,12 @@ export class ServiceApp<
147
149
  if (serviceResult == null) return false;
148
150
 
149
151
  if (!serviceResult.ok) {
150
- this.tryLoginResult(result);
152
+ if (callback) callback(serviceResult);
151
153
  return false;
152
154
  }
153
155
 
154
156
  if (serviceResult.data == null) {
155
- this.tryLoginResult(this.get('noData')!);
157
+ if (callback) callback(this.get('noData')!);
156
158
  return false;
157
159
  }
158
160
 
@@ -161,6 +163,22 @@ export class ServiceApp<
161
163
  return true;
162
164
  }
163
165
 
166
+ /**
167
+ * Try login
168
+ */
169
+ override async tryLogin() {
170
+ // Reset user state
171
+ const result = await super.tryLogin();
172
+ if (!result) return false;
173
+
174
+ // Refresh token
175
+ return await this.refreshToken({
176
+ callback: (_result) => {
177
+ this.toLoginPage();
178
+ }
179
+ });
180
+ }
181
+
164
182
  /**
165
183
  * User login extended
166
184
  * @param user New user