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';
22 } from '../helpers/Tournament';
24 export const Component = () => {
25 const params = useParams();
26 const { id } = params;
28 const [error, setError] = useState(null);
29 const [loading, setLoading] = useState(true);
30 const [tournament, setTournament] = useState(null);
33 const ctrl = new AbortController();
36 .get(`/api/tournaments/${id}`, { signal: ctrl.signal })
40 setTournament(sortParticipants(response.data));
53 window.Echo.channel(`Tournament.${id}`)
54 .listen('ApplicationAdded', e => {
56 setTournament(tournament => patchApplication(tournament, e.application));
59 .listen('ApplicationChanged', e => {
61 setTournament(tournament => patchApplication(tournament, e.application));
64 .listen('ApplicationRemoved', e => {
65 if (e.application_id) {
66 setTournament(tournament => removeApplication(tournament, e.application_id));
69 .listen('ParticipantChanged', e => {
71 setTournament(tournament => patchParticipant(tournament, e.participant));
74 .listen('ResultChanged', e => {
76 setTournament(tournament => patchResult(tournament, e.result));
79 .listen('RoundAdded', e => {
81 setTournament(tournament => ({
83 rounds: [e.round, ...tournament.rounds],
87 .listen('RoundChanged', e => {
89 setTournament(tournament => patchRound(tournament, e.round));
92 .listen('TournamentChanged', e => {
94 setTournament(tournament => ({ ...tournament, ...e.tournament }));
98 window.Echo.leave(`Tournament.${id}`);
102 const moreRounds = React.useCallback(async () => {
103 const last_round = getLastRound(tournament);
104 if (!last_round) return;
105 console.log(last_round);
106 const last_known = last_round.number;
107 const rsp = await axios.get(
108 `/api/tournaments/${id}/more-rounds`,
109 { params: { last_known } },
111 setTournament(tournament => ({
113 rounds: [...tournament.rounds, ...rsp.data],
115 }, [id, tournament]);
120 setTournament(tournament => patchUser(tournament, e.user));
123 window.Echo.channel('App.Control')
124 .listen('UserChanged', cb);
126 window.Echo.channel('App.Control')
127 .stopListening('UserChanged', cb);
136 return <ErrorMessage error={error} />;
143 const addRound = async () => {
144 await axios.post('/api/rounds', { tournament_id: tournament.id });
147 return <ErrorBoundary>
149 <title>{tournament.title}</title>
151 <CanonicalLinks base={`/tournaments/${tournament.id}`} />
154 moreRounds={canLoadMoreRounds(tournament) ? moreRounds : null}
155 tournament={tournament}