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