]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/pages/Tournament.js
cdafd69b12c2414140f3dce96d07c87cd2e68bf2
[alttp.git] / resources / js / components / pages / Tournament.js
1 import axios from 'axios';
2 import React, { useEffect, useState } from 'react';
3 import { useParams } from 'react-router-dom';
4
5 import ErrorBoundary from '../common/ErrorBoundary';
6 import ErrorMessage from '../common/ErrorMessage';
7 import Loading from '../common/Loading';
8 import NotFound from '../pages/NotFound';
9 import Detail from '../tournament/Detail';
10
11 const Tournament = () => {
12         const params = useParams();
13         const { id } = params;
14
15         const [error, setError] = useState(null);
16         const [loading, setLoading] = useState(true);
17         const [tournament, setTournament] = useState(null);
18
19         useEffect(() => {
20                 setLoading(true);
21                 axios
22                         .get(`/api/tournaments/${id}`)
23                         .then(response => {
24                                 setError(null);
25                                 setLoading(false);
26                                 setTournament(response.data);
27                         })
28                         .catch(error => {
29                                 setError(error);
30                                 setLoading(false);
31                                 setTournament(null);
32                         });
33         }, [id]);
34
35         if (loading) {
36                 return <Loading />;
37         }
38
39         if (error) {
40                 return <ErrorMessage error={error} />;
41         }
42
43         if (!tournament) {
44                 return <NotFound />;
45         }
46
47         const addRound = async () => {
48                 await axios.post('/api/rounds', { tournament_id: tournament.id });
49         };
50
51         return <ErrorBoundary>
52                 <Detail addRound={addRound} tournament={tournament} />
53         </ErrorBoundary>;
54 };
55
56 export default Tournament;