Skip to content

Commit 26ce4e4

Browse files
committed
Handling Network Error when API is down
1 parent 8d400c8 commit 26ce4e4

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

src/components/app/App.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import Error404 from '../errors/Error404';
88
import Error403 from '../errors/Error403';
99
import Error401 from '../errors/Error401';
10+
import NetworkError from '../errors/NetworkError'
1011
import ErrorBoundary from '../errors/ErrorBoundary';
1112
import CheckAuth from './CheckAuth'
1213
import Footer from './Footer';
@@ -36,6 +37,7 @@ const AuthenticationRequiredRoute = ({component: Component, ...rest}) => (
3637
)
3738

3839
const App = props => {
40+
const [networkError, setNetworkError] = React.useState(false)
3941
const { alert, setAlert, setToggles } = React.useContext(OperationsContext);
4042
const setupHotJar = () => {
4143
/*eslint no-undef: 0*/
@@ -47,8 +49,12 @@ const App = props => {
4749
const fetchToggles = async () => {
4850
return new Promise(resolve => {
4951
APIService.toggles().get().then(response => {
50-
setToggles(response.data)
51-
resolve();
52+
if(response === 'Network Error')
53+
setNetworkError(true)
54+
else {
55+
setToggles(response.data)
56+
resolve();
57+
}
5258
});
5359
});
5460
}
@@ -93,6 +99,7 @@ const App = props => {
9399
<Header>
94100
<ErrorBoundary>
95101
<main className='content'>
102+
{networkError && <NetworkError />}
96103
<Switch>
97104
<Route exact path="/oidc/login" component={OIDLoginCallback} />
98105
<AuthenticationRequiredRoute exact path='/' component={MapProjects} />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import { useTranslation } from 'react-i18next'
3+
4+
const NetworkError = () => {
5+
const { t } = useTranslation()
6+
return (
7+
<div style={{display: 'flex', height: 'calc(100vh - 100px)', alignItems: 'center', justifyContent: 'center', textAlign: 'center', flexDirection: 'column'}}>
8+
<div className='col-xs-12'>
9+
<p style={{color: '#000', fontSize: '24px', margin: '16px 0'}}>
10+
{t('errors.network_error')}
11+
</p>
12+
</div>
13+
</div>
14+
)
15+
}
16+
17+
export default NetworkError;

src/i18n/locales/en/translations.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@
9595
"errors": {
9696
"404": "Sorry your page could not be found.",
9797
"401": "Login is required",
98-
"403": "Access to this page is not authorized"
98+
"403": "Access to this page is not authorized",
99+
"network_error": "Network Error"
99100
},
100101
"dashboard": {
101102
"name": "Dashboard",

0 commit comments

Comments
 (0)