From 5821a02ba1ca2be17b2d67fc56166d964effe432 Mon Sep 17 00:00:00 2001 From: djmil Date: Thu, 19 Oct 2023 14:39:43 +0200 Subject: [PATCH] React: GlobalState updates Remove DataCtx dependacy on AppCtx --- webapp/src/components/DataPolling/index.jsx | 8 ++++---- webapp/src/context/app/reducer.js | 8 ++------ webapp/src/context/data/Poll.js | 9 +++++++++ webapp/src/context/data/index.jsx | 6 ++---- webapp/src/context/data/reducer.js | 7 +++++++ 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/webapp/src/components/DataPolling/index.jsx b/webapp/src/components/DataPolling/index.jsx index 4b4a789..96adfd8 100644 --- a/webapp/src/components/DataPolling/index.jsx +++ b/webapp/src/components/DataPolling/index.jsx @@ -3,13 +3,13 @@ import { AppData } from "../../context/data" import { AppContext } from "../../context/app" export default function DataPolling() { - const [appData] = React.useContext(AppData) - const [appCtx, dispatchAppData] = React.useContext(AppContext) + const [appData, dispatchData] = React.useContext(AppData) + //const [appCtx, dispatchAppData] = React.useContext(AppContext) return
polling - { appData.fetching }
diff --git a/webapp/src/context/app/reducer.js b/webapp/src/context/app/reducer.js index cc1536a..240ba51 100644 --- a/webapp/src/context/app/reducer.js +++ b/webapp/src/context/app/reducer.js @@ -1,16 +1,12 @@ export const reducer = (state, action) => { switch (action.type) { - case "togglePolling": - return { ...state, - disablePolling: !state.disablePolling // on/off - } - default: + console.warn("Unknown action.type", action) return state } } export const initialState = { - disablePolling: false, + myState: false, } diff --git a/webapp/src/context/data/Poll.js b/webapp/src/context/data/Poll.js index 55b37cf..6343d70 100644 --- a/webapp/src/context/data/Poll.js +++ b/webapp/src/context/data/Poll.js @@ -1,5 +1,14 @@ import { useState, useCallback, useEffect, } from "react" +/* + TODO: Poll(uri, flavour) + - uri: string + - execution_flvour: + - once (i.e. now) + - interval (sec) + - stop +*/ + export default function Poll(url, interval_sec, disabled) { const [cache, setCache] = useState(null) const [fetching, setFetching] = useState(false) diff --git a/webapp/src/context/data/index.jsx b/webapp/src/context/data/index.jsx index c1dc614..1340747 100644 --- a/webapp/src/context/data/index.jsx +++ b/webapp/src/context/data/index.jsx @@ -1,6 +1,5 @@ import React from "react" import { reducer, initialState } from "./reducer" -import { AppContext } from "../app" import Poll from "./Poll" @@ -12,10 +11,9 @@ export const AppData = React.createContext({ export const AppDataProvider = ({ children }) => { const [data, dispatchData] = React.useReducer(reducer, initialState) - const [appContext] = React.useContext(AppContext) - const games = Poll('api/gamestate', 30, appContext.disablePolling) - const leaderboard = Poll('api/leaderboard', 60, appContext.disablePolling) + const games = Poll('api/gamestate', 30, data.disablePolling) + const leaderboard = Poll('api/leaderboard', 60, data.disablePolling) data.games = games.data data.leaderboard = leaderboard.data diff --git a/webapp/src/context/data/reducer.js b/webapp/src/context/data/reducer.js index b6fcb2e..f06e879 100644 --- a/webapp/src/context/data/reducer.js +++ b/webapp/src/context/data/reducer.js @@ -1,6 +1,11 @@ export const reducer = (state, action) => { switch (action.type) { + case "togglePolling": + return { ...state, + disablePolling: !state.disablePolling // on/off + } + default: console.warn("Unknown action.type", action) return state @@ -10,5 +15,7 @@ export const reducer = (state, action) => { export const initialState = { games: null, leaderboard: null, + + disablePolling: false, fetching: [] }