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