Skip to content

Commit f050a8b

Browse files
author
Jovert Lota Palonpon
committed
[API] Fixed fetching error in UsersList
1 parent 71de3af commit f050a8b

File tree

2 files changed

+105
-21
lines changed

2 files changed

+105
-21
lines changed

resources/js/Backoffice.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ import { Loading } from './views';
1010
class Backoffice extends Component {
1111
state = {
1212
loading: true,
13+
retrying: false,
1314
navigating: true,
1415
authenticated: false,
1516
nightMode: false,
1617
token: {},
1718
user: {},
1819
username: '',
20+
21+
errorResponse: {},
22+
successfulResponse: {},
23+
responseInterceptor: null,
1924
};
2025

2126
/**
@@ -204,9 +209,91 @@ class Backoffice extends Component {
204209
} catch (error) {}
205210
};
206211

212+
/**
213+
* Remove the response interceptor.
214+
*
215+
* @param {any} interceptor
216+
*
217+
* @param {undefined}
218+
*/
219+
removeResponseInterceptor = interceptor => {
220+
axios.interceptors.response.eject(interceptor);
221+
};
222+
223+
/**
224+
* Record API responses & do something.
225+
*
226+
* @param {any} interceptor
227+
*
228+
* @param {undefined}
229+
*/
230+
addResponseInterceptor = () => {
231+
const responseInterceptor = axios.interceptors.response.use(
232+
response => {
233+
return response;
234+
},
235+
236+
async error => {
237+
// In occasions of Unauthorized requests, retry.
238+
if (error.response.status === 401) {
239+
this.setState({
240+
retrying: true,
241+
});
242+
243+
// Request options
244+
const {
245+
url,
246+
method,
247+
headers,
248+
params: data,
249+
} = error.response.config;
250+
251+
const response = await axios({
252+
url,
253+
method,
254+
headers: {
255+
...headers,
256+
257+
// This is an override of the Authorization header
258+
// to fix the 401 errors when we retry.
259+
Authorization:
260+
window.axios.defaults.headers.common[
261+
'Authorization'
262+
],
263+
},
264+
data,
265+
});
266+
267+
// If the request returns 200 or 201 (Resource created),
268+
// Treat it as successful response.
269+
if ([200, 201].indexOf(response.status) > -1) {
270+
this.setState({
271+
successfulResponse: response,
272+
});
273+
}
274+
275+
this.setState({
276+
retrying: false,
277+
});
278+
}
279+
280+
return Promise.reject(error);
281+
},
282+
);
283+
284+
this.setState({
285+
responseInterceptor,
286+
});
287+
};
288+
207289
async componentDidMount() {
290+
// Listen for all API responses.
291+
this.addResponseInterceptor();
292+
293+
// Setup Night Mode via Persistent Storage.
208294
this.setNightMode();
209295

296+
// Authenticate via Persistent Storage.
210297
const token = this.token();
211298

212299
if (token) {
@@ -216,6 +303,12 @@ class Backoffice extends Component {
216303
this.setState({ loading: false, navigating: false });
217304
}
218305

306+
componentWillUnmount() {
307+
const { responseInterceptor } = this.state;
308+
309+
this.removeResponseInterceptor(responseInterceptor);
310+
}
311+
219312
render() {
220313
const { width } = this.props;
221314
const { loading, nightMode } = this.state;

resources/js/views/__backoffice/users/List.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -321,18 +321,7 @@ class List extends Component {
321321
};
322322
});
323323
} catch (error) {
324-
this.setState({
325-
loading: false,
326-
message: {
327-
type: 'error',
328-
body: Lang.get('resources.not_fetched', {
329-
name: 'User',
330-
}),
331-
closed: () => this.setState({ message: {} }),
332-
actionText: Lang.get('actions.retry'),
333-
action: async () => await this.fetchUsers(),
334-
},
335-
});
324+
this.setState({ loading: false });
336325
}
337326
};
338327

@@ -358,14 +347,16 @@ class List extends Component {
358347
}
359348

360349
render() {
361-
const {
362-
loading,
363-
pagination,
364-
sorting,
365-
filters,
366-
message,
367-
alert,
368-
} = this.state;
350+
const { loading, sorting, filters, message, alert } = this.state;
351+
352+
let { pagination } = this.state;
353+
354+
// Use the response data from the successful response (result of retry
355+
// on failed (401) API requests) for pagination.
356+
const { successfulResponse: response, retrying } = this.props.pageProps;
357+
if (response.hasOwnProperty('data') && !retrying) {
358+
pagination = response.data;
359+
}
369360

370361
const {
371362
data: rawData,
@@ -490,7 +481,7 @@ class List extends Component {
490481
pageTitle={Lang.get('navigation.users')}
491482
primaryAction={primaryAction}
492483
tabs={tabs}
493-
loading={loading}
484+
loading={loading || pageProps.retrying}
494485
message={message}
495486
alert={alert}
496487
>

0 commit comments

Comments
 (0)