aboutsummaryrefslogtreecommitdiff
path: root/src/components/TimerPage/TimerPage.js
blob: f304b460dc926d8df21975f993817b0d1cd004c3 (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
import React from 'react';

import { Grid, Box } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";

import Timer from "../Timer/Timer";
import SolutionCard from "../SolutionCard/SolutionCard";

import { post } from '../../requests';


const useStyles = makeStyles(theme => ({
  root: {
    display: 'flex',
    justifyContent: 'space-between',
    padding: theme.spacing(5, 4, 4, 4),
  },
}));

const TimerPage = ({ recentSolutions, setRecentSolutions }) => {
  const classes = useStyles();

  const user = {
    id: null,
    username: 'anonymous',
  };

  const registerResult = result => {
    const solution = { author_id: user.id, result };
    post('solutions/', solution).then(response => {
      setRecentSolutions([response.data].concat(recentSolutions));
    });
  };

  const removeSolution = (id) => {
    setRecentSolutions(recentSolutions.filter((solution => solution.id !== id)));
  };

  return (
    <Box className={classes.root}>
      <Grid container direction="row" justify="space-around" spacing={8}>
        <Grid item xs={6}>
          <Timer registerResult={registerResult} style={{ position: 'fixed' }}/>
        </Grid>
        <Grid item xs={4} container direction="column" spacing={5}>
          {recentSolutions.slice(0, 3).map(solution => (
            <Grid item key={solution.id}>
              <SolutionCard data={solution} removeThisCard={removeSolution}/>
            </Grid>
          ))}
        </Grid>
      </Grid>
    </Box>
  );
};


export default TimerPage;