2023-08-13 15:31:47 +02:00
|
|
|
import './App.css';
|
2023-10-14 10:01:20 +02:00
|
|
|
import React, { useState, useEffect, useCallback } from 'react';
|
2023-10-11 13:16:27 +02:00
|
|
|
import {
|
|
|
|
BrowserRouter,
|
|
|
|
Routes, //replaces "Switch" used till v5
|
|
|
|
Route,
|
|
|
|
} from "react-router-dom";
|
2023-10-14 10:01:20 +02:00
|
|
|
|
2023-10-11 13:16:27 +02:00
|
|
|
import Header from "./Header"
|
2023-10-14 10:01:20 +02:00
|
|
|
import Leaderboard from "./Leaderboard";
|
|
|
|
import GameProposal from "./GameProposal";
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
const [games, setGames] = useState(null);
|
|
|
|
const [polling, setPolling] = useState(false);
|
|
|
|
|
|
|
|
const pollGames = useCallback(() => {
|
|
|
|
console.log('start polling..');
|
|
|
|
|
|
|
|
if (polling) {
|
|
|
|
console.log(' ..already in progress');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setPolling(true);
|
|
|
|
fetch('/api/gamestate')
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(data => {
|
|
|
|
console.log('poooled');
|
|
|
|
setGames(data);
|
|
|
|
setPolling(false);
|
|
|
|
})
|
|
|
|
.catch(err => console.log(err.message));
|
|
|
|
}, [polling]);
|
2023-08-13 15:31:47 +02:00
|
|
|
|
2023-10-14 10:01:20 +02:00
|
|
|
useEffect(() => {
|
|
|
|
const timer = setInterval(pollGames(), 35 * 1000);
|
|
|
|
return clearInterval(timer);
|
|
|
|
}, [pollGames])
|
2023-08-13 15:31:47 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="App">
|
2023-10-11 13:16:27 +02:00
|
|
|
<BrowserRouter>
|
|
|
|
<Header/>
|
|
|
|
<div className="Container">
|
|
|
|
<Routes>
|
|
|
|
<Route path="/leaderboard" element={<Leaderboard/>} />
|
2023-10-14 10:01:20 +02:00
|
|
|
<Route path="/gameproposal" element={<GameProposal games={games}/>} />
|
2023-10-11 13:16:27 +02:00
|
|
|
</Routes>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</BrowserRouter>
|
2023-08-13 15:31:47 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|