]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/App.js
public tournament page
[alttp.git] / resources / js / components / App.js
1 import axios from 'axios';
2 import React, { useEffect, useState } from 'react';
3 import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom';
4
5 import Header from './common/Header';
6 import Tournament from './pages/Tournament';
7 import UserContext from '../helpers/UserContext';
8
9 const App = () => {
10         const [user, setUser] = useState(null);
11
12         const checkAuth = async () => {
13                 try {
14                         const response = await axios.get('/api/user');
15                         setUser(response.data);
16                 } catch (e) {
17                         setUser(null);
18                 }
19         };
20
21         const doLogout = async () => {
22                 await axios.post('/logout');
23                 await checkAuth();
24         };
25
26         useEffect(() => {
27                 let timer = null;
28                 axios
29                         .get('/sanctum/csrf-cookie')
30                         .then(() => {
31                                 checkAuth();
32                                 timer = setInterval(checkAuth, 15 * 60 * 1000);
33                         });
34                 return () => {
35                         if (timer) clearInterval(timer);
36                 };
37         }, []);
38
39         useEffect(() => {
40                 window.Echo.channel('App.Control')
41                         .listen('PleaseRefresh', () => {
42                                 location.reload();
43                         });
44                 return () => {
45                         window.Echo.leave('App.Control');
46                 };
47         }, []);
48
49         return <BrowserRouter>
50                 <UserContext.Provider value={user}>
51                         <Header doLogout={doLogout} />
52                         <Routes>
53                                 <Route path="tournaments/:id" element={<Tournament />} />
54                                 <Route path="*" element={<Navigate to="/tournaments/1" />} />
55                         </Routes>
56                 </UserContext.Provider>
57         </BrowserRouter>;
58 };
59
60 export default App;