aboutsummaryrefslogtreecommitdiff
path: root/src/index.tsx
blob: eb43b7eb44eda554a62934a60b431f60e475b424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';

import {
  BenzinThemeProvider,
  Header,
} from 'react-benzin';

import 'typeface-roboto';

import Timer from './pages/Timer/Timer';
import Scoreboard from './pages/Scoreboard/Scoreboard';
import Contribute from './pages/Contribute/Contribute';
import Profile from './pages/Profile/Profile';

import TimerIcon from '@material-ui/icons/Timer';
import AccountCircleIcon from '@material-ui/icons/AccountCircle';
import AssignmentIcon from '@material-ui/icons/Assignment';
import GitHubIcon from '@material-ui/icons/GitHub';

import { get } from './requests';

interface User {
  username: string;
  id: number | null;
}

const App: React.FC = () => {
  const [page, setPage] = useState<string>('app');
  const [user, setUser] = useState<User>({ username: 'anonymous', id: null });
  const [recentSolutions, setRecentSolutions] = useState([]);

  const headerContents = {
    app: (<TimerIcon />),
    profile: (<AccountCircleIcon />),
    scoreboard: (<AssignmentIcon />),
    contribute: (<GitHubIcon />),
  };

  useEffect(() => {
    const userId = localStorage.getItem('userId');
    if (userId) {
      get('users/').then(response => {
        setUser(response.data.filter((user: User) => user.id === +userId)[0]);
      });
    }
  }, []);

  const Page: React.FC<{ page: string }> = ({ page }) => {
    switch (page) {
      case 'app':
        return (
          <Timer
            user={user}
            recentSolutions={recentSolutions}
            setRecentSolutions={setRecentSolutions}
            setPage={setPage}
          />
        );

      case 'profile':
        return <Profile user={user} setUser={setUser} />;

      case 'scoreboard':
        return <Scoreboard />;

      case 'contribute':
        return <Contribute />;

      default:
        return <Contribute />;
    }
  };

  return (
    <BenzinThemeProvider>
      <Header
        logo={{
          title: 'ChronoCube',
          icon: null
        }}
        contents={headerContents}
        page={page}
        setPage={setPage}/>
      <Page page={page} />
    </BenzinThemeProvider>
  );
};

document.body.style.overflow = 'hidden';
ReactDOM.render(<App />, document.getElementById('root'));