2020-10-21 01:52:09 +02:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-05-01 06:24:23 +02:00
|
|
|
import { Switch, Route, useHistory, useLocation, Redirect } from 'react-router-dom';
|
2020-04-10 04:40:22 +02:00
|
|
|
import * as H from 'history';
|
|
|
|
|
2020-04-10 18:31:29 +02:00
|
|
|
import Dashboard from 'Dashboard';
|
2020-06-23 22:20:53 +02:00
|
|
|
import Admin from 'Admin';
|
2020-12-31 03:56:59 +01:00
|
|
|
import MyTasks from 'MyTasks';
|
2020-10-21 01:52:09 +02:00
|
|
|
import Confirm from 'Confirm';
|
2020-04-10 04:40:22 +02:00
|
|
|
import Projects from 'Projects';
|
|
|
|
import Project from 'Projects/Project';
|
2020-06-23 22:20:53 +02:00
|
|
|
import Teams from 'Teams';
|
2020-04-10 04:40:22 +02:00
|
|
|
import Login from 'Auth';
|
2020-10-21 01:52:09 +02:00
|
|
|
import Register from 'Register';
|
2020-06-13 00:21:58 +02:00
|
|
|
import Profile from 'Profile';
|
|
|
|
import styled from 'styled-components';
|
2020-10-21 01:52:09 +02:00
|
|
|
import { useCurrentUser } from 'App/context';
|
2020-04-10 04:40:22 +02:00
|
|
|
|
2020-06-13 00:21:58 +02:00
|
|
|
const MainContent = styled.div`
|
2020-06-23 22:20:53 +02:00
|
|
|
padding: 0 0 0 0;
|
2020-06-13 00:21:58 +02:00
|
|
|
background: #262c49;
|
|
|
|
height: 100%;
|
2020-06-19 01:12:15 +02:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
flex-grow: 1;
|
2020-06-13 00:21:58 +02:00
|
|
|
`;
|
2020-08-23 19:27:56 +02:00
|
|
|
|
2021-04-29 04:32:19 +02:00
|
|
|
type ValidateTokenResponse = {
|
|
|
|
valid: boolean;
|
|
|
|
userID: string;
|
2020-12-19 03:34:35 +01:00
|
|
|
};
|
|
|
|
|
2021-05-01 06:24:23 +02:00
|
|
|
const UserRequiredRoute: React.FC<any> = ({ children }) => {
|
|
|
|
const { user } = useCurrentUser();
|
|
|
|
const location = useLocation();
|
2021-05-03 00:31:24 +02:00
|
|
|
console.log('user required', user);
|
2021-05-01 06:24:23 +02:00
|
|
|
if (user) {
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Redirect
|
|
|
|
to={{
|
|
|
|
pathname: '/login',
|
|
|
|
state: { redirect: location.pathname },
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-05-03 00:31:24 +02:00
|
|
|
const Routes: React.FC = () => {
|
2020-10-21 01:52:09 +02:00
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const { setUser } = useCurrentUser();
|
|
|
|
useEffect(() => {
|
2021-04-29 04:32:19 +02:00
|
|
|
fetch('/auth/validate', {
|
2020-10-21 01:52:09 +02:00
|
|
|
method: 'POST',
|
|
|
|
credentials: 'include',
|
2021-05-03 00:31:24 +02:00
|
|
|
}).then(async (x) => {
|
2021-04-29 04:32:19 +02:00
|
|
|
const response: ValidateTokenResponse = await x.json();
|
|
|
|
const { valid, userID } = response;
|
2021-05-01 05:55:37 +02:00
|
|
|
if (valid) {
|
2021-04-29 04:32:19 +02:00
|
|
|
setUser(userID);
|
2020-10-21 01:52:09 +02:00
|
|
|
}
|
|
|
|
setLoading(false);
|
|
|
|
});
|
|
|
|
}, []);
|
2021-05-03 00:31:24 +02:00
|
|
|
console.log('loading', loading);
|
2021-05-01 06:24:23 +02:00
|
|
|
if (loading) return null;
|
|
|
|
return (
|
2020-10-21 01:52:09 +02:00
|
|
|
<Switch>
|
2021-05-01 06:24:23 +02:00
|
|
|
<Route exact path="/login" component={Login} />
|
|
|
|
<Route exact path="/register" component={Register} />
|
|
|
|
<Route exact path="/confirm" component={Confirm} />
|
|
|
|
<Switch>
|
|
|
|
<MainContent>
|
|
|
|
<Route path="/projects/:projectID" component={Project} />
|
|
|
|
|
|
|
|
<UserRequiredRoute>
|
|
|
|
<Route exact path="/" component={Dashboard} />
|
|
|
|
<Route exact path="/projects" component={Projects} />
|
|
|
|
<Route path="/teams/:teamID" component={Teams} />
|
|
|
|
<Route path="/profile" component={Profile} />
|
|
|
|
<Route path="/admin" component={Admin} />
|
|
|
|
<Route path="/tasks" component={MyTasks} />
|
|
|
|
</UserRequiredRoute>
|
|
|
|
</MainContent>
|
|
|
|
</Switch>
|
2020-10-21 01:52:09 +02:00
|
|
|
</Switch>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
export default Routes;
|