Compare commits

...

3 Commits

Author SHA1 Message Date
b34335cefc front: show action button only for onHover elemnt 2023-10-14 11:59:05 +02:00
8cac7533dd front: js readability 2023-10-14 10:43:44 +02:00
c8480c3937 gameproposal: move game.message color to css 2023-10-14 10:06:13 +02:00
5 changed files with 94 additions and 69 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";
@ -14,19 +14,14 @@ function App() {
const [games, setGames] = useState(null); const [games, setGames] = useState(null);
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,24 +29,21 @@ 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"> <Routes>
<Routes> <Route path="/leaderboard" element={<Leaderboard/>} />
<Route path="/leaderboard" element={<Leaderboard/>} /> <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

@ -15,6 +15,18 @@
margin: 5px; margin: 5px;
} }
.GameProposal .li p q {
color: gray;
}
.GameProposal .li button.action {
display: none;
}
.GameProposal .li:hover button.action {
display: initial;
}
.separator { .separator {
width: 20%; width: 20%;
/* height: 20px; */ /* height: 20px; */

View File

@ -1,4 +1,5 @@
import React from 'react'; import React from 'react';
import {Accept, Reject, Cancel} from './GameProposalAction';
import './GameProposal.css'; import './GameProposal.css';
const State = { const State = {
@ -16,37 +17,42 @@ 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}<br/> from {game.opponentName}, opponentColor {game.opponentColor}
<q color="grey">{game.message}</q> <br/>
<q>{game.message}</q>
<br/>
<Accept uuid={game.uuid}/>
<Reject uuid={game.uuid}/>
</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}<br/> to {game.opponentName}, opponentColor {game.opponentColor}
<q color="grey">{game.message}</q> <br/>
<q>{game.message}</q>
<br/>
<Cancel uuid={game.uuid}/>
</p> </p>
</div>
)});
return (
<p className="GameProposal">
{waitForYou}
{WaitForOpponent.length > 0 &&
<div class="separator">
waiting for opponent ({WaitForOpponent.length})
</div> </div>
} });
{WaitForOpponent}
</p> return <div className="GameProposal">
); {waitForYou}
{WaitForOpponent.length > 0 &&
<div class="separator">
waiting for opponent ({WaitForOpponent.length})
</div>
}
{WaitForOpponent}
</div>
}; };
export default GameProposal; export default GameProposal;

View File

@ -0,0 +1,19 @@
import React from 'react';
export function Accept({uuid}) {
return <button className="action" id={uuid} type="submit">
Accept
</button>
}
export function Reject({uuid}) {
return <button className="action" id={uuid} type="submit">
Reject
</button>
}
export function Cancel({uuid}) {
return <button className="action" id={uuid} type="submit">
Cancel
</button>
}

View File

@ -24,40 +24,36 @@ const Leaderboard = () => {
// //
// return <li key={playerName}> // return <li key={playerName}>
// {playerName}: played {rank.gamesPlayed}, won {rank.gamesWon}, draw {rank.gamesDraw} // {playerName}: played {rank.gamesPlayed}, won {rank.gamesWon}, draw {rank.gamesDraw}
// </li> // </li>
// }); // });
// return <ul>{listItems}</ul>; // return <ul>{listItems}</ul>;
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> <th></th>
<th></th> <th>Played</th>
<th>Played</th> <th>Won</th>
<th>Won</th> <th>Draw</th>
<th>Draw</th> </tr>
</tr> </thead>
</thead> <tbody>
<tbody> { tableRows }
{ tableRows } </tbody>
</tbody> </table>
</table>
</div> </div>
);
}; };
export default Leaderboard; export default Leaderboard;