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