1 import axios from 'axios';
2 import React, { useEffect, useState } from 'react';
3 import { useParams } from 'react-router-dom';
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';
17 } from '../../helpers/Tournament';
19 const Tournament = () => {
20 const params = useParams();
21 const { id } = params;
23 const [error, setError] = useState(null);
24 const [loading, setLoading] = useState(true);
25 const [tournament, setTournament] = useState(null);
30 .get(`/api/tournaments/${id}`)
34 setTournament(sortParticipants(response.data));
35 window.document.title = response.data.title;
45 window.Echo.channel(`Tournament.${id}`)
46 .listen('ApplicationAdded', e => {
48 setTournament(tournament => patchApplication(tournament, e.application));
51 .listen('ApplicationChanged', e => {
53 setTournament(tournament => patchApplication(tournament, e.application));
56 .listen('ParticipantChanged', e => {
58 setTournament(tournament => patchParticipant(tournament, e.participant));
61 .listen('ResultChanged', e => {
63 setTournament(tournament => patchResult(tournament, e.result));
66 .listen('RoundAdded', e => {
68 setTournament(tournament => ({
70 rounds: [e.round, ...tournament.rounds],
74 .listen('RoundChanged', e => {
76 setTournament(tournament => patchRound(tournament, e.round));
79 .listen('TournamentChanged', e => {
81 setTournament(tournament => ({ ...tournament, ...e.tournament }));
85 window.Echo.leave(`Tournament.${id}`);
92 setTournament(tournament => patchUser(tournament, e.user));
95 window.Echo.channel('App.Control')
96 .listen('UserChanged', cb);
98 window.Echo.channel('App.Control')
99 .stopListening('UserChanged', cb);
108 return <ErrorMessage error={error} />;
115 const addRound = async () => {
116 await axios.post('/api/rounds', { tournament_id: tournament.id });
119 return <ErrorBoundary>
120 <Detail addRound={addRound} tournament={tournament} />
124 export default Tournament;