Newer
Older
import axios from "axios";
import { appGrowl } from './layout/components/AppGrowl';
import UIConstants from './utils/ui.constants';

Ramesh Kumar
committed
import Auth from './authenticate/auth';
import ShowErrorDetails from './show.error.details';
/**
* Trigger and validate the response for https status code
* @param {*} Wrapped
* @returns
*/

Ramesh Kumar
committed
const handleResponse= Wrapped => {

Ramesh Kumar
committed
function HandleResponse(props) {
useEffect(()=>{
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
if (error.response.status !== 403 &&error.response.status !== 404 &&error.response.status !== 400) {
{
if (!error?.response?.data?.indexOf("Keycloak admin API token could not be obtained")>0) {
showMessage(error.response);
}
}

Ramesh Kumar
committed
}
return Promise.reject(error);
});
})
return (
<Wrapped {...props} />
);
}
/**
* Catch relavent http status code details to show in growl
* @param {*} response
*/
function showMessage(response) {

Ramesh Kumar
committed
const httpStatusMsg = UIConstants.httpStatusMessages[response.status];
if(httpStatusMsg) {
appGrowl?.show({severity: httpStatusMsg.severity, summary: httpStatusMsg.summary, sticky: httpStatusMsg.sticky,
detail: <ShowErrorDetails response={response} /> });
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");
}
}

Ramesh Kumar
committed
if (response.status === 401) {
Auth.logout();
window.location.href = "/login";
}
return HandleResponse;

Ramesh Kumar
committed
export default handleResponse;