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));
44 window.Echo.channel(`Tournament.${id}`)
45 .listen('ApplicationAdded', e => {
47 setTournament(tournament => patchApplication(tournament, e.application));
50 .listen('ApplicationChanged', e => {
52 setTournament(tournament => patchApplication(tournament, e.application));
55 .listen('ParticipantChanged', e => {
57 setTournament(tournament => patchParticipant(tournament, e.participant));
60 .listen('ResultChanged', e => {
62 setTournament(tournament => patchResult(tournament, e.result));
65 .listen('RoundAdded', e => {
67 setTournament(tournament => ({
69 rounds: [e.round, ...tournament.rounds],
73 .listen('RoundChanged', e => {
75 setTournament(tournament => patchRound(tournament, e.round));
78 .listen('TournamentChanged', e => {
80 setTournament(tournament => ({ ...tournament, ...e.tournament }));
84 window.Echo.leave(`Tournament.${id}`);
91 setTournament(tournament => patchUser(tournament, e.user));
94 window.Echo.channel('App.Control')
95 .listen('UserChanged', cb);
97 window.Echo.channel('App.Control')
98 .stopListening('UserChanged', cb);
107 return <ErrorMessage error={error} />;
114 const addRound = async () => {
115 await axios.post('/api/rounds', { tournament_id: tournament.id });
118 return <ErrorBoundary>
119 <Detail addRound={addRound} tournament={tournament} />
123 export default Tournament;