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';
16 } from '../../helpers/Tournament';
18 const Tournament = () => {
19 const params = useParams();
20 const { id } = params;
22 const [error, setError] = useState(null);
23 const [loading, setLoading] = useState(true);
24 const [tournament, setTournament] = useState(null);
29 .get(`/api/tournaments/${id}`)
33 setTournament(sortParticipants(response.data));
43 window.Echo.channel(`Tournament.${id}`)
44 .listen('ParticipantChanged', e => {
46 setTournament(tournament => patchParticipant(tournament, e.participant));
49 .listen('ResultChanged', e => {
51 setTournament(tournament => patchResult(tournament, e.result));
54 .listen('RoundAdded', e => {
56 setTournament(tournament => ({
58 rounds: [e.round, ...tournament.rounds],
62 .listen('RoundChanged', e => {
64 setTournament(tournament => patchRound(tournament, e.round));
67 .listen('TournamentChanged', e => {
69 setTournament(tournament => ({ ...tournament, ...e.tournament }));
73 window.Echo.leave(`Tournament.${id}`);
80 setTournament(tournament => patchUser(tournament, e.user));
83 window.Echo.channel('App.Control')
84 .listen('UserChanged', cb);
86 window.Echo.channel('App.Control')
87 .stopListening('UserChanged', cb);
96 return <ErrorMessage error={error} />;
103 const addRound = async () => {
104 await axios.post('/api/rounds', { tournament_id: tournament.id });
107 return <ErrorBoundary>
108 <Detail addRound={addRound} tournament={tournament} />
112 export default Tournament;