2019-11-25 01:21:08 +07:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import styled from '@emotion/styled';
|
2019-02-03 17:23:33 +07:00
|
|
|
import isNil from 'lodash/isNil';
|
2019-11-25 01:21:08 +07:00
|
|
|
import { Router } from 'react-router-dom';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
import storage from '../utils/storage';
|
2019-11-25 01:21:08 +07:00
|
|
|
import { isTokenExpire } from '../utils/login';
|
2019-06-20 19:37:28 +07:00
|
|
|
import API from '../utils/api';
|
2019-11-25 01:21:08 +07:00
|
|
|
import Header from '../components/Header';
|
2019-06-20 19:37:28 +07:00
|
|
|
import Footer from '../components/Footer';
|
2019-11-25 01:21:08 +07:00
|
|
|
import Box from '../muiComponents/Box';
|
|
|
|
import Loading from '../components/Loading';
|
|
|
|
import StyleBaseline from '../design-tokens/StyleBaseline';
|
|
|
|
import { breakpoints } from '../utils/styles/media';
|
|
|
|
|
|
|
|
import AppContextProvider from './AppContextProvider';
|
|
|
|
import AppRoute, { history } from './AppRoute';
|
|
|
|
|
|
|
|
const StyledBoxContent = styled(Box)({
|
|
|
|
padding: 15,
|
|
|
|
[`@media screen and (min-width: ${breakpoints.container}px)`]: {
|
|
|
|
maxWidth: breakpoints.container,
|
|
|
|
width: '100%',
|
|
|
|
marginLeft: 'auto',
|
|
|
|
marginRight: 'auto',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/* eslint-disable react/jsx-max-depth */
|
|
|
|
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
|
|
/* eslint-disable react/jsx-no-bind */
|
|
|
|
/* eslint-disable react-hooks/exhaustive-deps */
|
|
|
|
const App: React.FC = () => {
|
|
|
|
const [user, setUser] = useState();
|
|
|
|
const [packages, setPackages] = useState([]);
|
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
2019-10-19 15:49:04 +07:00
|
|
|
|
2019-11-25 01:21:08 +07:00
|
|
|
/**
|
|
|
|
* Logouts user
|
|
|
|
* Required by: <Header />
|
|
|
|
*/
|
|
|
|
const logout = () => {
|
|
|
|
storage.removeItem('username');
|
|
|
|
storage.removeItem('token');
|
|
|
|
setUser(undefined);
|
2019-02-03 17:23:33 +07:00
|
|
|
};
|
|
|
|
|
2019-11-25 01:21:08 +07:00
|
|
|
const checkUserAlreadyLoggedIn = () => {
|
2019-02-03 17:23:33 +07:00
|
|
|
// checks for token validity
|
|
|
|
const token = storage.getItem('token');
|
2019-11-25 01:21:08 +07:00
|
|
|
const username = storage.getItem('username');
|
|
|
|
|
2019-02-03 17:23:33 +07:00
|
|
|
if (isTokenExpire(token) || isNil(username)) {
|
2019-11-25 01:21:08 +07:00
|
|
|
logout();
|
|
|
|
return;
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
2019-11-25 01:21:08 +07:00
|
|
|
|
|
|
|
setUser({ username });
|
2019-02-03 17:23:33 +07:00
|
|
|
};
|
|
|
|
|
2019-11-25 01:21:08 +07:00
|
|
|
const loadOnHandler = async () => {
|
2019-02-03 17:23:33 +07:00
|
|
|
try {
|
2019-11-25 01:21:08 +07:00
|
|
|
const packages = await API.request('packages', 'GET');
|
|
|
|
// FIXME add correct type for package
|
|
|
|
setPackages(packages as never[]);
|
2019-02-03 17:23:33 +07:00
|
|
|
} catch (error) {
|
|
|
|
// FIXME: add dialog
|
|
|
|
console.error({
|
|
|
|
title: 'Warning',
|
|
|
|
message: `Unable to load package list: ${error.message}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-25 01:21:08 +07:00
|
|
|
setIsLoading(false);
|
2019-02-03 17:23:33 +07:00
|
|
|
};
|
|
|
|
|
2019-11-25 01:21:08 +07:00
|
|
|
useEffect(() => {
|
|
|
|
checkUserAlreadyLoggedIn();
|
|
|
|
loadOnHandler();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<StyleBaseline />
|
|
|
|
<Box display="flex" flexDirection="column" height="100%">
|
|
|
|
{isLoading ? (
|
|
|
|
<Loading />
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Router history={history}>
|
|
|
|
<AppContextProvider packages={packages} user={user}>
|
|
|
|
<Header />
|
|
|
|
<StyledBoxContent flexGrow={1}>
|
|
|
|
<AppRoute />
|
|
|
|
</StyledBoxContent>
|
|
|
|
</AppContextProvider>
|
|
|
|
</Router>
|
|
|
|
<Footer />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|