GamesContext: initial implementation

This commit is contained in:
djmil 2023-11-09 13:59:14 +01:00
parent 3f47654cf2
commit ac50d92c1a
8 changed files with 64 additions and 55 deletions

View File

@ -10,27 +10,29 @@ import Games from './container/Games';
import Leaderboard from './container/Leaderboard'; import Leaderboard from './container/Leaderboard';
import usePollingReducer from './reducer/polling'; import usePollingReducer from './reducer/polling';
//import useGamesReducer from './reducer/games'; import useGamesReducer from './reducer/games';
import useUserApi from './api/user'; import useUserApi from './api/user';
import useLeaderboardApi from './api/leaderboard'; import useLeaderboardApi from './api/leaderboard';
//import useGamesApi from './api/games'; import useGamesApi from './api/games';
export default function App() { export default function App() {
const pollingReducer = usePollingReducer(); const pollingReducer = usePollingReducer();
//const gamesReducer = useGamesReducer();
//const games = useGamesApi(gamesReducer).list(pollingReducer);
const leaderboard = useLeaderboardApi().poll(pollingReducer); const leaderboard = useLeaderboardApi().poll(pollingReducer);
const user = useUserApi().get(); const user = useUserApi().get();
const [games, dispatchGames] = useGamesReducer();
const gamesApi = useGamesApi(dispatchGames);
gamesApi.list(pollingReducer);
return ( return (
<BrowserRouter> <BrowserRouter>
<Header pollingReducer={pollingReducer} /> <Header pollingReducer={pollingReducer} />
<Routes> <Routes>
<Route path='/' element={<About />} /> <Route path='/' element={<About />} />
<Route path='/about' element={<About />} /> <Route path='/about' element={<About />} />
<Route path='/games/*' element={<Games />} /> <Route path='/games/*' element={<Games games={games} dispatchGames={dispatchGames} gamesApi={gamesApi} />} />
<Route path='/leaderboard' element={<Leaderboard leaderboard={leaderboard} user={user} />} /> <Route path='/leaderboard' element={<Leaderboard leaderboard={leaderboard} user={user} />} />
</Routes> </Routes>
</BrowserRouter> </BrowserRouter>

View File

@ -1,22 +1,23 @@
import usePolling from "../util/Polling" import usePolling from '../util/Polling';
export default function useGamesApi(gamesState) { export default function useGamesApi(dispatchGames) {
const [games, setGames] = gamesState;
const useList = (pollingReducer) => { const useList = (pollingReducer) => {
const [polling, dispatchPolling] = pollingReducer; const [polling, dispatchPolling] = pollingReducer;
const onResponce = (json) => {
dispatchGames({ type: 'next', list: json });
}
const mode = (polling.enabled === true) const mode = (polling.enabled === true)
? { interval_sec: 30 } // update games list every half a minue ? { interval_sec: 30 } // fetch gamesList every half a minue
: { interval_stop: true } // user has fliped OfflineToggel : { interval_stop: true } // user has fliped OfflineToggel
const isPolling = usePolling('/api/games', setGames, mode); const isPolling = usePolling('/api/games', onResponce, mode);
if (isPolling !== polling.games) { if (isPolling !== polling.games) {
dispatchPolling({ type: 'next', games: isPolling }); dispatchPolling({ type: 'next', games: isPolling });
} }
return games;
} }
return { return {

View File

@ -10,6 +10,7 @@ export default function useUserApi() {
} }
usePolling('/api/user', onResponce); // <<-- fetch once usePolling('/api/user', onResponce); // <<-- fetch once
return user; return user;
} }

View File

@ -1,6 +1,6 @@
import './Games.css'; import './Games.css';
import React from "react" import React from 'react';
import { NavLink, Routes, Route } from "react-router-dom"; import { NavLink, Routes, Route } from 'react-router-dom';
import NewGame from './games/view/NewGame'; import NewGame from './games/view/NewGame';
import GameProposals from './games/view/GameProposals'; import GameProposals from './games/view/GameProposals';
@ -19,10 +19,15 @@ import Forward from './games/action/Forward';
import GameBoard from './games/GameBoard'; import GameBoard from './games/GameBoard';
export default function Games({ gamesReducer, user }) { import { GamesContext, GamesDispatchContext, GamesApiContext } from '../context/games';
export default function Games({ games, dispatchGames, gamesApi }) {
return ( return (
<div className="Games"> <div className="Games">
<GamesContext.Provider value={games} >
<GamesDispatchContext.Provider value={dispatchGames} >
<GamesApiContext.Provider value={gamesApi} >
<div className='left-side'> <div className='left-side'>
<ViewSelector /> <ViewSelector />
<ViewProvider /> <ViewProvider />
@ -32,10 +37,12 @@ export default function Games({ gamesReducer, user }) {
<GameBoard /> <GameBoard />
{/* {/*
<GameMessage /> <GameMessage />
<GameBoard />
<Message2Opponent /> */} <Message2Opponent /> */}
</div> </div>
</div> </GamesApiContext.Provider>
</GamesDispatchContext.Provider>
</GamesContext.Provider>
</div >
) )
}; };

View File

@ -1,6 +1,11 @@
import React from 'react'; import React, { useContext } from 'react';
import { GamesContext } from '../../../context/games';
export default function NewGame() { export default function NewGame() {
const games = useContext(GamesContext);
console.log("NewGame", games);
return ( return (
<div>View: NewGame</div> <div>View: NewGame</div>
) )

View File

@ -1,17 +1,20 @@
import React from "react" import { createContext } from 'react';
import { reducer, initialState } from "./reducer"
export const Games = React.createContext({ export const GamesContext = createContext(null);
state: initialState, export const GamesDispatchContext = createContext(null);
dispatch: () => null export const GamesApiContext = createContext(null);
})
export const GamesProvider = ({ children }) => { // export const Games = React.createContext({
const [state, dispatch] = React.useReducer(reducer, initialState) // state: initialState,
// dispatch: () => null
// })
return ( // export const GamesProvider = ({ children }) => {
<Games.Provider value={[ state, dispatch ]}> // const [state, dispatch] = React.useReducer(reducer, initialState)
{ children }
</Games.Provider> // return (
) // <Games.Provider value={[ state, dispatch ]}>
} // { children }
// </Games.Provider>
// )
// }

View File

@ -12,7 +12,7 @@ export function gamesReducer(state, action) {
return nextState(state, action); return nextState(state, action);
default: default:
throw Error('GameReducer: unknown action.type', action.type); throw Error('GamesReducer: unknown action.type', action.type);
} }
} }

View File

@ -1,6 +1,5 @@
import { useReducer } from 'react'; import { useReducer } from 'react';
import { localeCompare } from '../util/Locale'; import { localeCompare } from '../util/Locale';
//import { nextState } from '../util/StateHelper';
export const userInitialState = { export const userInitialState = {
username: '', username: '',
@ -14,11 +13,9 @@ export function userReducer(state, action) {
switch (action.type) { switch (action.type) {
case 'parse': case 'parse':
const apiData = parse(action.json);
return { return {
...state, ...state,
...apiData username: action.json.username
}; };
default: default:
@ -29,10 +26,3 @@ export function userReducer(state, action) {
export default function useUserReducer() { export default function useUserReducer() {
return useReducer(userReducer, userInitialState); return useReducer(userReducer, userInitialState);
} }
function parse(json) {
console.log("userreducer.parse", json);
return {
username: json.username
}
}