1 import axios from 'axios';
2 import React, { useEffect, useState } from 'react';
3 import { Helmet } from 'react-helmet';
4 import { useParams } from 'react-router-dom';
6 import CanonicalLinks from '../components/common/CanonicalLinks';
7 import ErrorBoundary from '../components/common/ErrorBoundary';
8 import ErrorMessage from '../components/common/ErrorMessage';
9 import Loading from '../components/common/Loading';
10 import NotFound from '../pages/NotFound';
11 import Detail from '../components/tournament/Detail';
20 } from '../helpers/Tournament';
22 const Tournament = () => {
23 const params = useParams();
24 const { id } = params;
26 const [error, setError] = useState(null);
27 const [loading, setLoading] = useState(true);
28 const [tournament, setTournament] = useState(null);
31 const ctrl = new AbortController();
34 .get(`/api/tournaments/${id}`, { signal: ctrl.signal })
38 setTournament(sortParticipants(response.data));
51 window.Echo.channel(`Tournament.${id}`)
52 .listen('ApplicationAdded', e => {
54 setTournament(tournament => patchApplication(tournament, e.application));
57 .listen('ApplicationChanged', e => {
59 setTournament(tournament => patchApplication(tournament, e.application));
62 .listen('ApplicationRemoved', e => {
63 if (e.application_id) {
64 setTournament(tournament => removeApplication(tournament, e.application_id));
67 .listen('ParticipantChanged', e => {
70 setTournament(tournament => patchParticipant(tournament, e.participant));
73 .listen('ResultChanged', e => {
75 setTournament(tournament => patchResult(tournament, e.result));
78 .listen('RoundAdded', e => {
80 setTournament(tournament => ({
82 rounds: [e.round, ...tournament.rounds],
86 .listen('RoundChanged', e => {
88 setTournament(tournament => patchRound(tournament, e.round));
91 .listen('TournamentChanged', e => {
93 setTournament(tournament => ({ ...tournament, ...e.tournament }));
97 window.Echo.leave(`Tournament.${id}`);
104 setTournament(tournament => patchUser(tournament, e.user));
107 window.Echo.channel('App.Control')
108 .listen('UserChanged', cb);
110 window.Echo.channel('App.Control')
111 .stopListening('UserChanged', cb);
120 return <ErrorMessage error={error} />;
127 const addRound = async () => {
128 await axios.post('/api/rounds', { tournament_id: tournament.id });
131 return <ErrorBoundary>
133 <title>{tournament.title}</title>
135 <CanonicalLinks base={`/tournaments/${tournament.id}`} />
136 <Detail addRound={addRound} tournament={tournament} />
140 export default Tournament;