Skip to content
Snippets Groups Projects
Commit 40e6f58b authored by Ramesh Kumar's avatar Ramesh Kumar
Browse files

TMSS-649: Updated function names and added logout on 401 status from API call.

parent 3939564a
No related branches found
No related tags found
1 merge request!392Resolve TMSS-649
...@@ -8,7 +8,7 @@ import {AppFooter } from './layout/components/AppFooter'; ...@@ -8,7 +8,7 @@ import {AppFooter } from './layout/components/AppFooter';
import {RoutedContent} from './routes'; import {RoutedContent} from './routes';
import {AppBreadcrumb } from "./layout/components/AppBreadcrumb"; import {AppBreadcrumb } from "./layout/components/AppBreadcrumb";
import {withRouter } from 'react-router'; import {withRouter } from 'react-router';
import validateResponse from "./response.handler" import handleResponse from "./response.handler"
import { setAppGrowl } from './layout/components/AppGrowl'; import { setAppGrowl } from './layout/components/AppGrowl';
import { Growl } from 'primereact/components/growl/Growl'; import { Growl } from 'primereact/components/growl/Growl';
...@@ -181,4 +181,4 @@ class App extends Component { ...@@ -181,4 +181,4 @@ class App extends Component {
} }
} }
export default validateResponse(App); export default handleResponse(App);
...@@ -2,13 +2,14 @@ import React, {useEffect} from "react"; ...@@ -2,13 +2,14 @@ import React, {useEffect} from "react";
import axios from "axios"; import axios from "axios";
import { appGrowl } from './layout/components/AppGrowl'; import { appGrowl } from './layout/components/AppGrowl';
import UIConstants from './utils/ui.constants'; import UIConstants from './utils/ui.constants';
import Auth from './authenticate/auth';
/** /**
* Trigger and validate the response for https status code * Trigger and validate the response for https status code
* @param {*} Wrapped * @param {*} Wrapped
* @returns * @returns
*/ */
const validateResponse= Wrapped => { const handleResponse= Wrapped => {
function ValidateResponse(props) { function HandleResponse(props) {
useEffect(()=>{ useEffect(()=>{
axios.interceptors.response.use(function (response) { axios.interceptors.response.use(function (response) {
return response; return response;
...@@ -27,14 +28,18 @@ const validateResponse= Wrapped => { ...@@ -27,14 +28,18 @@ const validateResponse= Wrapped => {
* @param {*} response * @param {*} response
*/ */
function showMessage(response) { function showMessage(response) {
const httpres = UIConstants.httpResponses[response.status]; const httpStatusMsg = UIConstants.httpStatusMessages[response.status];
if(httpres) { if(httpStatusMsg) {
appGrowl.show({severity: httpres.severity, summary: httpres.summary, sticky: httpres.sticky, detail: '['+response.status+'] '+JSON.stringify(response.statusText)+ ' ['+httpres.detail+']'}); appGrowl.show({severity: httpStatusMsg.severity, summary: httpStatusMsg.summary, sticky: httpStatusMsg.sticky, detail: '['+response.status+'] '+JSON.stringify(response.statusText)+ ' ['+httpStatusMsg.detail+']'});
} else { } else {
appGrowl.show({severity: 'error', summary: 'Error', sticky: 'true', detail: '['+response.status+'] '+JSON.stringify(response.statusText)+ ' '+JSON.stringify(response.data)}); appGrowl.show({severity: 'error', summary: 'Error', sticky: 'true', detail: '['+response.status+'] '+JSON.stringify(response.statusText)+ ' '+JSON.stringify(response.data)});
} }
if (response.status === 401) {
Auth.logout();
window.location.href = "/login";
} }
return ValidateResponse; }
return HandleResponse;
} }
export default validateResponse; export default handleResponse;
\ No newline at end of file \ No newline at end of file
...@@ -3,7 +3,7 @@ const UIConstants = { ...@@ -3,7 +3,7 @@ const UIConstants = {
timeline: { timeline: {
types: { NORMAL: "NORMAL", WEEKVIEW:"WEEKVIEW"} types: { NORMAL: "NORMAL", WEEKVIEW:"WEEKVIEW"}
}, },
httpResponses: { httpStatusMessages: {
400: {severity: 'error', summary: 'Error', sticky: true, detail: 'Error while process request, please contact system admin'}, 400: {severity: 'error', summary: 'Error', sticky: true, detail: 'Error while process request, please contact system admin'},
401: {severity: 'error', summary: 'Error', sticky: true, detail: 'Not authenticated, Please retry to login with valid credential'}, 401: {severity: 'error', summary: 'Error', sticky: true, detail: 'Not authenticated, Please retry to login with valid credential'},
403: {severity: 'error', summary: 'Error', sticky: true, detail: 'Unknown request, please contact system admin'}, 403: {severity: 'error', summary: 'Error', sticky: true, detail: 'Unknown request, please contact system admin'},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment