From 7ed91f36216cccb1d00d96cfd9c48a03039a6a8e Mon Sep 17 00:00:00 2001 From: Inga Date: Tue, 19 Nov 2024 01:13:09 +0000 Subject: [PATCH] extracted display controls to shared function --- src/backend/components/boardgame.tsx | 30 ++++++++++++++-------------- src/shared/display.ts | 11 ++++++++++ 2 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 src/shared/display.ts diff --git a/src/backend/components/boardgame.tsx b/src/backend/components/boardgame.tsx index 9a755cb..ca46281 100644 --- a/src/backend/components/boardgame.tsx +++ b/src/backend/components/boardgame.tsx @@ -6,6 +6,16 @@ import { SquareState, formatSquareState, } from "../../shared/datatypes.ts"; +import { ClassNames, getDisplayStates } from "../../shared/display.ts"; + +const getClassAndDisplayAttributes = ( + className: ClassNames, + gameState: BoardgameStateType, + currentOutcome: CurrentOutcome, +) => ({ + class: className, + style: getDisplayStates(gameState, currentOutcome)[className] ? {} : { display: "none" }, +}); const getCellHtml = ({ key, @@ -61,25 +71,15 @@ export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rul

- - Player X won - - - Player O won - - - Draw - + Player X won + Player O won + Draw

diff --git a/src/shared/display.ts b/src/shared/display.ts new file mode 100644 index 0000000..0a08d3e --- /dev/null +++ b/src/shared/display.ts @@ -0,0 +1,11 @@ +import { BoardgameStateType, CurrentOutcome } from "./datatypes.ts"; + +export const getDisplayStates = (gameState: BoardgameStateType, currentOutcome: CurrentOutcome) => ({ + "outcome-winx": currentOutcome === CurrentOutcome.WinX, + "outcome-wino": currentOutcome === CurrentOutcome.WinO, + "outcome-draw": currentOutcome === CurrentOutcome.Draw, + "start-new-game": !gameState.board, + "start-clear-game": gameState.board, +}); + +export type ClassNames = keyof ReturnType;