|
@@ -1,219 +1,205 @@
|
1
|
1
|
// @flow
|
2
|
2
|
|
3
|
3
|
import * as React from 'react';
|
4
|
|
-import ConnectionManager from "../../managers/ConnectionManager";
|
5
|
|
-import {ERROR_TYPE} from "../../utils/WebData";
|
6
|
|
-import ErrorView from "../Screens/ErrorView";
|
7
|
|
-import BasicLoadingScreen from "../Screens/BasicLoadingScreen";
|
8
|
|
-import {StackNavigationProp} from "@react-navigation/stack";
|
9
|
|
-
|
10
|
|
-type Props = {
|
11
|
|
- navigation: StackNavigationProp,
|
12
|
|
- requests: Array<{
|
13
|
|
- link: string,
|
14
|
|
- params: Object,
|
15
|
|
- mandatory: boolean
|
16
|
|
- }>,
|
17
|
|
- renderFunction: (Array<{ [key: string]: any } | null>) => React.Node,
|
18
|
|
- errorViewOverride?: Array<{
|
19
|
|
- errorCode: number,
|
20
|
|
- message: string,
|
21
|
|
- icon: string,
|
22
|
|
- showRetryButton: boolean
|
23
|
|
- }>,
|
24
|
|
-}
|
25
|
|
-
|
26
|
|
-type State = {
|
27
|
|
- loading: boolean,
|
28
|
|
-}
|
29
|
|
-
|
30
|
|
-class AuthenticatedScreen extends React.Component<Props, State> {
|
31
|
|
-
|
32
|
|
- state = {
|
33
|
|
- loading: true,
|
34
|
|
- };
|
35
|
|
-
|
36
|
|
- currentUserToken: string | null;
|
37
|
|
- connectionManager: ConnectionManager;
|
38
|
|
- errors: Array<number>;
|
39
|
|
- fetchedData: Array<{ [key: string]: any } | null>;
|
40
|
|
-
|
41
|
|
- constructor(props: Object) {
|
42
|
|
- super(props);
|
43
|
|
- this.connectionManager = ConnectionManager.getInstance();
|
44
|
|
- this.props.navigation.addListener('focus', this.onScreenFocus);
|
45
|
|
- this.fetchedData = new Array(this.props.requests.length);
|
46
|
|
- this.errors = new Array(this.props.requests.length);
|
47
|
|
- }
|
48
|
|
-
|
49
|
|
- /**
|
50
|
|
- * Refreshes screen if user changed
|
51
|
|
- */
|
52
|
|
- onScreenFocus = () => {
|
53
|
|
- if (this.currentUserToken !== this.connectionManager.getToken()) {
|
54
|
|
- this.currentUserToken = this.connectionManager.getToken();
|
55
|
|
- this.fetchData();
|
56
|
|
- }
|
|
4
|
+import {StackNavigationProp} from '@react-navigation/stack';
|
|
5
|
+import ConnectionManager from '../../managers/ConnectionManager';
|
|
6
|
+import type {ApiGenericDataType} from '../../utils/WebData';
|
|
7
|
+import {ERROR_TYPE} from '../../utils/WebData';
|
|
8
|
+import ErrorView from '../Screens/ErrorView';
|
|
9
|
+import BasicLoadingScreen from '../Screens/BasicLoadingScreen';
|
|
10
|
+
|
|
11
|
+type PropsType = {
|
|
12
|
+ navigation: StackNavigationProp,
|
|
13
|
+ requests: Array<{
|
|
14
|
+ link: string,
|
|
15
|
+ params: {...},
|
|
16
|
+ mandatory: boolean,
|
|
17
|
+ }>,
|
|
18
|
+ renderFunction: (Array<ApiGenericDataType | null>) => React.Node,
|
|
19
|
+ errorViewOverride?: Array<{
|
|
20
|
+ errorCode: number,
|
|
21
|
+ message: string,
|
|
22
|
+ icon: string,
|
|
23
|
+ showRetryButton: boolean,
|
|
24
|
+ }> | null,
|
|
25
|
+};
|
|
26
|
+
|
|
27
|
+type StateType = {
|
|
28
|
+ loading: boolean,
|
|
29
|
+};
|
|
30
|
+
|
|
31
|
+class AuthenticatedScreen extends React.Component<PropsType, StateType> {
|
|
32
|
+ static defaultProps = {
|
|
33
|
+ errorViewOverride: null,
|
|
34
|
+ };
|
|
35
|
+
|
|
36
|
+ currentUserToken: string | null;
|
|
37
|
+
|
|
38
|
+ connectionManager: ConnectionManager;
|
|
39
|
+
|
|
40
|
+ errors: Array<number>;
|
|
41
|
+
|
|
42
|
+ fetchedData: Array<ApiGenericDataType | null>;
|
|
43
|
+
|
|
44
|
+ constructor(props: PropsType) {
|
|
45
|
+ super(props);
|
|
46
|
+ this.state = {
|
|
47
|
+ loading: true,
|
57
|
48
|
};
|
58
|
|
-
|
59
|
|
- /**
|
60
|
|
- * Fetches the data from the server.
|
61
|
|
- *
|
62
|
|
- * If the user is not logged in errorCode is set to BAD_TOKEN and all requests fail.
|
63
|
|
- *
|
64
|
|
- * If the user is logged in, send all requests.
|
65
|
|
- */
|
66
|
|
- fetchData = () => {
|
67
|
|
- if (!this.state.loading)
|
68
|
|
- this.setState({loading: true});
|
69
|
|
- if (this.connectionManager.isLoggedIn()) {
|
70
|
|
- for (let i = 0; i < this.props.requests.length; i++) {
|
71
|
|
- this.connectionManager.authenticatedRequest(
|
72
|
|
- this.props.requests[i].link,
|
73
|
|
- this.props.requests[i].params)
|
74
|
|
- .then((data) => {
|
75
|
|
- this.onRequestFinished(data, i, -1);
|
76
|
|
- })
|
77
|
|
- .catch((error) => {
|
78
|
|
- this.onRequestFinished(null, i, error);
|
79
|
|
- });
|
80
|
|
- }
|
81
|
|
- } else {
|
82
|
|
- for (let i = 0; i < this.props.requests.length; i++) {
|
83
|
|
- this.onRequestFinished(null, i, ERROR_TYPE.BAD_TOKEN);
|
84
|
|
- }
|
85
|
|
- }
|
86
|
|
- };
|
87
|
|
-
|
88
|
|
- /**
|
89
|
|
- * Callback used when a request finishes, successfully or not.
|
90
|
|
- * Saves data and error code.
|
91
|
|
- * If the token is invalid, logout the user and open the login screen.
|
92
|
|
- * If the last request was received, stop the loading screen.
|
93
|
|
- *
|
94
|
|
- * @param data The data fetched from the server
|
95
|
|
- * @param index The index for the data
|
96
|
|
- * @param error The error code received
|
97
|
|
- */
|
98
|
|
- onRequestFinished(data: { [key: string]: any } | null, index: number, error: number) {
|
99
|
|
- if (index >= 0 && index < this.props.requests.length) {
|
100
|
|
- this.fetchedData[index] = data;
|
101
|
|
- this.errors[index] = error;
|
102
|
|
- }
|
103
|
|
-
|
104
|
|
- if (error === ERROR_TYPE.BAD_TOKEN) // Token expired, logout user
|
105
|
|
- this.connectionManager.disconnect();
|
106
|
|
-
|
107
|
|
- if (this.allRequestsFinished())
|
108
|
|
- this.setState({loading: false});
|
|
49
|
+ this.connectionManager = ConnectionManager.getInstance();
|
|
50
|
+ props.navigation.addListener('focus', this.onScreenFocus);
|
|
51
|
+ this.fetchedData = new Array(props.requests.length);
|
|
52
|
+ this.errors = new Array(props.requests.length);
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ /**
|
|
56
|
+ * Refreshes screen if user changed
|
|
57
|
+ */
|
|
58
|
+ onScreenFocus = () => {
|
|
59
|
+ if (this.currentUserToken !== this.connectionManager.getToken()) {
|
|
60
|
+ this.currentUserToken = this.connectionManager.getToken();
|
|
61
|
+ this.fetchData();
|
109
|
62
|
}
|
110
|
|
-
|
111
|
|
- /**
|
112
|
|
- * Checks if all requests finished processing
|
113
|
|
- *
|
114
|
|
- * @return {boolean} True if all finished
|
115
|
|
- */
|
116
|
|
- allRequestsFinished() {
|
117
|
|
- let finished = true;
|
118
|
|
- for (let i = 0; i < this.fetchedData.length; i++) {
|
119
|
|
- if (this.fetchedData[i] === undefined) {
|
120
|
|
- finished = false;
|
121
|
|
- break;
|
122
|
|
- }
|
123
|
|
- }
|
124
|
|
- return finished;
|
125
|
|
- }
|
126
|
|
-
|
127
|
|
- /**
|
128
|
|
- * Checks if all requests have finished successfully.
|
129
|
|
- * This will return false only if a mandatory request failed.
|
130
|
|
- * All non-mandatory requests can fail without impacting the return value.
|
131
|
|
- *
|
132
|
|
- * @return {boolean} True if all finished successfully
|
133
|
|
- */
|
134
|
|
- allRequestsValid() {
|
135
|
|
- let valid = true;
|
136
|
|
- for (let i = 0; i < this.fetchedData.length; i++) {
|
137
|
|
- if (this.fetchedData[i] === null && this.props.requests[i].mandatory) {
|
138
|
|
- valid = false;
|
139
|
|
- break;
|
140
|
|
- }
|
141
|
|
- }
|
142
|
|
- return valid;
|
|
63
|
+ };
|
|
64
|
+
|
|
65
|
+ /**
|
|
66
|
+ * Callback used when a request finishes, successfully or not.
|
|
67
|
+ * Saves data and error code.
|
|
68
|
+ * If the token is invalid, logout the user and open the login screen.
|
|
69
|
+ * If the last request was received, stop the loading screen.
|
|
70
|
+ *
|
|
71
|
+ * @param data The data fetched from the server
|
|
72
|
+ * @param index The index for the data
|
|
73
|
+ * @param error The error code received
|
|
74
|
+ */
|
|
75
|
+ onRequestFinished(
|
|
76
|
+ data: ApiGenericDataType | null,
|
|
77
|
+ index: number,
|
|
78
|
+ error?: number,
|
|
79
|
+ ) {
|
|
80
|
+ const {props} = this;
|
|
81
|
+ if (index >= 0 && index < props.requests.length) {
|
|
82
|
+ this.fetchedData[index] = data;
|
|
83
|
+ this.errors[index] = error != null ? error : ERROR_TYPE.SUCCESS;
|
143
|
84
|
}
|
144
|
|
-
|
145
|
|
- /**
|
146
|
|
- * Gets the error to render.
|
147
|
|
- * Non-mandatory requests are ignored.
|
148
|
|
- *
|
149
|
|
- *
|
150
|
|
- * @return {number} The error code or ERROR_TYPE.SUCCESS if no error was found
|
151
|
|
- */
|
152
|
|
- getError() {
|
153
|
|
- for (let i = 0; i < this.errors.length; i++) {
|
154
|
|
- if (this.errors[i] !== 0 && this.props.requests[i].mandatory) {
|
155
|
|
- return this.errors[i];
|
156
|
|
- }
|
157
|
|
- }
|
158
|
|
- return ERROR_TYPE.SUCCESS;
|
|
85
|
+ // Token expired, logout user
|
|
86
|
+ if (error === ERROR_TYPE.BAD_TOKEN) this.connectionManager.disconnect();
|
|
87
|
+
|
|
88
|
+ if (this.allRequestsFinished()) this.setState({loading: false});
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ /**
|
|
92
|
+ * Gets the error to render.
|
|
93
|
+ * Non-mandatory requests are ignored.
|
|
94
|
+ *
|
|
95
|
+ *
|
|
96
|
+ * @return {number} The error code or ERROR_TYPE.SUCCESS if no error was found
|
|
97
|
+ */
|
|
98
|
+ getError(): number {
|
|
99
|
+ const {props} = this;
|
|
100
|
+ for (let i = 0; i < this.errors.length; i += 1) {
|
|
101
|
+ if (
|
|
102
|
+ this.errors[i] !== ERROR_TYPE.SUCCESS &&
|
|
103
|
+ props.requests[i].mandatory
|
|
104
|
+ ) {
|
|
105
|
+ return this.errors[i];
|
|
106
|
+ }
|
159
|
107
|
}
|
160
|
|
-
|
161
|
|
- /**
|
162
|
|
- * Gets the error view to display in case of error
|
163
|
|
- *
|
164
|
|
- * @return {*}
|
165
|
|
- */
|
166
|
|
- getErrorRender() {
|
167
|
|
- const errorCode = this.getError();
|
168
|
|
- let shouldOverride = false;
|
169
|
|
- let override = null;
|
170
|
|
- const overrideList = this.props.errorViewOverride;
|
171
|
|
- if (overrideList != null) {
|
172
|
|
- for (let i = 0; i < overrideList.length; i++) {
|
173
|
|
- if (overrideList[i].errorCode === errorCode) {
|
174
|
|
- shouldOverride = true;
|
175
|
|
- override = overrideList[i];
|
176
|
|
- break;
|
177
|
|
- }
|
178
|
|
- }
|
|
108
|
+ return ERROR_TYPE.SUCCESS;
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ /**
|
|
112
|
+ * Gets the error view to display in case of error
|
|
113
|
+ *
|
|
114
|
+ * @return {*}
|
|
115
|
+ */
|
|
116
|
+ getErrorRender(): React.Node {
|
|
117
|
+ const {props} = this;
|
|
118
|
+ const errorCode = this.getError();
|
|
119
|
+ let shouldOverride = false;
|
|
120
|
+ let override = null;
|
|
121
|
+ const overrideList = props.errorViewOverride;
|
|
122
|
+ if (overrideList != null) {
|
|
123
|
+ for (let i = 0; i < overrideList.length; i += 1) {
|
|
124
|
+ if (overrideList[i].errorCode === errorCode) {
|
|
125
|
+ shouldOverride = true;
|
|
126
|
+ override = overrideList[i];
|
|
127
|
+ break;
|
179
|
128
|
}
|
180
|
|
- if (shouldOverride && override != null) {
|
181
|
|
- return (
|
182
|
|
- <ErrorView
|
183
|
|
- {...this.props}
|
184
|
|
- icon={override.icon}
|
185
|
|
- message={override.message}
|
186
|
|
- showRetryButton={override.showRetryButton}
|
187
|
|
- />
|
188
|
|
- );
|
189
|
|
- } else {
|
190
|
|
- return (
|
191
|
|
- <ErrorView
|
192
|
|
- {...this.props}
|
193
|
|
- errorCode={errorCode}
|
194
|
|
- onRefresh={this.fetchData}
|
195
|
|
- />
|
196
|
|
- );
|
197
|
|
- }
|
198
|
|
-
|
|
129
|
+ }
|
199
|
130
|
}
|
200
|
131
|
|
201
|
|
- /**
|
202
|
|
- * Reloads the data, to be called using ref by parent components
|
203
|
|
- */
|
204
|
|
- reload() {
|
205
|
|
- this.fetchData();
|
|
132
|
+ if (shouldOverride && override != null) {
|
|
133
|
+ return (
|
|
134
|
+ <ErrorView
|
|
135
|
+ icon={override.icon}
|
|
136
|
+ message={override.message}
|
|
137
|
+ showRetryButton={override.showRetryButton}
|
|
138
|
+ />
|
|
139
|
+ );
|
206
|
140
|
}
|
207
|
|
-
|
208
|
|
- render() {
|
209
|
|
- return (
|
210
|
|
- this.state.loading
|
211
|
|
- ? <BasicLoadingScreen/>
|
212
|
|
- : (this.allRequestsValid()
|
213
|
|
- ? this.props.renderFunction(this.fetchedData)
|
214
|
|
- : this.getErrorRender())
|
215
|
|
- );
|
|
141
|
+ return <ErrorView errorCode={errorCode} onRefresh={this.fetchData} />;
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ /**
|
|
145
|
+ * Fetches the data from the server.
|
|
146
|
+ *
|
|
147
|
+ * If the user is not logged in errorCode is set to BAD_TOKEN and all requests fail.
|
|
148
|
+ *
|
|
149
|
+ * If the user is logged in, send all requests.
|
|
150
|
+ */
|
|
151
|
+ fetchData = () => {
|
|
152
|
+ const {state, props} = this;
|
|
153
|
+ if (!state.loading) this.setState({loading: true});
|
|
154
|
+
|
|
155
|
+ if (this.connectionManager.isLoggedIn()) {
|
|
156
|
+ for (let i = 0; i < props.requests.length; i += 1) {
|
|
157
|
+ this.connectionManager
|
|
158
|
+ .authenticatedRequest(
|
|
159
|
+ props.requests[i].link,
|
|
160
|
+ props.requests[i].params,
|
|
161
|
+ )
|
|
162
|
+ .then((response: ApiGenericDataType): void =>
|
|
163
|
+ this.onRequestFinished(response, i),
|
|
164
|
+ )
|
|
165
|
+ .catch((error: number): void =>
|
|
166
|
+ this.onRequestFinished(null, i, error),
|
|
167
|
+ );
|
|
168
|
+ }
|
|
169
|
+ } else {
|
|
170
|
+ for (let i = 0; i < props.requests.length; i += 1) {
|
|
171
|
+ this.onRequestFinished(null, i, ERROR_TYPE.BAD_TOKEN);
|
|
172
|
+ }
|
216
|
173
|
}
|
|
174
|
+ };
|
|
175
|
+
|
|
176
|
+ /**
|
|
177
|
+ * Checks if all requests finished processing
|
|
178
|
+ *
|
|
179
|
+ * @return {boolean} True if all finished
|
|
180
|
+ */
|
|
181
|
+ allRequestsFinished(): boolean {
|
|
182
|
+ let finished = true;
|
|
183
|
+ this.errors.forEach((error: number | null) => {
|
|
184
|
+ if (error == null) finished = false;
|
|
185
|
+ });
|
|
186
|
+ return finished;
|
|
187
|
+ }
|
|
188
|
+
|
|
189
|
+ /**
|
|
190
|
+ * Reloads the data, to be called using ref by parent components
|
|
191
|
+ */
|
|
192
|
+ reload() {
|
|
193
|
+ this.fetchData();
|
|
194
|
+ }
|
|
195
|
+
|
|
196
|
+ render(): React.Node {
|
|
197
|
+ const {state, props} = this;
|
|
198
|
+ if (state.loading) return <BasicLoadingScreen />;
|
|
199
|
+ if (this.getError() === ERROR_TYPE.SUCCESS)
|
|
200
|
+ return props.renderFunction(this.fetchedData);
|
|
201
|
+ return this.getErrorRender();
|
|
202
|
+ }
|
217
|
203
|
}
|
218
|
204
|
|
219
|
205
|
export default AuthenticatedScreen;
|