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, 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],
53 window.Echo.leave(`Tournament.${id}`);
62 return <ErrorMessage error={error} />;
69 const addRound = async () => {
70 await axios.post('/api/rounds', { tournament_id: tournament.id });
73 return <ErrorBoundary>
74 <Detail addRound={addRound} tournament={tournament} />
78 export default Tournament;