Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import React, {useEffect} from "react";
import axios from "axios";
import { appGrowl } from './layout/components/AppGrowl';
import UIConstants from './utils/ui.constants';
/**
* Trigger and validate the response for https status code
* @param {*} Wrapped
* @returns
*/
const validateResponse= Wrapped => {
function ValidateResponse(props) {
useEffect(()=>{
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
showMessage(error.response);
return Promise.reject(error);
});
})
return (
<Wrapped {...props} />
);
}
/**
* Catch relavent http status code details to show in growl
* @param {*} response
*/
function showMessage(response) {
const httpres = UIConstants.httpResponses[response.status];
if(httpres) {
appGrowl.show({severity: httpres.severity, summary: httpres.summary, sticky: httpres.sticky, detail: '['+response.status+'] '+JSON.stringify(response.statusText)+ ' ['+httpres.detail+']'});
} else {
appGrowl.show({severity: 'error', summary: 'Error', sticky: 'true', detail: '['+response.status+'] '+JSON.stringify(response.statusText)+ ' '+JSON.stringify(response.data)});
}
}
return ValidateResponse;
}
export default validateResponse;