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';
18 } from '../../helpers/Tournament';
20 const Tournament = () => {
21 const params = useParams();
22 const { id } = params;
24 const [error, setError] = useState(null);
25 const [loading, setLoading] = useState(true);
26 const [tournament, setTournament] = useState(null);
29 const ctrl = new AbortController();
32 .get(`/api/tournaments/${id}`, { signal: ctrl.signal })
36 setTournament(sortParticipants(response.data));
37 window.document.title = response.data.title;
50 window.Echo.channel(`Tournament.${id}`)
51 .listen('ApplicationAdded', e => {
53 setTournament(tournament => patchApplication(tournament, e.application));
56 .listen('ApplicationChanged', e => {
58 setTournament(tournament => patchApplication(tournament, e.application));
61 .listen('ApplicationRemoved', e => {
62 if (e.application_id) {
63 setTournament(tournament => removeApplication(tournament, e.application_id));
66 .listen('ParticipantChanged', e => {
69 setTournament(tournament => patchParticipant(tournament, e.participant));
72 .listen('ResultChanged', e => {
74 setTournament(tournament => patchResult(tournament, e.result));
77 .listen('RoundAdded', e => {
79 setTournament(tournament => ({
81 rounds: [e.round, ...tournament.rounds],
85 .listen('RoundChanged', e => {
87 setTournament(tournament => patchRound(tournament, e.round));
90 .listen('TournamentChanged', e => {
92 setTournament(tournament => ({ ...tournament, ...e.tournament }));
96 window.Echo.leave(`Tournament.${id}`);
103 setTournament(tournament => patchUser(tournament, e.user));
106 window.Echo.channel('App.Control')
107 .listen('UserChanged', cb);
109 window.Echo.channel('App.Control')
110 .stopListening('UserChanged', cb);
119 return <ErrorMessage error={error} />;
126 const addRound = async () => {
127 await axios.post('/api/rounds', { tournament_id: tournament.id });
130 return <ErrorBoundary>
131 <Detail addRound={addRound} tournament={tournament} />
135 export default Tournament;