Skip to content

Commit

Permalink
Adds confirm all to fix phone numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
wjames111 committed Aug 7, 2024
1 parent e4b7269 commit 07b5449
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 51 deletions.
88 changes: 66 additions & 22 deletions src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import { ApolloCache, InMemoryCache } from '@apollo/client';
import { ThemeProvider } from '@mui/material/styles';
import userEvent from '@testing-library/user-event';
import { ErgonoMockShape } from 'graphql-ergonomock';
import { SnackbarProvider } from 'notistack';
import TestRouter from '__tests__/util/TestRouter';
import TestWrapper from '__tests__/util/TestWrapper';
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
Expand Down Expand Up @@ -73,30 +75,46 @@ const testData: ErgonoMockShape[] = [
},
];

const Components: React.FC<{ data?: ErgonoMockShape[] }> = ({
data = testData,
}) => (
const mockEnqueue = jest.fn();
jest.mock('notistack', () => ({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
...jest.requireActual('notistack'),
useSnackbar: () => {
return {
enqueueSnackbar: mockEnqueue,
};
},
}));

const Components: React.FC<{
data?: ErgonoMockShape[];
cache?: ApolloCache<object>;
}> = ({ data = testData, cache }) => (
<ThemeProvider theme={theme}>
<TestRouter router={router}>
<TestWrapper>
<GqlMockedProvider<{
GetInvalidPhoneNumbers: GetInvalidPhoneNumbersQuery;
}>
mocks={{
GetInvalidPhoneNumbers: {
people: {
nodes: data,
<SnackbarProvider>
<TestRouter router={router}>
<TestWrapper>
<GqlMockedProvider<{
GetInvalidPhoneNumbers: GetInvalidPhoneNumbersQuery;
}>
mocks={{
GetInvalidPhoneNumbers: {
people: {
nodes: data,
},
},
},
}}
>
<FixPhoneNumbers
accountListId={accountListId}
setContactFocus={setContactFocus}
/>
</GqlMockedProvider>
</TestWrapper>
</TestRouter>
}}
cache={cache}
>
<FixPhoneNumbers
accountListId={accountListId}
setContactFocus={setContactFocus}
/>
</GqlMockedProvider>
</TestWrapper>
</TestRouter>
</SnackbarProvider>
</ThemeProvider>
);

Expand Down Expand Up @@ -214,4 +232,30 @@ describe('FixPhoneNumbers-Home', () => {
).not.toBeInTheDocument();
});
});
it('should bulk confirm all phone numbers', async () => {
const cache = new InMemoryCache();

const { getByTestId, queryByTestId, getByText } = render(
<Components cache={cache} />,
);
await waitFor(() => {
expect(queryByTestId('loading')).not.toBeInTheDocument();
expect(getByTestId('starOutlineIcon-testid-1')).toBeInTheDocument();
});

userEvent.click(getByTestId(`starOutlineIcon-testid-1`));

const confirmAllButton = getByTestId('source-button');
userEvent.click(confirmAllButton);

await waitFor(() => {
expect(mockEnqueue).toHaveBeenCalledWith(`Phone numbers updated!`, {
variant: 'success',
autoHideDuration: 7000,
});
expect(
getByText('No people with phone numbers need attention'),
).toBeVisible();
});
});
});
95 changes: 67 additions & 28 deletions src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useEffect, useState } from 'react';
import { useApolloClient } from '@apollo/client';
import { mdiCheckboxMarkedCircle } from '@mdi/js';
import Icon from '@mdi/react';
import {
Expand All @@ -15,17 +14,16 @@ import { useSnackbar } from 'notistack';
import { Trans, useTranslation } from 'react-i18next';
import { makeStyles } from 'tss-react/mui';
import { SetContactFocus } from 'pages/accountLists/[accountListId]/tools/useToolsHelper';
import { PersonPhoneNumberInput } from 'src/graphql/types.generated';
import {
PersonPhoneNumberInput,
PersonUpdateInput,
} from 'src/graphql/types.generated';
import theme from '../../../theme';
import NoData from '../NoData';
import { StyledInput } from '../StyledInput';
import Contact from './Contact';
import DeleteModal from './DeleteModal';
import {
GetInvalidPhoneNumbersDocument,
GetInvalidPhoneNumbersQuery,
useGetInvalidPhoneNumbersQuery,
} from './GetInvalidPhoneNumbers.generated';
import { useGetInvalidPhoneNumbersQuery } from './GetInvalidPhoneNumbers.generated';
import { useUpdateInvalidPhoneNumbersMutation } from './UpdateInvalidPhoneNumbers.generated';

const useStyles = makeStyles()(() => ({
Expand Down Expand Up @@ -131,7 +129,6 @@ const FixPhoneNumbers: React.FC<Props> = ({
}: Props) => {
const { classes } = useStyles();
const { enqueueSnackbar } = useSnackbar();
const client = useApolloClient();
const [defaultSource, setDefaultSource] = useState('MPDX');
const [deleteModalState, setDeleteModalState] = useState<ModalState>(
defaultDeleteModalState,
Expand Down Expand Up @@ -173,6 +170,34 @@ const FixPhoneNumbers: React.FC<Props> = ({
[loading],
);

const determineBulkDataToSend = (
dataState: { [key: string]: PersonPhoneNumbers },
defaultSource: string,
): PersonUpdateInput[] => {
const dataToSend = [] as PersonUpdateInput[];

Object.entries(dataState).forEach((value) => {
const primaryNumber = value[1].phoneNumbers.find(
(number) => number.source === defaultSource,
);
if (primaryNumber) {
dataToSend.push({
id: value[0],
phoneNumbers: value[1].phoneNumbers.map(
(number) =>
({
id: number.id,
primary: number.id === primaryNumber.id,
number: number.number,
validValues: true,
} as PersonPhoneNumberInput),
),
});
}
});
return dataToSend;
};

const handleDeleteModalOpen = (
personId: string,
numberIndex: number,
Expand Down Expand Up @@ -256,6 +281,7 @@ const FixPhoneNumbers: React.FC<Props> = ({
id: phoneNumber.id,
primary: phoneNumber.primary,
number: phoneNumber.number,
validValues: true,
})),
id: personId,
},
Expand All @@ -267,6 +293,9 @@ const FixPhoneNumbers: React.FC<Props> = ({
attributes,
},
},
update: (cache) => {
cache.evict({ id: `Person:${personId}` });
},
onError() {
enqueueSnackbar(t(`Error updating ${name}'s phone numbers`), {
variant: 'error',
Expand All @@ -278,33 +307,42 @@ const FixPhoneNumbers: React.FC<Props> = ({
variant: 'success',
autoHideDuration: 7000,
});
hideContactFromView(personId);
},
});
};

const hideContactFromView = (hideId: string): void => {
const query = {
query: GetInvalidPhoneNumbersDocument,
variables: {
accountListId,
},
};
const handleBulkConfirm = async () => {
const dataToSend = determineBulkDataToSend(dataState, defaultSource ?? '');

const dataFromCache = client.readQuery<GetInvalidPhoneNumbersQuery>(query);
if (!dataToSend.length) {
return;

Check warning on line 318 in src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx#L318

Added line #L318 was not covered by tests
}

if (dataFromCache) {
const data = {
...dataFromCache,
people: {
...dataFromCache.people,
nodes: dataFromCache.people.nodes.filter(
(person) => person.id !== hideId,
),
await updateInvalidPhoneNumbers({
variables: {
input: {
accountListId,
attributes: dataToSend,
},
};
client.writeQuery({ ...query, data });
}
},
update: (cache) => {
data?.people.nodes.forEach((person) => {
cache.evict({ id: `Person:${person.id}` });
});
},
onError: () => {
enqueueSnackbar(t(`Error updating phone numbers`), {

Check warning on line 334 in src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx#L333-L334

Added lines #L333 - L334 were not covered by tests
variant: 'error',
autoHideDuration: 7000,
});
},
onCompleted: () => {
enqueueSnackbar(t(`Phone numbers updated!`), {
variant: 'success',
autoHideDuration: 7000,
});
},
});
};

return (
Expand Down Expand Up @@ -361,6 +399,7 @@ const FixPhoneNumbers: React.FC<Props> = ({
</NativeSelect>
<Button
className={classes.buttonBlue}
onClick={handleBulkConfirm}
data-testid="source-button"
>
<Icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ mutation UpdateInvalidPhoneNumbers($input: PeopleUpdateMutationInput!) {
people {
phoneNumbers {
nodes {
source
number
validValues
source
id
primary
}
Expand Down

0 comments on commit 07b5449

Please sign in to comment.