2020-08-21 09:51:52 +00:00
|
|
|
import React, { useState } from 'react';
|
2020-08-19 18:04:52 +00:00
|
|
|
import 'rsuite/lib/styles/index.less';
|
|
|
|
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
|
|
|
|
|
2020-08-21 09:51:52 +00:00
|
|
|
import { AuthContext } from './helpers/authContext';
|
2020-08-21 23:08:18 +00:00
|
|
|
import { PrivateRoute } from './components';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Home,
|
|
|
|
Login,
|
|
|
|
Register,
|
|
|
|
Dashboard,
|
|
|
|
Invite,
|
|
|
|
Availability,
|
|
|
|
Schedule,
|
|
|
|
} from './screens';
|
2020-08-19 18:04:52 +00:00
|
|
|
|
2020-08-22 16:33:42 +00:00
|
|
|
const existingToken = JSON.parse(localStorage.getItem('token'));
|
|
|
|
const existingUser = JSON.parse(localStorage.getItem('user'));
|
2020-08-21 09:51:52 +00:00
|
|
|
|
2020-08-19 18:04:52 +00:00
|
|
|
export default function App() {
|
2020-08-22 16:33:42 +00:00
|
|
|
const [isAuthenticated, setIsAuthenticated] = useState(!!existingToken);
|
|
|
|
const [currentUser, setCurrentUser] = useState(existingUser || '');
|
2020-08-22 20:49:03 +00:00
|
|
|
|
2020-08-22 16:33:42 +00:00
|
|
|
const [possibleDates, setPossibleDates] = useState();
|
2020-08-22 20:49:03 +00:00
|
|
|
const [currentMeeting, setCurrentMeeting] = useState({
|
|
|
|
title: '',
|
2020-08-27 13:51:23 +00:00
|
|
|
description: '', // optional
|
2020-08-27 14:31:09 +00:00
|
|
|
duration: 0,
|
2020-08-27 13:51:23 +00:00
|
|
|
status: 0, // Always 0 when meeting unconfirmed
|
2020-08-22 20:49:03 +00:00
|
|
|
});
|
2020-08-22 16:33:42 +00:00
|
|
|
|
2020-08-21 22:08:42 +00:00
|
|
|
const [authToken, setAuthToken] = useState(existingToken || '');
|
2020-08-21 09:51:52 +00:00
|
|
|
|
|
|
|
const setToken = (data) => {
|
|
|
|
localStorage.setItem('token', JSON.stringify(data));
|
|
|
|
setAuthToken(data);
|
|
|
|
};
|
|
|
|
|
2020-08-22 16:33:42 +00:00
|
|
|
const setUser = (data) => {
|
|
|
|
localStorage.setItem('user', JSON.stringify(data));
|
|
|
|
setCurrentUser(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setAuthentication = (boolean) => {
|
|
|
|
if (!boolean) {
|
|
|
|
localStorage.removeItem('token');
|
|
|
|
localStorage.removeItem('user');
|
|
|
|
}
|
|
|
|
setIsAuthenticated(boolean);
|
|
|
|
};
|
|
|
|
|
2020-08-19 18:04:52 +00:00
|
|
|
return (
|
2020-08-22 16:33:42 +00:00
|
|
|
<AuthContext.Provider
|
|
|
|
value={{
|
|
|
|
authToken,
|
|
|
|
setAuthToken: setToken,
|
|
|
|
isAuthenticated,
|
|
|
|
setIsAuthenticated: setAuthentication,
|
|
|
|
currentUser,
|
|
|
|
setCurrentUser: setUser,
|
|
|
|
}}
|
|
|
|
>
|
2020-08-21 09:51:52 +00:00
|
|
|
<Router>
|
|
|
|
<Switch>
|
|
|
|
<Route path='/' exact component={Home} />
|
|
|
|
<Route path='/login' component={Login} />
|
|
|
|
<Route path='/register' component={Register} />
|
|
|
|
|
|
|
|
<PrivateRoute path='/dashboard' component={Dashboard} />
|
2020-08-22 16:33:42 +00:00
|
|
|
<PrivateRoute
|
|
|
|
path='/schedule'
|
|
|
|
component={Schedule}
|
|
|
|
possibleDates={possibleDates}
|
|
|
|
setPossibleDates={setPossibleDates}
|
2020-08-22 20:49:03 +00:00
|
|
|
currentMeeting={currentMeeting}
|
|
|
|
setCurrentMeeting={setCurrentMeeting}
|
2020-08-27 14:31:09 +00:00
|
|
|
currentUser={currentUser}
|
2020-08-22 16:33:42 +00:00
|
|
|
/>
|
2020-08-21 09:51:52 +00:00
|
|
|
<PrivateRoute
|
|
|
|
path='/availability'
|
|
|
|
component={Availability}
|
2020-08-22 20:49:03 +00:00
|
|
|
possibleDates={possibleDates}
|
2020-08-23 14:30:25 +00:00
|
|
|
currentUser={currentUser}
|
2020-08-27 12:51:58 +00:00
|
|
|
currentMeeting={currentMeeting}
|
2020-08-21 09:51:52 +00:00
|
|
|
/>
|
2020-08-22 20:49:03 +00:00
|
|
|
<PrivateRoute path='/invite' component={Invite} />
|
2020-08-21 09:51:52 +00:00
|
|
|
</Switch>
|
|
|
|
</Router>
|
|
|
|
</AuthContext.Provider>
|
2020-08-19 18:04:52 +00:00
|
|
|
);
|
|
|
|
}
|