Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPDX-8237 - Shows tasks count, updates Tasks once deleted. #1088

Merged
merged 12 commits into from
Sep 26, 2024
20 changes: 20 additions & 0 deletions pages/accountLists/[accountListId]/tasks/Tasks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,24 @@ describe('tasks page', () => {
expect(getByText('All Tasks')).toHaveClass('MuiButton-contained'),
);
});

it('should remove task', async () => {
const { getByText, getByTestId } = render(
<MocksProviders>
<Tasks />
</MocksProviders>,
);
await waitFor(() => expect(getByText('Test Person')).toBeInTheDocument());
userEvent.click(getByTestId('task-checkbox-1'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're clicking this checkbox, you don't also have to check that it's in the document.

expect(getByText('Showing 17')).toBeInTheDocument();
userEvent.click(getByTestId('DeleteIconButton-1'));
// This is needed for some reason
canac marked this conversation as resolved.
Show resolved Hide resolved
userEvent.click(getByTestId('DeleteIconButton-1'));
userEvent.click(getByText('Yes'));
await waitFor(() => {
expect(mockEnqueue).toHaveBeenCalledWith(`Task deleted successfully`, {
variant: 'success',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ const TasksPage: React.FC = () => {
deselectAll,
toggleSelectAll,
toggleSelectionById,
deselectIds,
} = useMassSelection(
data?.tasks?.totalCount ?? 0,
allTaskIds,
Expand Down Expand Up @@ -344,6 +345,7 @@ const TasksPage: React.FC = () => {
toggleStarredFilter={setStarredFilter}
headerCheckboxState={selectionType}
massDeselectAll={deselectAll}
showShowingCount
caleballdrin marked this conversation as resolved.
Show resolved Hide resolved
selectedIds={ids}
buttonGroup={
<Hidden xsDown>
Expand Down Expand Up @@ -415,6 +417,7 @@ const TasksPage: React.FC = () => {
onTaskCheckToggle={toggleSelectionById}
isChecked={isRowChecked(task.id)}
useTopMargin={index === 0}
removeSelectedIds={deselectIds}
/>
</Box>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ interface DeleteTaskIconButtonProps {
accountListId: string;
taskId: string;
onDeleteConfirm?: () => void;
removeSelectedIds?: (id: string[]) => void;
}

export const DeleteTaskIconButton: React.FC<DeleteTaskIconButtonProps> = ({
accountListId,
taskId,
onDeleteConfirm,
removeSelectedIds,
}) => {
const { t } = useTranslation();

const [removeDialogOpen, setRemoveDialogOpen] = useState(false);

return (
<>
<DeleteButton onClick={() => setRemoveDialogOpen(true)}>
<DeleteButton
onClick={() => setRemoveDialogOpen(true)}
data-testid={`DeleteIconButton-${taskId}`}
>
<DeletedItemIcon />
</DeleteButton>
<DeleteConfirmation
Expand All @@ -36,6 +41,7 @@ export const DeleteTaskIconButton: React.FC<DeleteTaskIconButtonProps> = ({
onClickDecline={setRemoveDialogOpen}
accountListId={accountListId}
taskId={taskId}
removeSelectedIds={removeSelectedIds}
/>
</>
);
Expand Down
31 changes: 26 additions & 5 deletions src/components/Shared/Header/ListHeader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ContactFilterSetInput,
TaskFilterSetInput,
} from 'src/graphql/types.generated';
import { useAccountListId } from 'src/hooks/useAccountListId';
import i18n from 'src/lib/i18n';
import theme from '../../../theme';
import { TasksMassActionsDropdown } from '../MassActions/TasksMassActionsDropdown';
Expand Down Expand Up @@ -112,10 +111,6 @@ const router = {
push,
};

beforeEach(() => {
(useAccountListId as jest.Mock).mockReturnValue(router);
});

const ButtonGroup: React.FC = () => {
return (
<>
Expand Down Expand Up @@ -473,3 +468,29 @@ describe('ListHeader', () => {
expect(queryByText('Actions')).not.toBeInTheDocument();
});
});

describe('Counts', () => {
it('Should update count upon deletion', async () => {
const { queryByText, getByText } = render(
<Components
selectedIds={['a', 'b', 'c']}
page={PageEnum.Task}
totalItems={50}
showShowingCount={true}
/>,
);
expect(queryByText('Showing 50')).toBeInTheDocument();
expect(queryByText('3 Selected')).toBeInTheDocument();
const actionsButton = getByText('Actions');
userEvent.click(actionsButton);
expect(getByText('Delete Tasks')).toBeInTheDocument();
caleballdrin marked this conversation as resolved.
Show resolved Hide resolved
userEvent.click(getByText('Delete Tasks'));
await waitFor(() => {
expect(
queryByText('Are you sure you wish to delete the 3 selected tasks?'),
).toBeInTheDocument();
});
userEvent.click(getByText('Yes'));
expect(queryByText('Showing 50')).toBeInTheDocument();
});
});
4 changes: 4 additions & 0 deletions src/components/Task/TaskRow/TaskRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ interface TaskRowProps {
onContactSelected: (taskId: string) => void;
onTaskCheckToggle: (taskId: string) => void;
useTopMargin?: boolean;
removeSelectedIds?: (id: string[]) => void;
}

export const TaskRow: React.FC<TaskRowProps> = ({
Expand All @@ -75,6 +76,7 @@ export const TaskRow: React.FC<TaskRowProps> = ({
onContactSelected,
onTaskCheckToggle,
useTopMargin,
removeSelectedIds,
}) => {
const { t } = useTranslation();

Expand Down Expand Up @@ -166,6 +168,7 @@ export const TaskRow: React.FC<TaskRowProps> = ({
<Box padding="checkbox">
<Checkbox
checked={isChecked}
data-testid={`task-checkbox-${taskId}`}
color="secondary"
onClick={(event) => event.stopPropagation()}
onChange={() => onTaskCheckToggle(taskId)}
Expand Down Expand Up @@ -269,6 +272,7 @@ export const TaskRow: React.FC<TaskRowProps> = ({
<DeleteTaskIconButton
accountListId={accountListId}
taskId={taskId}
removeSelectedIds={removeSelectedIds}
/>
</Box>
<Box onClick={(e) => e.stopPropagation()}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface DeleteConfirmationProps {
accountListId?: string;
taskId?: string;
onClose?: () => void;
removeSelectedIds?: (id: string[]) => void;
}

export const DeleteConfirmation: React.FC<DeleteConfirmationProps> = ({
Expand All @@ -40,6 +41,7 @@ export const DeleteConfirmation: React.FC<DeleteConfirmationProps> = ({
accountListId,
taskId,
onClose,
removeSelectedIds,
}) => {
const { t } = useTranslation();
const { enqueueSnackbar } = useSnackbar();
Expand All @@ -56,12 +58,18 @@ export const DeleteConfirmation: React.FC<DeleteConfirmationProps> = ({
cache.evict({ id: `Task:${taskId}` });
cache.gc();
},
refetchQueries: ['ContactTasksTab', 'GetWeeklyActivity', 'GetThisWeek'],
refetchQueries: [
'ContactTasksTab',
'GetWeeklyActivity',
'GetThisWeek',
'Tasks',
],
});
enqueueSnackbar(t('Task deleted successfully'), { variant: 'success' });
onClickDecline(false);
onClose && onClose();
onClickConfirm && onClickConfirm();
removeSelectedIds && removeSelectedIds([taskId]);
}
};

Expand Down
22 changes: 22 additions & 0 deletions src/hooks/useMassSelection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,26 @@ describe('useMassSelection', () => {
expect(result.current.ids).toEqual(['1', '2', '3', '7', '8', '9', '10']);
});
});

describe('deselectIds()', () => {
it('should deselect ids', () => {
const { result, rerender } = renderHook(() =>
useMassSelection(
defaultTotalCount,
defaultIdsList,
defaultActiveFilters,
defaultWildcardSearch,
defaultStarredFilter,
),
);

result.current.toggleSelectAll();
rerender();
expect(result.current.ids).toEqual(defaultIdsList);

result.current.deselectIds(['7', '8', '9']);
rerender();
expect(result.current.ids).toEqual(['1', '2', '3', '4', '5', '6', '10']);
});
});
});
6 changes: 6 additions & 0 deletions src/hooks/useMassSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const useMassSelection = (
toggleSelectionById: (id: string) => void;
selectMultipleIds: (ids: string[]) => void;
deselectMultipleIds: (ids: string[]) => void;
deselectIds: (idsToRemove: string[]) => void;
} => {
const [selectionType, setSelectionType] = useState(
ListHeaderCheckBoxState.Unchecked,
Expand Down Expand Up @@ -93,6 +94,10 @@ export const useMassSelection = (
}
};

const deselectIds = (idsToRemove: string[]) => {
setIds(ids.filter((id) => !idsToRemove.includes(id)));
};

useEffect(() => {
switch (selectionType) {
case ListHeaderCheckBoxState.Checked:
Expand Down Expand Up @@ -121,5 +126,6 @@ export const useMassSelection = (
toggleSelectionById,
selectMultipleIds,
deselectMultipleIds,
deselectIds,
};
};
Loading