diff --git a/src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/AddAddressModal/AddAddressModal.tsx b/src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/AddAddressModal/AddAddressModal.tsx index d4fdc61a6..b7a7a930f 100644 --- a/src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/AddAddressModal/AddAddressModal.tsx +++ b/src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/AddAddressModal/AddAddressModal.tsx @@ -23,6 +23,7 @@ import { SubmitButton, } from 'src/components/common/Modal/ActionButtons/ActionButtons'; import { AddressCreateInput } from 'src/graphql/types.generated'; +import { useUpdateCache } from 'src/hooks/useUpdateCache'; import Modal from '../../../../../common/Modal/Modal'; import { ContactDetailsTabDocument, @@ -34,7 +35,6 @@ import { } from '../AddressLocation'; import { useSetContactPrimaryAddressMutation } from '../SetPrimaryAddress.generated'; import { StreetAutocomplete } from '../StreetAutocomplete/StreetAutocomplete'; -import { useUpdateCache } from '../useUpdateCache'; import { useCreateContactAddressMutation } from './CreateContactAddress.generated'; import { createAddressSchema } from './createAddressSchema'; diff --git a/src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/EditContactAddressModal/EditContactAddressModal.tsx b/src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/EditContactAddressModal/EditContactAddressModal.tsx index 698a5d6bf..af60c3bbe 100644 --- a/src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/EditContactAddressModal/EditContactAddressModal.tsx +++ b/src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/EditContactAddressModal/EditContactAddressModal.tsx @@ -27,6 +27,7 @@ import { SubmitButton, } from 'src/components/common/Modal/ActionButtons/ActionButtons'; import { AddressUpdateInput } from 'src/graphql/types.generated'; +import { useUpdateCache } from 'src/hooks/useUpdateCache'; import Modal from '../../../../../common/Modal/Modal'; import { ContactDetailsTabDocument, @@ -39,7 +40,6 @@ import { import { ContactMailingFragment } from '../ContactMailing.generated'; import { useSetContactPrimaryAddressMutation } from '../SetPrimaryAddress.generated'; import { StreetAutocomplete } from '../StreetAutocomplete/StreetAutocomplete'; -import { useUpdateCache } from '../useUpdateCache'; import { useDeleteContactAddressMutation, useDonationServicesEmailQuery, diff --git a/src/components/Tool/FixMailingAddresses/Contact.tsx b/src/components/Tool/FixMailingAddresses/Contact.tsx index 9b57119a5..41132ed8e 100644 --- a/src/components/Tool/FixMailingAddresses/Contact.tsx +++ b/src/components/Tool/FixMailingAddresses/Contact.tsx @@ -11,6 +11,7 @@ import { Card, CardContent, CardHeader, + CircularProgress, Grid, Hidden, IconButton, @@ -18,9 +19,11 @@ import { } from '@mui/material'; import clsx from 'clsx'; import { DateTime } from 'luxon'; +import { useSnackbar } from 'notistack'; import { useTranslation } from 'react-i18next'; import { makeStyles } from 'tss-react/mui'; import { editableSources } from 'src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/EditContactAddressModal/EditContactAddressModal'; +import { useSetContactPrimaryAddressMutation } from 'src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/SetPrimaryAddress.generated'; import { AddButton, AddIcon, @@ -29,6 +32,7 @@ import { LockIcon, } from 'src/components/Contacts/ContactDetails/ContactDetailsTab/StyledComponents'; import { useLocale } from 'src/hooks/useLocale'; +import { useUpdateCache } from 'src/hooks/useUpdateCache'; import { dateFormatShort } from 'src/lib/intlFormat'; import { contactPartnershipStatus } from 'src/utils/contacts/contactPartnershipStatus'; import theme from '../../../theme'; @@ -122,10 +126,35 @@ const Contact: React.FC = ({ }) => { const { t } = useTranslation(); const locale = useLocale(); + const { enqueueSnackbar } = useSnackbar(); const { classes } = useStyles(); const newAddress = { ...emptyAddress, newAddress: true }; - //TODO: Add button functionality - //TODO: Make contact name a link to contact page + const [setContactPrimaryAddress, { loading: settingPrimaryAddress }] = + useSetContactPrimaryAddressMutation(); + const { update } = useUpdateCache(id); + + const handleSetPrimaryContact = async (address: ContactAddressFragment) => { + await setContactPrimaryAddress({ + variables: { + contactId: id, + primaryAddressId: address.primaryMailingAddress ? null : address.id, + }, + update, + onCompleted: () => { + enqueueSnackbar(t('Mailing information edited successfully'), { + variant: 'success', + }); + }, + onError: () => { + enqueueSnackbar( + t('Error occurred while updating mailing information'), + { + variant: 'error', + }, + ); + }, + }); + }; return ( @@ -191,13 +220,30 @@ const Contact: React.FC = ({ - - {address.primaryMailingAddress ? ( - - ) : ( - - )} - + {!settingPrimaryAddress && ( + handleSetPrimaryContact(address)} + > + {address.primaryMailingAddress ? ( + + ) : ( + + )} + + )} + {settingPrimaryAddress && ( + + )} diff --git a/src/components/Tool/FixMailingAddresses/FixMailingAddresses.test.tsx b/src/components/Tool/FixMailingAddresses/FixMailingAddresses.test.tsx index 21d007b42..72ac5fc2a 100644 --- a/src/components/Tool/FixMailingAddresses/FixMailingAddresses.test.tsx +++ b/src/components/Tool/FixMailingAddresses/FixMailingAddresses.test.tsx @@ -358,4 +358,42 @@ describe('FixSendNewsletter', () => { ); }, 10000); }); + describe('Set primary mailing address', () => { + it('should set the address as primary', async () => { + const { getByTestId, getAllByTestId, queryByTestId, queryAllByTestId } = + render( + , + ); + await waitFor(() => + expect(queryByTestId('loading')).not.toBeInTheDocument(), + ); + + const primaryAddress = getByTestId('primaryContactStarIcon'); + const secondaryAddresses = getAllByTestId('contactStarIcon'); + + expect(primaryAddress).toBeInTheDocument(); + expect(secondaryAddresses.length).toBe(2); + + expect(queryAllByTestId('settingPrimaryAddress').length).toBe(0); + + userEvent.click(secondaryAddresses[0]); + + expect(getAllByTestId('settingPrimaryAddress').length).toBe(3); + + await waitFor(() => + expect(mockEnqueue).toHaveBeenCalledWith( + 'Mailing information edited successfully', + { + variant: 'success', + }, + ), + ); + }); + }); }); diff --git a/src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/useUpdateCache.ts b/src/hooks/useUpdateCache.ts similarity index 93% rename from src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/useUpdateCache.ts rename to src/hooks/useUpdateCache.ts index f8af3a1f2..30909e406 100644 --- a/src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/useUpdateCache.ts +++ b/src/hooks/useUpdateCache.ts @@ -3,7 +3,7 @@ import { ContactPrimaryAddressRelationFragmentDoc, PrimaryMailingAddressFragmentDoc, SetContactPrimaryAddressMutation, -} from './SetPrimaryAddress.generated'; +} from 'src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/SetPrimaryAddress.generated'; // This hook provides an Apollo cache update function for the setContactPrimaryAddress mutation // used by the add address and edit address modals