mirror of
https://github.com/TheRealOwenRees/chess-scribe-website.git
synced 2026-07-23 09:56:57 +00:00
set and clear game state using reducers - removed the need for global state management
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
interface IDiagrams {
|
||||
ply: number;
|
||||
fen: string;
|
||||
}
|
||||
|
||||
export interface IGameState {
|
||||
pgn: string;
|
||||
headers: string;
|
||||
diagrams: IDiagrams[];
|
||||
diagramClock: boolean;
|
||||
}
|
||||
|
||||
type GameAction =
|
||||
| { type: "SET_GAME"; payload: { pgn: string; headers: "" } }
|
||||
| { type: "CLEAR_GAME" };
|
||||
|
||||
export const initialGameState = {
|
||||
pgn: "",
|
||||
headers: "",
|
||||
diagrams: [],
|
||||
diagramClock: false,
|
||||
};
|
||||
|
||||
export const gameReducer = (state: IGameState, action: GameAction) => {
|
||||
switch (action.type) {
|
||||
case "SET_GAME":
|
||||
return {
|
||||
pgn: action.payload.pgn,
|
||||
headers: "",
|
||||
diagrams: [],
|
||||
diagramClock: false,
|
||||
};
|
||||
case "CLEAR_GAME":
|
||||
return initialGameState;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user