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