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