frontend/src/App.js

99 lines
2.5 KiB
JavaScript

import React, { useState } from 'react';
import 'rsuite/lib/styles/index.less';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import { AuthContext } from './helpers/authContext';
import { PrivateRoute } from './components';
import {
Home,
Login,
Register,
Dashboard,
Invite,
Availability,
Schedule,
} from './screens';
const existingToken = JSON.parse(localStorage.getItem('token'));
const existingUser = JSON.parse(localStorage.getItem('user'));
export default function App() {
const [isAuthenticated, setIsAuthenticated] = useState(!!existingToken);
const [currentUser, setCurrentUser] = useState(existingUser || '');
const [participant, setParticipant] = useState();
const [possibleDates, setPossibleDates] = useState([]);
const [currentMeeting, setCurrentMeeting] = useState({
title: '',
description: '', // optional
duration: 0,
status: 0, // Always 0 when meeting unconfirmed
});
const [authToken, setAuthToken] = useState(existingToken || '');
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);
};
return (
<AuthContext.Provider
value={{
authToken,
setAuthToken: setToken,
isAuthenticated,
setIsAuthenticated: setAuthentication,
currentUser,
setCurrentUser: setUser,
}}
>
<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}
currentMeeting={currentMeeting}
setCurrentMeeting={setCurrentMeeting}
currentUser={currentUser}
setParticipant={setParticipant}
/>
<PrivateRoute
path='/availability'
component={Availability}
possibleDates={possibleDates}
currentUser={currentUser}
currentMeeting={currentMeeting}
participant={participant}
/>
<PrivateRoute
path='/invite'
component={Invite}
currentMeeting={currentMeeting}
/>
</Switch>
</Router>
</AuthContext.Provider>
);
}