frontend/src/App.js

83 lines
2.1 KiB
JavaScript
Raw Normal View History

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
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() {
const [isAuthenticated, setIsAuthenticated] = useState(!!existingToken);
const [currentUser, setCurrentUser] = useState(existingUser || '');
const [possibleDates, setPossibleDates] = useState();
// const [currentMeeting, setCurrentMeeting] = useState();
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);
};
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 (
<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} />
<PrivateRoute
path='/schedule'
component={Schedule}
possibleDates={possibleDates}
setPossibleDates={setPossibleDates}
/>
2020-08-21 09:51:52 +00:00
<PrivateRoute path='/invite' component={Invite} />
<PrivateRoute
path='/availability'
component={Availability}
/>
<PrivateRoute path='/schedule' component={Schedule} />
</Switch>
</Router>
</AuthContext.Provider>
2020-08-19 18:04:52 +00:00
);
}