forked from graasp/graasp-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
278 lines (256 loc) · 8.53 KB
/
App.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { toastr } from 'react-redux-toastr';
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
import { withStyles } from '@material-ui/core';
import { withTranslation } from 'react-i18next';
import WifiIcon from '@material-ui/icons/Wifi';
import WifiOffIcon from '@material-ui/icons/WifiOff';
import { MuiThemeProvider } from '@material-ui/core/styles';
import Home from './Home';
import VisitSpace from './components/VisitSpace';
import SpacesNearby from './components/SpacesNearby';
import Settings from './components/Settings';
import LoadSpace from './components/LoadSpace';
import SpaceScreen from './components/space/SpaceScreen';
import SyncScreen from './components/space/SyncScreen';
import ExportSelectionScreen from './components/space/export/ExportSelectionScreen';
import LoadSelectionScreen from './components/space/load/LoadSelectionScreen';
import DeveloperScreen from './components/developer/DeveloperScreen';
import Dashboard from './components/dashboard/Dashboard';
import SignInScreen from './components/signin/SignInScreen';
import Authorization from './components/Authorization';
import Classrooms from './components/classrooms/Classrooms';
import ImportDataScreen from './components/classrooms/ImportDataScreen';
import { OnlineTheme, OfflineTheme } from './themes';
import {
SETTINGS_PATH,
SYNC_SPACE_PATH,
SPACE_PATH,
HOME_PATH,
SPACES_NEARBY_PATH,
VISIT_PATH,
LOAD_SPACE_PATH,
DEVELOPER_PATH,
DASHBOARD_PATH,
SIGN_IN_PATH,
SAVED_SPACES_PATH,
buildExportSelectionPathForSpaceId,
LOAD_SELECTION_SPACE_PATH,
CLASSROOMS_PATH,
buildClassroomPath,
buildImportDataInClassroomPath,
} from './config/paths';
import {
getGeolocation,
getUserFolder,
getLanguage,
getDeveloperMode,
getGeolocationEnabled,
isAuthenticated,
} from './actions';
import { DEFAULT_LANGUAGE, USER_MODES } from './config/constants';
import {
CONNECTION_MESSAGE_HEADER,
CONNECTION_OFFLINE_MESSAGE,
CONNECTION_ONLINE_MESSAGE,
} from './config/messages';
import './App.css';
import SavedSpaces from './components/SavedSpaces';
import ClassroomScreen from './components/classrooms/ClassroomScreen';
const styles = () => ({
toastrIcon: { marginBottom: '-20px', fontSize: '45px' },
});
export class App extends Component {
state = { height: 0 };
static propTypes = {
dispatchGetGeolocation: PropTypes.func.isRequired,
dispatchGetUserFolder: PropTypes.func.isRequired,
dispatchGetLanguage: PropTypes.func.isRequired,
dispatchGetDeveloperMode: PropTypes.func.isRequired,
dispatchGetGeolocationEnabled: PropTypes.func.isRequired,
dispatchIsAuthenticated: PropTypes.func.isRequired,
lang: PropTypes.string,
i18n: PropTypes.shape({
changeLanguage: PropTypes.func.isRequired,
}).isRequired,
geolocationEnabled: PropTypes.bool.isRequired,
classes: PropTypes.shape({
toastrIcon: PropTypes.string.isRequired,
}).isRequired,
connexionStatus: PropTypes.bool.isRequired,
userMode: PropTypes.oneOf(USER_MODES).isRequired,
t: PropTypes.func.isRequired,
};
static defaultProps = {
lang: DEFAULT_LANGUAGE,
};
constructor(props) {
super(props);
const {
dispatchGetUserFolder,
dispatchGetLanguage,
dispatchGetDeveloperMode,
dispatchGetGeolocationEnabled,
dispatchIsAuthenticated,
} = this.props;
dispatchIsAuthenticated();
dispatchGetLanguage();
dispatchGetDeveloperMode();
dispatchGetUserFolder();
dispatchGetGeolocationEnabled();
}
componentDidMount() {
this.updateWindowDimensions();
window.addEventListener('resize', this.updateWindowDimensions);
}
componentDidUpdate({
lang: prevLang,
geolocationEnabled: prevGeolocationEnabled,
dispatchGetGeolocation,
connexionStatus: prevConnexionStatus,
}) {
const { lang, i18n, geolocationEnabled, connexionStatus } = this.props;
if (lang !== prevLang) {
i18n.changeLanguage(lang);
}
// fetch geolocation only if enabled
if (geolocationEnabled && geolocationEnabled !== prevGeolocationEnabled) {
dispatchGetGeolocation();
}
// display toastr when connexion status changes
if (connexionStatus !== prevConnexionStatus) {
this.triggerConnectionToastr();
}
}
componentWillUnmount() {
window.removeEventListener('resize', this.updateWindowDimensions);
}
updateWindowDimensions = () => {
this.setState({ height: window.innerHeight });
};
triggerConnectionToastr = () => {
const { classes, connexionStatus, t } = this.props;
if (connexionStatus) {
toastr.light(t(CONNECTION_MESSAGE_HEADER), t(CONNECTION_ONLINE_MESSAGE), {
icon: <WifiIcon className={classes.toastrIcon} />,
});
} else {
toastr.light(
t(CONNECTION_MESSAGE_HEADER),
t(CONNECTION_OFFLINE_MESSAGE),
{
icon: <WifiOffIcon className={classes.toastrIcon} />,
}
);
}
};
render() {
const { height } = this.state;
const { userMode, connexionStatus } = this.props;
return (
<MuiThemeProvider
theme={connexionStatus ? OnlineTheme(userMode) : OfflineTheme}
>
<Router>
<div className="app" style={{ height }}>
<Switch>
<Route exact path={SIGN_IN_PATH} component={SignInScreen} />
<Route exact path={HOME_PATH} component={Authorization()(Home)} />
<Route
exact
path={SAVED_SPACES_PATH}
component={Authorization()(SavedSpaces)}
/>
<Route
exact
path={SPACES_NEARBY_PATH}
component={Authorization()(SpacesNearby)}
/>
<Route
exact
path={VISIT_PATH}
component={Authorization()(VisitSpace)}
/>
<Route
exact
path={LOAD_SPACE_PATH}
component={Authorization()(LoadSpace)}
/>
<Route
exact
path={LOAD_SELECTION_SPACE_PATH}
component={Authorization()(LoadSelectionScreen)}
/>
<Route exact path={SETTINGS_PATH} component={Settings} />
<Route
exact
path={SYNC_SPACE_PATH}
component={Authorization()(SyncScreen)}
/>
<Route
exact
path={buildExportSelectionPathForSpaceId()}
component={Authorization()(ExportSelectionScreen)}
/>
<Route
exact
path={SPACE_PATH}
component={Authorization()(SpaceScreen)}
/>
<Route
exact
path={DASHBOARD_PATH}
component={Authorization()(Dashboard)}
/>
<Route
exact
path={buildClassroomPath()}
component={Authorization([USER_MODES.TEACHER])(ClassroomScreen)}
/>
<Route
exact
path={CLASSROOMS_PATH}
component={Authorization([USER_MODES.TEACHER])(Classrooms)}
/>
<Route
exact
path={buildImportDataInClassroomPath()}
component={Authorization([USER_MODES.TEACHER])(
ImportDataScreen
)}
/>
<Route
exact
path={DEVELOPER_PATH}
component={Authorization()(DeveloperScreen)}
/>
</Switch>
</div>
</Router>
</MuiThemeProvider>
);
}
}
const mapStateToProps = ({ authentication }) => ({
lang: authentication.getIn(['user', 'settings', 'lang']),
geolocationEnabled: authentication.getIn([
'user',
'settings',
'geolocationEnabled',
]),
userMode: authentication.getIn(['user', 'settings', 'userMode']),
});
const mapDispatchToProps = {
dispatchGetGeolocation: getGeolocation,
dispatchGetUserFolder: getUserFolder,
dispatchGetLanguage: getLanguage,
dispatchGetDeveloperMode: getDeveloperMode,
dispatchGetGeolocationEnabled: getGeolocationEnabled,
dispatchIsAuthenticated: isAuthenticated,
};
const ConnectedApp = connect(mapStateToProps, mapDispatchToProps)(App);
const StyledApp = withStyles(styles, { withTheme: true })(ConnectedApp);
const TranslatedApp = withTranslation()(StyledApp);
export default TranslatedApp;