Skip to content

Commit

Permalink
feat: grey out undo button when there is no more undo
Browse files Browse the repository at this point in the history
  • Loading branch information
TN1ck committed Aug 24, 2024
1 parent 4ec28b7 commit 7020913
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/components/pages/Game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,33 @@ const sudokuMenuNumbersConnector = connect(
);
const SudokuMenuNumbersConnected = sudokuMenuNumbersConnector(SudokuMenuNumbers);

function UndoButton({state, undoAction}: {state: GameStateMachine; undoAction: () => void}) {
const undoButtonConnector = connect(
(state: RootState) => {
return {
state: state.game.state,
sudoku: state.sudoku,
};
},
{
undo,
},
);

type UndoButtonProps = ConnectedProps<typeof undoButtonConnector>;

const UndoButton: React.FC<UndoButtonProps> = ({state, undo, sudoku}) => {
const canUndo = sudoku.historyIndex < sudoku.history.length - 1;
return (
<Button disabled={state === GameStateMachine.wonGame || state === GameStateMachine.paused} onClick={undoAction}>
<Button
disabled={state === GameStateMachine.wonGame || state === GameStateMachine.paused || !canUndo}
onClick={undo}
>
{"Undo"}
</Button>
);
}
};

const ConnectedUndoButton = undoButtonConnector(UndoButton);

function PauseButton({
state,
Expand Down Expand Up @@ -197,7 +217,6 @@ const connector = connect(
toggleShowCircleMenu,
toggleShowWrongEntries,
toggleShowConflicts,
undo,
},
);

Expand Down Expand Up @@ -238,7 +257,7 @@ class Game extends React.Component<PropsFromRedux> {
};

render() {
const {game, application, pauseGame, continueGame, chooseGame, sudoku, undo} = this.props;
const {game, application, pauseGame, continueGame, chooseGame, sudoku} = this.props;
const pausedGame = game.state === GameStateMachine.paused;
const activeCell = game.activeCellCoordinates
? sudoku.find((s) => {
Expand All @@ -264,7 +283,7 @@ class Game extends React.Component<PropsFromRedux> {
<PauseButton state={game.state} continueGame={continueGame} pauseGame={pauseGame} />
</div>
<div>
<UndoButton state={game.state} undoAction={undo} />
<ConnectedUndoButton />
</div>
</div>
</GameHeaderArea>
Expand Down

0 comments on commit 7020913

Please sign in to comment.