]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/pages/Tournament.js
improved directory structure
[alttp.git] / resources / js / pages / Tournament.js
diff --git a/resources/js/pages/Tournament.js b/resources/js/pages/Tournament.js
new file mode 100644 (file)
index 0000000..20f34ce
--- /dev/null
@@ -0,0 +1,140 @@
+import axios from 'axios';
+import React, { useEffect, useState } from 'react';
+import { Helmet } from 'react-helmet';
+import { useParams } from 'react-router-dom';
+
+import CanonicalLinks from '../components/common/CanonicalLinks';
+import ErrorBoundary from '../components/common/ErrorBoundary';
+import ErrorMessage from '../components/common/ErrorMessage';
+import Loading from '../components/common/Loading';
+import NotFound from '../pages/NotFound';
+import Detail from '../components/tournament/Detail';
+import {
+       patchApplication,
+       patchParticipant,
+       patchResult,
+       patchRound,
+       patchUser,
+       removeApplication,
+       sortParticipants,
+} from '../helpers/Tournament';
+
+const Tournament = () => {
+       const params = useParams();
+       const { id } = params;
+
+       const [error, setError] = useState(null);
+       const [loading, setLoading] = useState(true);
+       const [tournament, setTournament] = useState(null);
+
+       useEffect(() => {
+               const ctrl = new AbortController();
+               setLoading(true);
+               axios
+                       .get(`/api/tournaments/${id}`, { signal: ctrl.signal })
+                       .then(response => {
+                               setError(null);
+                               setLoading(false);
+                               setTournament(sortParticipants(response.data));
+                       })
+                       .catch(error => {
+                               setError(error);
+                               setLoading(false);
+                               setTournament(null);
+                       });
+               return () => {
+                       ctrl.abort();
+               };
+       }, [id]);
+
+       useEffect(() => {
+               window.Echo.channel(`Tournament.${id}`)
+                       .listen('ApplicationAdded', e => {
+                               if (e.application) {
+                                       setTournament(tournament => patchApplication(tournament, e.application));
+                               }
+                       })
+                       .listen('ApplicationChanged', e => {
+                               if (e.application) {
+                                       setTournament(tournament => patchApplication(tournament, e.application));
+                               }
+                       })
+                       .listen('ApplicationRemoved', e => {
+                               if (e.application_id) {
+                                       setTournament(tournament => removeApplication(tournament, e.application_id));
+                               }
+                       })
+                       .listen('ParticipantChanged', e => {
+                               console.log(e);
+                               if (e.participant) {
+                                       setTournament(tournament => patchParticipant(tournament, e.participant));
+                               }
+                       })
+                       .listen('ResultChanged', e => {
+                               if (e.result) {
+                                       setTournament(tournament => patchResult(tournament, e.result));
+                               }
+                       })
+                       .listen('RoundAdded', e => {
+                               if (e.round) {
+                                       setTournament(tournament => ({
+                                               ...tournament,
+                                               rounds: [e.round, ...tournament.rounds],
+                                       }));
+                               }
+                       })
+                       .listen('RoundChanged', e => {
+                               if (e.round) {
+                                       setTournament(tournament => patchRound(tournament, e.round));
+                               }
+                       })
+                       .listen('TournamentChanged', e => {
+                               if (e.tournament) {
+                                       setTournament(tournament => ({ ...tournament, ...e.tournament }));
+                               }
+                       });
+               return () => {
+                       window.Echo.leave(`Tournament.${id}`);
+               };
+       }, [id]);
+
+       useEffect(() => {
+               const cb = (e) => {
+                       if (e.user) {
+                               setTournament(tournament => patchUser(tournament, e.user));
+                       }
+               };
+               window.Echo.channel('App.Control')
+                       .listen('UserChanged', cb);
+               return () => {
+                       window.Echo.channel('App.Control')
+                               .stopListening('UserChanged', cb);
+               };
+       }, []);
+
+       if (loading) {
+               return <Loading />;
+       }
+
+       if (error) {
+               return <ErrorMessage error={error} />;
+       }
+
+       if (!tournament) {
+               return <NotFound />;
+       }
+
+       const addRound = async () => {
+               await axios.post('/api/rounds', { tournament_id: tournament.id });
+       };
+
+       return <ErrorBoundary>
+               <Helmet>
+                       <title>{tournament.title}</title>
+               </Helmet>
+               <CanonicalLinks base={`/tournaments/${tournament.id}`} />
+               <Detail addRound={addRound} tournament={tournament} />
+       </ErrorBoundary>;
+};
+
+export default Tournament;