aboutsummaryrefslogtreecommitdiff
path: root/src/index.tsx
blob: ce6a9d4089fe63f22685d971c91ac309591bb62f (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
import React from 'react';
import ReactDOM from 'react-dom';
import './style.css';
import './fonts/Bitter-Regular.woff';

import useLocalStorage from './hooks/useLocalStorage';
import Timer from './components/Timer';

interface Solution {
  result: string;
  timestamp: string;
  author?: string;
}

const App: React.FC = () => {
  const [author, setAuthor] = useLocalStorage<string>('author', 'anonymous');
  const [solutions, setSolutions] = useLocalStorage<Solution[]>('solutions', []);

  const registerResult = (result: string) => {
    setSolutions([{ author, result, timestamp: new Date().toISOString() }, ...solutions]);
  }

  const removeSolution = (index: number) => {
    setSolutions(solutions.filter((_, i) => i !== index));
  }

  return (
    <>
      <h2>Timer</h2>
      <Timer registerResult={registerResult} />

      <h2>Results</h2>
      <table>
        <tr>
          <th>Author</th>
          <th>Result</th>
          <th>Timestamp</th>
          <th>Actions</th>
        </tr>
        {solutions.map((solution, index) => (
          <tr key={solution.timestamp}>
            <td>{solution.author}</td>
            <td>{solution.result}</td>
            <td>{new Date(solution.timestamp).toLocaleString()}</td>
            <td><button onClick={() => removeSolution(index)}>remove</button></td>
          </tr>
        ))}
      </table>
    </>
  );
};

ReactDOM.render(<App />, document.getElementById('root'));