From efd7127575cf6436bd78cf36fed1ab008a22557c Mon Sep 17 00:00:00 2001 From: djmil Date: Wed, 18 Oct 2023 17:20:37 +0200 Subject: [PATCH] react: GlobalState: data, appCtx + polling feature - Polling - time beased URI polling - can be diabled - GlobaState - utilizes Flux pattern - data: for storing data for UI to work with - appData: for string global UI state - major refactoring --- webapp/src/App.css | 4 ++ webapp/src/App.js | 50 +++++++------------ webapp/src/Header.js | 17 ------- webapp/src/components/DataPolling/index.jsx | 16 ++++++ .../GameProposal}/GameProposalAction.js | 10 ++-- .../GameProposal/GameProposalCancel.js | 24 +++++++++ webapp/src/components/GameProposal/Reject.jsx | 21 ++++++++ .../GameProposal/index.css} | 0 .../GameProposal/index.jsx} | 50 +++++++++++-------- .../Header/index.css} | 0 webapp/src/components/Header/index.jsx | 17 +++++++ .../Leaderboard/index.css} | 0 .../Leaderboard/index.jsx} | 29 +++-------- webapp/src/context/app/index.jsx | 17 +++++++ webapp/src/context/app/reducer.js | 16 ++++++ webapp/src/context/data/Poll.js | 40 +++++++++++++++ webapp/src/context/data/index.jsx | 36 +++++++++++++ webapp/src/context/data/reducer.js | 14 ++++++ webapp/src/index.js | 15 ++++-- 19 files changed, 274 insertions(+), 102 deletions(-) delete mode 100644 webapp/src/Header.js create mode 100644 webapp/src/components/DataPolling/index.jsx rename webapp/src/{ => components/GameProposal}/GameProposalAction.js (66%) create mode 100644 webapp/src/components/GameProposal/GameProposalCancel.js create mode 100644 webapp/src/components/GameProposal/Reject.jsx rename webapp/src/{GameProposal.css => components/GameProposal/index.css} (100%) rename webapp/src/{GameProposal.js => components/GameProposal/index.jsx} (57%) rename webapp/src/{Header.css => components/Header/index.css} (100%) create mode 100644 webapp/src/components/Header/index.jsx rename webapp/src/{Leaderboard.css => components/Leaderboard/index.css} (100%) rename webapp/src/{Leaderboard.js => components/Leaderboard/index.jsx} (59%) create mode 100644 webapp/src/context/app/index.jsx create mode 100644 webapp/src/context/app/reducer.js create mode 100644 webapp/src/context/data/Poll.js create mode 100644 webapp/src/context/data/index.jsx create mode 100644 webapp/src/context/data/reducer.js diff --git a/webapp/src/App.css b/webapp/src/App.css index 50b63a3..f09c515 100644 --- a/webapp/src/App.css +++ b/webapp/src/App.css @@ -16,3 +16,7 @@ .App-link { color: #61dafb; } + +.Container { + margin-top: 25px; +} \ No newline at end of file diff --git a/webapp/src/App.js b/webapp/src/App.js index 9fac52a..f796e62 100644 --- a/webapp/src/App.js +++ b/webapp/src/App.js @@ -1,47 +1,31 @@ import './App.css'; -import React, { useState, useEffect, useCallback } from 'react'; +import React from 'react' import { BrowserRouter, Routes, Route, -} from "react-router-dom"; +} from "react-router-dom" -import Header from "./Header" -import Leaderboard from "./Leaderboard"; -import GameProposal from "./GameProposal"; +import Header from "./components/Header" +import Leaderboard from "./components/Leaderboard" +import GameProposal from "./components/GameProposal" +import DataPolling from './components/DataPolling'; + +//import { UserProvider } from "../contexts/UserProvider" +//import { GameProposalProvider } from './context/GameProposal'; function App() { - const [games, setGames] = useState(null); - const [polling, setPolling] = useState(false); - - const pollGames = useCallback(() => { - if (polling) - return; - - setPolling(true); - fetch('/api/gamestate') - .then(response => response.json()) - .then(data => { - setGames(data); - setPolling(false); - }) - .catch(err => console.log(err.message)); - }, [polling]); - - useEffect(() => { - const timer = setInterval(pollGames(), 35 * 1000); // <<-- poll new gamestates every 35 sec - return clearInterval(timer); - }, [pollGames]) return - - - - } /> - } /> - - + + + + + } /> + } /> + + } diff --git a/webapp/src/Header.js b/webapp/src/Header.js deleted file mode 100644 index f9dc7dc..0000000 --- a/webapp/src/Header.js +++ /dev/null @@ -1,17 +0,0 @@ -import { Link } from "react-router-dom"; -import './Header.css'; - -export default function Header() { - return ( - - CordaCheckers - - Leaderboard {"| "} - Game Proposal {"| "} - Active Games {"| "} - Archive {"| "} - About - -
-
Loading..
You {Stone(game.myColor)} vs {game.opponentName} {Stone(oppositeColor(game.myColor))} @@ -30,10 +31,10 @@ const GameProposal = ({games}) => {
You {Stone(game.myColor)} vs {game.opponentName} {Stone(oppositeColor(game.myColor))} @@ -47,7 +48,7 @@ const GameProposal = ({games}) => { return
Loading...