From 3d93641d1880970ad29b08df96ea262f7c1ae1f8 Mon Sep 17 00:00:00 2001 From: Harry Digos Date: Thu, 23 Jan 2025 19:26:01 +0200 Subject: [PATCH 1/4] fix: combine selected and filtered rules when deleting --- .../src/components/ManageRules.tsx | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/desktop-client/src/components/ManageRules.tsx b/packages/desktop-client/src/components/ManageRules.tsx index bae79319f3a..38374fbb87f 100644 --- a/packages/desktop-client/src/components/ManageRules.tsx +++ b/packages/desktop-client/src/components/ManageRules.tsx @@ -18,7 +18,10 @@ import * as undo from 'loot-core/src/platform/client/undo'; import { getNormalisedString } from 'loot-core/src/shared/normalisation'; import { mapField, friendlyOp } from 'loot-core/src/shared/rules'; import { describeSchedule } from 'loot-core/src/shared/schedules'; -import { type NewRuleEntity } from 'loot-core/src/types/models'; +import { + type RuleEntity, + type NewRuleEntity, +} from 'loot-core/src/types/models'; import { useAccounts } from '../hooks/useAccounts'; import { useCategories } from '../hooks/useCategories'; @@ -110,7 +113,7 @@ export function ManageRules({ payeeId, setLoading = () => {}, }: ManageRulesProps) { - const [allRules, setAllRules] = useState([]); + const [allRules, setAllRules] = useState([]); const [page, setPage] = useState(0); const [filter, setFilter] = useState(''); const dispatch = useDispatch(); @@ -197,10 +200,20 @@ export function ManageRules({ setPage(page => page + 1); } - async function onDeleteSelected() { + const rulesToDelete = useMemo( + () => + // @ts-ignore `intersection` is fully compatible with browser versions + selectedInst.items.intersection( + new Set(filteredRules.map(r => r.id)), + ) as Set, + [selectedInst, filteredRules], + ); + + const onDeleteSelected = useCallback(async () => { setLoading(true); + const { someDeletionsFailed } = await send('rule-delete-all', [ - ...selectedInst.items, + ...rulesToDelete, ]); if (someDeletionsFailed) { @@ -212,7 +225,7 @@ export function ManageRules({ await loadRules(); selectedInst.dispatch({ type: 'select-none' }); setLoading(false); - } + }, [rulesToDelete]); async function onDeleteRule(id: string) { setLoading(true); @@ -338,9 +351,9 @@ export function ManageRules({ }} > - {selectedInst.items.size > 0 && ( + {rulesToDelete.size > 0 && ( )}