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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React, {useEffect} from "react";
import axios from "axios";
import { appGrowl } from './layout/components/AppGrowl';
import UIConstants from './utils/ui.constants';
import Auth from './authenticate/auth';
import ShowErrorDetails from './show.error.details';
/**
* Trigger and validate the response for https status code
* @param {*} Wrapped
* @returns
*/
const handleResponse= Wrapped => {
function HandleResponse(props) {
useEffect(()=>{
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
if (error.response.status !== 403) {
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 httpStatusMsg = UIConstants.httpStatusMessages[response.status];
if(httpStatusMsg) {
appGrowl.show({severity: httpStatusMsg.severity, summary: httpStatusMsg.summary, sticky: httpStatusMsg.sticky,
detail: <ShowErrorDetails response={response} /> });
} else {
appGrowl.show({severity: 'error', summary: 'Error', sticky: 'true', detail: <ShowErrorDetails response={response} /> });
}
var elements = document.getElementsByClassName('p-growl p-component p-growl-topright');
if(elements) {
for(var i = 0 ; i < elements.length; i++){
elements[i].classList.remove("show_error");
}
}
if (response.status === 401) {
Auth.logout();
window.location.href = "/login";
}
}
return HandleResponse;
}
export default handleResponse;