]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/pages/Tournament.js
protocol frontend
[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
11 const Tournament = () => {
12         const params = useParams();
13         const { id } = params;
14
15         const [error, setError] = useState(null);
16         const [loading, setLoading] = useState(true);
17         const [tournament, setTournament] = useState(null);
18
19         useEffect(() => {
20                 setLoading(true);
21                 axios
22                         .get(`/api/tournaments/${id}`)
23                         .then(response => {
24                                 setError(null);
25                                 setLoading(false);
26                                 setTournament(response.data);
27                         })
28                         .catch(error => {
29                                 setError(error);
30                                 setLoading(false);
31                                 setTournament(null);
32                         });
33         }, [id]);
34
35         useEffect(() => {
36                 window.Echo.private(`Tournament.${id}`)
37                         .listen('RoundAdded', e => {
38                                 console.log(e);
39                                 if (e.round) {
40                                         setTournament(tournament => ({
41                                                 ...tournament,
42                                                 rounds: [...tournament.rounds, e.round],
43                                         }));
44                                 }
45                         });
46                 return () => {
47                         window.Echo.leave(`Tournament.${id}`);
48                 };
49         }, [id]);
50
51         if (loading) {
52                 return <Loading />;
53         }
54
55         if (error) {
56                 return <ErrorMessage error={error} />;
57         }
58
59         if (!tournament) {
60                 return <NotFound />;
61         }
62
63         const addRound = async () => {
64                 await axios.post('/api/rounds', { tournament_id: tournament.id });
65         };
66
67         return <ErrorBoundary>
68                 <Detail addRound={addRound} tournament={tournament} />
69         </ErrorBoundary>;
70 };
71
72 export default Tournament;