import React, { useState, useEffect } from 'react'; import ReactDOM from 'react-dom'; import { createMuiTheme, ThemeProvider, makeStyles } from '@material-ui/core/styles'; import { CssBaseline } from '@material-ui/core'; import teal from '@material-ui/core/colors/teal'; import 'typeface-roboto'; import { User } from 'which-types'; import Header from './components/Header/Header'; import ProfilePage from './pages/ProfilePage/ProfilePage'; import FeedPage from './pages/FeedPage/FeedPage'; import AuthPage from './pages/AuthPage/AuthPage'; import { Page } from './types'; import { get, post } from './requests'; import ScrollTopArrow from './components/ScrollTopArrow/ScrollTopArrow'; const theme = createMuiTheme({ palette: { primary: { main: teal[700], light: teal[100] } } }); const useStyles = makeStyles({ root: { width: theme.spacing(75), marginTop: theme.spacing(15), margin: '0 auto' } }); const App: React.FC = () => { const [user, setUser] = React.useState(); const [page, setPage] = useState({ prefix: 'feed', id: '' }); const classes = useStyles(); const navigate = (prefix: string, id?: string): void => { if (prefix === 'profile' && !id && !user) { setPage({ prefix: 'auth', id: '' }); } else { setPage({ prefix, id: (id || user?._id || '') }); } }; const logIn = (username: string, password: string, remember = true): Promise => { return post('/authentication', { strategy: 'local', username, password }).then(response => { const me = response.data.user; const token = response.data.accessToken; setUser(me); navigate('profile', me._id); localStorage.setItem('userId', me._id); localStorage.setItem('token', token); if (!remember) localStorage.setItem('shouldClear', 'true'); return true; }).catch(() => false); }; const logOut = (redirect = true) => { setUser(undefined); localStorage.removeItem('userId'); localStorage.removeItem('token'); if (redirect) navigate('auth'); }; useEffect(() => { if (localStorage.getItem('shouldClear')) logOut(false); const userId = localStorage.getItem('userId'); if (userId) { get(`/users/${userId}`).then(response => { setUser(response.data); }); } }, []); return (
{ page.prefix === 'profile' && } { page.prefix === 'feed' && } { page.prefix === 'auth' && }
); }; ReactDOM.render(, document.getElementById('root'));