-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.jsx
186 lines (172 loc) · 5.56 KB
/
App.jsx
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// @flow
import React, { useCallback, Suspense, lazy, useState, useEffect } from 'react';
import {
Route,
BrowserRouter as Router,
Switch,
Redirect,
} from 'react-router-dom';
import { IntlProvider } from 'react-intl';
import Loader from 'react-loader-spinner';
import { ConfigProvider } from 'antd';
import frFR from 'antd/lib/locale/fr_FR';
import zhCN from 'antd/lib/locale/zh_CN';
import esES from 'antd/lib/locale/es_ES';
import enUS from 'antd/lib/locale/en_US';
import PrivateRoute from './components/PrivateRoute';
import Footer from './components/Footer';
import Navigation from './components/Navigation';
import ScrollToTop from './components/ScrollToTop';
import { useAuth } from './utils/use-auth';
import { getTranslationByLanguage } from './utils/api';
const Home = lazy(() => import('./pages/Home'));
const Login = lazy(() => import('./pages/Login'));
const Logout = lazy(() => import('./pages/Logout'));
const NotFound = lazy(() => import('./pages/NotFound'));
const PasswordReset = lazy(() => import('./pages/PasswordReset'));
const Register = lazy(() => import('./pages/Register'));
const Resources = lazy(() => import('./pages/Resources'));
const ResourceUnknown = lazy(() => import('./pages/ResourceUnknown'));
const SavedResources = lazy(() => import('./pages/SavedResources'));
const ResourceDetailCommon = lazy(() =>
import('./components/ResourceDetailCommon'),
);
const AdminResourceManager = lazy(() => import('./pages/AdminResourceManager'));
const Translations = lazy(() => import('./pages/Translations'));
const EditTranslations = lazy(() => import('./pages/EditTranslations'));
const FORMAT_JS_LOCALE_DICT = {
English: 'en',
Spanish: 'es',
French: 'fr',
Chinese: 'zh',
};
const ANTD_LOCALE_DICT = {
English: enUS,
Spanish: esES,
French: frFR,
Chinese: zhCN,
};
const App = (): React$Element<React$FragmentType> => {
const { authed, authRoleIsEquivalentTo } = useAuth();
const [language, setLanguage] = useState(
localStorage.getItem('language') || 'English',
);
const [messages, setMessages] = useState({});
useEffect(() => {
const fetchTranslations = async () => {
if (language === 'English') {
setMessages({});
} else {
const res = await getTranslationByLanguage(language);
let newMessages = {};
if (res && res.result) {
newMessages = res.result.messages;
}
setMessages(newMessages);
}
};
fetchTranslations();
}, [language, setMessages]);
const storeLanguage = (selectedLanguage) => {
localStorage.setItem('language', selectedLanguage);
setLanguage(selectedLanguage);
};
const showIfUnauthed = useCallback(
(component) => {
if (authed !== null && authed !== undefined) {
if (!authed) {
return component;
}
if (authRoleIsEquivalentTo('admin')) {
return <Redirect to="/admin" />;
}
if (authRoleIsEquivalentTo('volunteer')) {
return <Redirect to="/translations" />;
}
return <Redirect to="/" />;
}
return null;
},
[authRoleIsEquivalentTo, authed],
);
return (
<ConfigProvider locale={ANTD_LOCALE_DICT[language]}>
<IntlProvider
messages={messages}
locale={FORMAT_JS_LOCALE_DICT[language]}
>
<Router>
<ScrollToTop />
<Navigation language={language} setLanguage={storeLanguage} />
<Suspense
fallback={
<Loader
className="app-loader"
type="Circles"
color="#6A3E9E"
height={100}
width={100}
style={{
textAlign: 'center',
}}
/>
}
>
<Switch>
<Route path="/" exact component={Home} />
<PrivateRoute
path="/admin"
component={AdminResourceManager}
exact
minRole="admin"
/>
<PrivateRoute
path="/admin/:id"
component={AdminResourceManager}
minRole="admin"
/>
<PrivateRoute
path="/translations"
component={Translations}
exact
minRole="volunteer"
/>
<PrivateRoute
path="/translations/:id"
component={EditTranslations}
minRole="volunteer"
/>
<Route
path="/saved"
render={(props) => <SavedResources {...props} />}
/>
<Route path="/login" render={() => showIfUnauthed(<Login />)} />
<Route
path="/register"
render={() => showIfUnauthed(<Register />)}
/>
<Route
path="/password-reset"
render={() => showIfUnauthed(<PasswordReset />)}
/>
<Route path="/logout" render={() => <Logout />} />
<Route
path="/resources"
exact
render={(props) => <Resources {...props} />}
/>
<Route path="/resources/unknown" component={ResourceUnknown} />
<Route
path="/resources/:id"
render={(props) => <ResourceDetailCommon {...props} />}
/>
<Route component={NotFound} />
</Switch>
</Suspense>
<Footer />
</Router>
</IntlProvider>
</ConfigProvider>
);
};
export default App;