]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/pages/Tournament.js
90f7d038c3718612d5051dcf9453abbb79e14fe8
[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, patchRound, 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                                 if (e.result) {
40                                         setTournament(tournament => patchResult(tournament, e.result));
41                                 }
42                         })
43                         .listen('RoundAdded', e => {
44                                 if (e.round) {
45                                         setTournament(tournament => ({
46                                                 ...tournament,
47                                                 rounds: [...tournament.rounds, e.round],
48                                         }));
49                                 }
50                         })
51                         .listen('RoundChanged', e => {
52                                 if (e.round) {
53                                         setTournament(tournament => patchRound(tournament, e.round));
54                                 }
55                         });
56                 return () => {
57                         window.Echo.leave(`Tournament.${id}`);
58                 };
59         }, [id]);
60
61         if (loading) {
62                 return <Loading />;
63         }
64
65         if (error) {
66                 return <ErrorMessage error={error} />;
67         }
68
69         if (!tournament) {
70                 return <NotFound />;
71         }
72
73         const addRound = async () => {
74                 await axios.post('/api/rounds', { tournament_id: tournament.id });
75         };
76
77         return <ErrorBoundary>
78                 <Detail addRound={addRound} tournament={tournament} />
79         </ErrorBoundary>;
80 };
81
82 export default Tournament;