]> git.localhorst.tv Git - alttp.git/blob - resources/js/app/index.js
events overview
[alttp.git] / resources / js / app / index.js
1 import axios from 'axios';
2 import React, { useEffect, useState } from 'react';
3 import { Helmet } from 'react-helmet';
4 import { useTranslation } from 'react-i18next';
5 import { BrowserRouter } from 'react-router-dom';
6
7 import Routes from './Routes';
8 import AlttpBaseRomProvider from '../helpers/AlttpBaseRomContext';
9 import UserContext from '../helpers/UserContext';
10 import i18n from '../i18n';
11
12 const App = () => {
13         const [user, setUser] = useState(null);
14
15         const { t } = useTranslation();
16
17         const checkAuth = async () => {
18                 try {
19                         const response = await axios.get('/api/user');
20                         setUser(response.data);
21                 } catch (e) {
22                         setUser(null);
23                 }
24         };
25
26         const doLogout = async () => {
27                 await axios.post('/logout');
28                 await checkAuth();
29         };
30
31         useEffect(() => {
32                 let timer = null;
33                 axios
34                         .get('/sanctum/csrf-cookie')
35                         .then(() => {
36                                 checkAuth();
37                                 timer = setInterval(checkAuth, 15 * 60 * 1000);
38                         });
39                 return () => {
40                         if (timer) clearInterval(timer);
41                 };
42         }, []);
43
44         useEffect(() => {
45                 window.Echo.channel('App.Control')
46                         .listen('PleaseRefresh', () => {
47                                 location.reload();
48                         });
49                 return () => {
50                         window.Echo.leave('App.Control');
51                 };
52         }, []);
53
54         return <BrowserRouter>
55                 <AlttpBaseRomProvider>
56                         <UserContext.Provider value={user}>
57                                 <Helmet>
58                                         <html lang={i18n.language} />
59                                         <title>{t('general.appName')}</title>
60                                         <meta name="description" content={t('general.appDescription')} />
61                                 </Helmet>
62                                 <Routes doLogout={doLogout} />
63                         </UserContext.Provider>
64                 </AlttpBaseRomProvider>
65         </BrowserRouter>;
66 };
67
68 export default App;