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