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