]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/pages/Tournament.js
729e55d138d856d2da8491a0a20f177831d05cee
[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 {
11         patchApplication,
12         patchParticipant,
13         patchResult,
14         patchRound,
15         patchUser,
16         sortParticipants,
17 } from '../../helpers/Tournament';
18
19 const Tournament = () => {
20         const params = useParams();
21         const { id } = params;
22
23         const [error, setError] = useState(null);
24         const [loading, setLoading] = useState(true);
25         const [tournament, setTournament] = useState(null);
26
27         useEffect(() => {
28                 setLoading(true);
29                 axios
30                         .get(`/api/tournaments/${id}`)
31                         .then(response => {
32                                 setError(null);
33                                 setLoading(false);
34                                 setTournament(sortParticipants(response.data));
35                                 window.document.title = response.data.title;
36                         })
37                         .catch(error => {
38                                 setError(error);
39                                 setLoading(false);
40                                 setTournament(null);
41                         });
42         }, [id]);
43
44         useEffect(() => {
45                 window.Echo.channel(`Tournament.${id}`)
46                         .listen('ApplicationAdded', e => {
47                                 if (e.application) {
48                                         setTournament(tournament => patchApplication(tournament, e.application));
49                                 }
50                         })
51                         .listen('ApplicationChanged', e => {
52                                 if (e.application) {
53                                         setTournament(tournament => patchApplication(tournament, e.application));
54                                 }
55                         })
56                         .listen('ParticipantChanged', e => {
57                                 if (e.participant) {
58                                         setTournament(tournament => patchParticipant(tournament, e.participant));
59                                 }
60                         })
61                         .listen('ResultChanged', e => {
62                                 if (e.result) {
63                                         setTournament(tournament => patchResult(tournament, e.result));
64                                 }
65                         })
66                         .listen('RoundAdded', e => {
67                                 if (e.round) {
68                                         setTournament(tournament => ({
69                                                 ...tournament,
70                                                 rounds: [e.round, ...tournament.rounds],
71                                         }));
72                                 }
73                         })
74                         .listen('RoundChanged', e => {
75                                 if (e.round) {
76                                         setTournament(tournament => patchRound(tournament, e.round));
77                                 }
78                         })
79                         .listen('TournamentChanged', e => {
80                                 if (e.tournament) {
81                                         setTournament(tournament => ({ ...tournament, ...e.tournament }));
82                                 }
83                         });
84                 return () => {
85                         window.Echo.leave(`Tournament.${id}`);
86                 };
87         }, [id]);
88
89         useEffect(() => {
90                 const cb = (e) => {
91                         if (e.user) {
92                                 setTournament(tournament => patchUser(tournament, e.user));
93                         }
94                 };
95                 window.Echo.channel('App.Control')
96                         .listen('UserChanged', cb);
97                 return () => {
98                         window.Echo.channel('App.Control')
99                                 .stopListening('UserChanged', cb);
100                 };
101         }, []);
102
103         if (loading) {
104                 return <Loading />;
105         }
106
107         if (error) {
108                 return <ErrorMessage error={error} />;
109         }
110
111         if (!tournament) {
112                 return <NotFound />;
113         }
114
115         const addRound = async () => {
116                 await axios.post('/api/rounds', { tournament_id: tournament.id });
117         };
118
119         return <ErrorBoundary>
120                 <Detail addRound={addRound} tournament={tournament} />
121         </ErrorBoundary>;
122 };
123
124 export default Tournament;