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';
10 import { patchResult, patchRound, sortParticipants } from '../../helpers/Tournament';
12 const Tournament = () => {
13 const params = useParams();
14 const { id } = params;
16 const [error, setError] = useState(null);
17 const [loading, setLoading] = useState(true);
18 const [tournament, setTournament] = useState(null);
23 .get(`/api/tournaments/${id}`)
27 setTournament(sortParticipants(response.data));
37 window.Echo.private(`Tournament.${id}`)
38 .listen('ResultReported', e => {
41 setTournament(tournament => patchResult(tournament, e.result));
44 .listen('RoundAdded', e => {
46 setTournament(tournament => ({
48 rounds: [...tournament.rounds, e.round],
52 .listen('RoundChanged', e => {
54 setTournament(tournament => patchRound(tournament, e.round));
58 window.Echo.leave(`Tournament.${id}`);
67 return <ErrorMessage error={error} />;
74 const addRound = async () => {
75 await axios.post('/api/rounds', { tournament_id: tournament.id });
78 return <ErrorBoundary>
79 <Detail addRound={addRound} tournament={tournament} />
83 export default Tournament;