]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/pages/Tournament.js
aab442346149865aa01f5bc47185a56f6200eede
[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 import { patchResult, sortParticipants } from '../../helpers/Tournament';
11
12 const Tournament = () => {
13         const params = useParams();
14         const { id } = params;
15
16         const [error, setError] = useState(null);
17         const [loading, setLoading] = useState(true);
18         const [tournament, setTournament] = useState(null);
19
20         useEffect(() => {
21                 setLoading(true);
22                 axios
23                         .get(`/api/tournaments/${id}`)
24                         .then(response => {
25                                 setError(null);
26                                 setLoading(false);
27                                 setTournament(sortParticipants(response.data));
28                         })
29                         .catch(error => {
30                                 setError(error);
31                                 setLoading(false);
32                                 setTournament(null);
33                         });
34         }, [id]);
35
36         useEffect(() => {
37                 window.Echo.private(`Tournament.${id}`)
38                         .listen('ResultReported', e => {
39                                 console.log(e);
40                                 if (e.result) {
41                                         setTournament(tournament => patchResult(tournament, e.result));
42                                 }
43                         })
44                         .listen('RoundAdded', e => {
45                                 if (e.round) {
46                                         setTournament(tournament => ({
47                                                 ...tournament,
48                                                 rounds: [...tournament.rounds, e.round],
49                                         }));
50                                 }
51                         });
52                 return () => {
53                         window.Echo.leave(`Tournament.${id}`);
54                 };
55         }, [id]);
56
57         if (loading) {
58                 return <Loading />;
59         }
60
61         if (error) {
62                 return <ErrorMessage error={error} />;
63         }
64
65         if (!tournament) {
66                 return <NotFound />;
67         }
68
69         const addRound = async () => {
70                 await axios.post('/api/rounds', { tournament_id: tournament.id });
71         };
72
73         return <ErrorBoundary>
74                 <Detail addRound={addRound} tournament={tournament} />
75         </ErrorBoundary>;
76 };
77
78 export default Tournament;