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