front: js readability

This commit is contained in:
djmil 2023-10-14 10:43:44 +02:00
parent c8480c3937
commit 8cac7533dd
3 changed files with 51 additions and 65 deletions

View File

@ -2,7 +2,7 @@ import './App.css';
import React, { useState, useEffect, useCallback } from 'react'; import React, { useState, useEffect, useCallback } from 'react';
import { import {
BrowserRouter, BrowserRouter,
Routes, //replaces "Switch" used till v5 Routes,
Route, Route,
} from "react-router-dom"; } from "react-router-dom";
@ -15,18 +15,13 @@ function App() {
const [polling, setPolling] = useState(false); const [polling, setPolling] = useState(false);
const pollGames = useCallback(() => { const pollGames = useCallback(() => {
console.log('start polling..'); if (polling)
if (polling) {
console.log(' ..already in progress');
return; return;
}
setPolling(true); setPolling(true);
fetch('/api/gamestate') fetch('/api/gamestate')
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
console.log('poooled');
setGames(data); setGames(data);
setPolling(false); setPolling(false);
}) })
@ -34,12 +29,11 @@ function App() {
}, [polling]); }, [polling]);
useEffect(() => { useEffect(() => {
const timer = setInterval(pollGames(), 35 * 1000); const timer = setInterval(pollGames(), 35 * 1000); // <<-- poll new gamestates every 35 sec
return clearInterval(timer); return clearInterval(timer);
}, [pollGames]) }, [pollGames])
return ( return <div className="App">
<div className="App">
<BrowserRouter> <BrowserRouter>
<Header/> <Header/>
<div className="Container"> <div className="Container">
@ -48,10 +42,8 @@ function App() {
<Route path="/gameproposal" element={<GameProposal games={games}/>} /> <Route path="/gameproposal" element={<GameProposal games={games}/>} />
</Routes> </Routes>
</div> </div>
</BrowserRouter> </BrowserRouter>
</div> </div>
);
} }
export default App; export default App;

View File

@ -16,30 +16,29 @@ const GameProposal = ({games}) => {
const waitForYou = games const waitForYou = games
.filter(game => game.status === State.WaitForYou) .filter(game => game.status === State.WaitForYou)
.map(game => { return ( .map(game => {
<div class="li" key={game.uuid}> return <div class="li" key={game.uuid}>
<p> <p>
from {game.opponentName}, opponentColor {game.opponentColor} from {game.opponentName}, opponentColor {game.opponentColor}
<br/> <br/>
<q>{game.message}</q> <q>{game.message}</q>
</p> </p>
</div> </div>
)}); });
const WaitForOpponent = games const WaitForOpponent = games
.filter(game => game.status === State.WaitForOpponent) .filter(game => game.status === State.WaitForOpponent)
.map(game => { return ( .map(game => {
<div class="li" key={game.uuid}> return <div class="li" key={game.uuid}>
<p> <p>
to {game.opponentName}, opponentColor {game.opponentColor} to {game.opponentName}, opponentColor {game.opponentColor}
<br/> <br/>
<q>{game.message}</q> <q>{game.message}</q>
</p> </p>
</div> </div>
)}); });
return ( return <div className="GameProposal">
<p className="GameProposal">
{waitForYou} {waitForYou}
{WaitForOpponent.length > 0 && {WaitForOpponent.length > 0 &&
<div class="separator"> <div class="separator">
@ -47,8 +46,7 @@ const GameProposal = ({games}) => {
</div> </div>
} }
{WaitForOpponent} {WaitForOpponent}
</p> </div>
);
}; };
export default GameProposal; export default GameProposal;

View File

@ -31,18 +31,15 @@ const Leaderboard = () => {
const tableRows = Object.keys(data).map(playerName => { const tableRows = Object.keys(data).map(playerName => {
var rank = data[playerName]; var rank = data[playerName];
return ( return <tr key={playerName}>
<tr key={playerName}>
<td>{playerName}</td> <td>{playerName}</td>
<td>{rank.gamesPlayed}</td> <td>{rank.gamesPlayed}</td>
<td>{rank.gamesWon}</td> <td>{rank.gamesWon}</td>
<td>{rank.gamesDraw}</td> <td>{rank.gamesDraw}</td>
</tr> </tr>
);
}); });
return ( return <div className="Leaderboard">
<div className="Leaderboard">
<table> <table>
<thead> <thead>
<tr> <tr>
@ -57,7 +54,6 @@ const Leaderboard = () => {
</tbody> </tbody>
</table> </table>
</div> </div>
);
}; };
export default Leaderboard; export default Leaderboard;