React: GlobalState updates
Remove DataCtx dependacy on AppCtx
This commit is contained in:
parent
efd7127575
commit
5821a02ba1
@ -3,13 +3,13 @@ import { AppData } from "../../context/data"
|
|||||||
import { AppContext } from "../../context/app"
|
import { AppContext } from "../../context/app"
|
||||||
|
|
||||||
export default function DataPolling() {
|
export default function DataPolling() {
|
||||||
const [appData] = React.useContext(AppData)
|
const [appData, dispatchData] = React.useContext(AppData)
|
||||||
const [appCtx, dispatchAppData] = React.useContext(AppContext)
|
//const [appCtx, dispatchAppData] = React.useContext(AppContext)
|
||||||
|
|
||||||
return <div className={DataPolling.name}>
|
return <div className={DataPolling.name}>
|
||||||
polling
|
polling
|
||||||
<button onClick={() => dispatchAppData({type: "togglePolling"})}>
|
<button onClick={() => dispatchData({type: "togglePolling"})}>
|
||||||
{ appCtx.disablePolling === true ? "off" : "on" }
|
{ appData.disablePolling === true ? "off" : "on" }
|
||||||
</button>
|
</button>
|
||||||
{ appData.fetching }
|
{ appData.fetching }
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,16 +1,12 @@
|
|||||||
export const reducer = (state, action) => {
|
export const reducer = (state, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
|
||||||
case "togglePolling":
|
|
||||||
return { ...state,
|
|
||||||
disablePolling: !state.disablePolling // on/off
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
console.warn("Unknown action.type", action)
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const initialState = {
|
export const initialState = {
|
||||||
disablePolling: false,
|
myState: false,
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,14 @@
|
|||||||
import { useState, useCallback, useEffect, } from "react"
|
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) {
|
export default function Poll(url, interval_sec, disabled) {
|
||||||
const [cache, setCache] = useState(null)
|
const [cache, setCache] = useState(null)
|
||||||
const [fetching, setFetching] = useState(false)
|
const [fetching, setFetching] = useState(false)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import React from "react"
|
import React from "react"
|
||||||
import { reducer, initialState } from "./reducer"
|
import { reducer, initialState } from "./reducer"
|
||||||
import { AppContext } from "../app"
|
|
||||||
|
|
||||||
import Poll from "./Poll"
|
import Poll from "./Poll"
|
||||||
|
|
||||||
@ -12,10 +11,9 @@ export const AppData = React.createContext({
|
|||||||
export const AppDataProvider = ({ children }) => {
|
export const AppDataProvider = ({ children }) => {
|
||||||
|
|
||||||
const [data, dispatchData] = React.useReducer(reducer, initialState)
|
const [data, dispatchData] = React.useReducer(reducer, initialState)
|
||||||
const [appContext] = React.useContext(AppContext)
|
|
||||||
|
|
||||||
const games = Poll('api/gamestate', 30, appContext.disablePolling)
|
const games = Poll('api/gamestate', 30, data.disablePolling)
|
||||||
const leaderboard = Poll('api/leaderboard', 60, appContext.disablePolling)
|
const leaderboard = Poll('api/leaderboard', 60, data.disablePolling)
|
||||||
|
|
||||||
data.games = games.data
|
data.games = games.data
|
||||||
data.leaderboard = leaderboard.data
|
data.leaderboard = leaderboard.data
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
export const reducer = (state, action) => {
|
export const reducer = (state, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
|
||||||
|
case "togglePolling":
|
||||||
|
return { ...state,
|
||||||
|
disablePolling: !state.disablePolling // on/off
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
console.warn("Unknown action.type", action)
|
console.warn("Unknown action.type", action)
|
||||||
return state
|
return state
|
||||||
@ -10,5 +15,7 @@ export const reducer = (state, action) => {
|
|||||||
export const initialState = {
|
export const initialState = {
|
||||||
games: null,
|
games: null,
|
||||||
leaderboard: null,
|
leaderboard: null,
|
||||||
|
|
||||||
|
disablePolling: false,
|
||||||
fetching: []
|
fetching: []
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user