From fd5089be4c57201a03d5d5097f6ed902965f79ca Mon Sep 17 00:00:00 2001 From: Xanh Date: Mon, 14 Oct 2024 14:39:03 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20delete=20rounds=20when=20deleting?= =?UTF-8?q?=20a=20game?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/home.tsx | 6 +++++- src/redux/round.slice.ts | 11 ++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/pages/home.tsx b/src/pages/home.tsx index c367e26..a047c6d 100644 --- a/src/pages/home.tsx +++ b/src/pages/home.tsx @@ -6,6 +6,7 @@ import { IconInfoSquareRounded, IconTrash } from '@tabler/icons-react'; import { useAppDispatch, useAppSelector } from '../redux/store'; import { selectAllGames } from '../redux/game.selector'; import { deleteGame } from '../redux/game.slice'; +import { deleteGameRounds } from '../redux/round.slice'; import { formatDateTime } from '../utils/helpers'; import TopNav from '../components/shared/TopNav'; @@ -28,7 +29,10 @@ function HomePage() { withCloseButton: false, centered: true, labels: { confirm: 'Oke', cancel: 'Thoi' }, - onConfirm: () => dispatch(deleteGame(gameId)), + onConfirm: () => { + dispatch(deleteGame(gameId)); + dispatch(deleteGameRounds(gameId)); + }, }); }; diff --git a/src/redux/round.slice.ts b/src/redux/round.slice.ts index edb5c99..f276859 100644 --- a/src/redux/round.slice.ts +++ b/src/redux/round.slice.ts @@ -38,8 +38,17 @@ const roundSlice = createSlice({ }); }, deleteRound: roundAdaptor.removeOne, + deleteGameRounds: (state, action: PayloadAction) => { + const roundIds = roundAdaptor + .getSelectors() + .selectAll(state) + .filter((round) => round.gameId === action.payload) + .map((round) => round.id); + roundAdaptor.removeMany(state, roundIds); + }, }, }); -export const { createRound, updateRound, deleteRound } = roundSlice.actions; +export const { createRound, updateRound, deleteRound, deleteGameRounds } = + roundSlice.actions; export default roundSlice;