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