Skip to content

Commit

Permalink
Deselect removed ids
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Oct 23, 2024
1 parent 5ade3d4 commit beaeb08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
11 changes: 10 additions & 1 deletion pages/accountLists/[accountListId]/tasks/Tasks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import userEvent from '@testing-library/user-event';
import { VirtuosoMockContext } from 'react-virtuoso';
import TestRouter from '__tests__/util/TestRouter';
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
import { GetTaskIdsForMassSelectionQuery } from 'src/hooks/GetIdsForMassSelection.generated';
import useTaskModal from 'src/hooks/useTaskModal';
import { dispatch } from 'src/lib/analytics';
import theme from 'src/theme';
Expand Down Expand Up @@ -141,7 +142,10 @@ describe('tasks page', () => {
it('should dispatch one analytics event per task', async () => {
const { getAllByTestId, getByRole, findByRole } = render(
<MocksProviders>
<GqlMockedProvider<{ Tasks: TasksQuery }>
<GqlMockedProvider<{
Tasks: TasksQuery;
GetTaskIdsForMassSelection: GetTaskIdsForMassSelectionQuery;
}>
mocks={{
Tasks: {
tasks: {
Expand All @@ -154,6 +158,11 @@ describe('tasks page', () => {
pageInfo: { hasNextPage: false },
},
},
GetTaskIdsForMassSelection: {
tasks: {
nodes: [{ id: '1' }, { id: '2' }, { id: '3' }],
},
},
}}
>
<Tasks />
Expand Down
16 changes: 15 additions & 1 deletion src/hooks/useMassSelection.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderHook } from '@testing-library/react';
import { ListHeaderCheckBoxState } from 'src/components/Shared/Header/ListHeader';
import { useMassSelection } from './useMassSelection';
import { UseMassSelectionResult, useMassSelection } from './useMassSelection';

const defaultIdsList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];

Expand Down Expand Up @@ -117,4 +117,18 @@ describe('useMassSelection', () => {
expect(result.current.ids).toEqual(['1', '2', '3', '7', '8', '9', '10']);
});
});

it('deselects removed ids', () => {
const { result, rerender } = renderHook<
UseMassSelectionResult,
{ ids: string[] }
>(({ ids }) => useMassSelection(ids), {
initialProps: { ids: defaultIdsList },
});

result.current.selectMultipleIds(['1', '2', '3']);

rerender({ ids: ['2', '3', '4'] });
expect(result.current.ids).toEqual(['2', '3']);
});
});
15 changes: 10 additions & 5 deletions src/hooks/useMassSelection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { ListHeaderCheckBoxState } from '../components/Shared/Header/ListHeader';

export const useMassSelection = (
idsList: string[],
): {
export interface UseMassSelectionResult {
ids: string[];
selectionType: ListHeaderCheckBoxState;
isRowChecked: (id: string) => boolean;
Expand All @@ -12,11 +10,18 @@ export const useMassSelection = (
toggleSelectionById: (id: string) => void;
selectMultipleIds: (ids: string[]) => void;
deselectMultipleIds: (ids: string[]) => void;
} => {
}

export const useMassSelection = (idsList: string[]): UseMassSelectionResult => {
const totalCount = idsList.length;

const [ids, setIds] = useState<string[]>([]);

// When the idsList change, deselect any ids that were removed
useEffect(() => {
setIds((previousIds) => previousIds.filter((id) => idsList.includes(id)));
}, [idsList]);

const toggleSelectionById = (id: string) => {
if (ids.includes(id)) {
setIds((previousIds) =>
Expand Down

0 comments on commit beaeb08

Please sign in to comment.