Skip to content

Commit

Permalink
Confirm all contact's addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-bizz committed Jun 25, 2024
1 parent 945772d commit 9ea79bf
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/components/Tool/FixMailingAddresses/FixMailingAddresses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { makeStyles } from 'tss-react/mui';
import { SetContactFocus } from 'pages/accountLists/[accountListId]/tools/useToolsHelper';
import { DynamicAddAddressModal } from 'src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/AddAddressModal/DynamicAddAddressModal';
import { DynamicEditContactAddressModal } from 'src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/EditContactAddressModal/DynamicEditContactAddressModal';
import { Confirmation } from 'src/components/common/Modal/Confirmation/Confirmation';
import theme from '../../../theme';
import NoData from '../NoData';
import Contact from './Contact';
Expand Down Expand Up @@ -140,6 +141,8 @@ const FixSendNewsletter: React.FC<Props> = ({
const [selectedAddress, setSelectedAddress] = useState(emptyAddress);
const [selectedContactId, setSelectedContactId] = useState('');
const [defaultSource, setDefaultSource] = useState(appName);
const [openBulkConfirmModal, setOpenBulkConfirmModal] = useState(false);

const { data, loading } = useInvalidAddressesQuery({
variables: { accountListId },
});
Expand Down Expand Up @@ -200,6 +203,62 @@ const FixSendNewsletter: React.FC<Props> = ({
}
};

const handleBulkConfirm = async () => {
try {
const callsByContact: (() => Promise<{ success: boolean }>)[] = [];
data?.contacts?.nodes.forEach((contact) => {
const primaryAddress = contact.addresses.nodes.find(
(address) =>
address.source === defaultSource ||
(defaultSource === appName && address.source === 'MPDX'),
);
if (primaryAddress) {
const addresses: ContactAddressFragment[] = [];
contact.addresses.nodes.forEach((address) => {
addresses.push({
...address,
primaryMailingAddress: address.id === primaryAddress?.id,
});
});
const callContactMutation = () =>
handleSingleConfirm({
addresses,
id: contact.id,
name: contact.name,
onlyErrorOnce: true,
});
callsByContact.push(callContactMutation);
}
});

if (callsByContact.length) {
const results = await Promise.all(callsByContact.map((call) => call()));

const failedUpdates = results.filter(
(result) => !result.success,
).length;
const successfulUpdates = results.length - failedUpdates;

if (successfulUpdates) {
enqueueSnackbar(t(`Updated ${successfulUpdates} contact(s)`), {
variant: 'success',
});
}
if (failedUpdates) {
enqueueSnackbar(
t(`Error when updating ${failedUpdates} contact(s)`),
{
variant: 'error',
},
);
}
} else {
enqueueSnackbar(t(`No contacts were updated`), { variant: 'warning' });
}
} catch (error) {
enqueueSnackbar(t(`Error updating contacts`), { variant: 'error' });
}
};

const handleUpdateCacheForDeleteAddress = useCallback(
(cache: ApolloCache<unknown>, data) => {
Expand Down Expand Up @@ -279,6 +338,10 @@ const FixSendNewsletter: React.FC<Props> = ({
setDefaultSource(event.target.value);
};

const handleBulkConfirmModalClose = () => {
setOpenBulkConfirmModal(false);
};

const totalContacts = data?.contacts?.nodes?.length || 0;

return (
Expand Down Expand Up @@ -339,6 +402,7 @@ const FixSendNewsletter: React.FC<Props> = ({
<Button
variant="contained"
className={classes.confirmAllButton}
onClick={() => setOpenBulkConfirmModal(true)}
>
<Icon
path={mdiCheckboxMarkedCircle}
Expand Down Expand Up @@ -408,6 +472,18 @@ const FixSendNewsletter: React.FC<Props> = ({
handleUpdateCache={handleUpdateCacheForAddAddress}
/>
)}
{openBulkConfirmModal && (
<Confirmation
isOpen={true}
title={t('Confirm')}
message={t(
`You are updating all contacts visible on this page, setting the first {{source}} address as the primary address. If no such address exists the contact will not be updated. Are you sure you want to do this?`,
{ source: defaultSource },
)}
handleClose={handleBulkConfirmModalClose}
mutation={handleBulkConfirm}
/>
)}
</Box>
);
};
Expand Down

0 comments on commit 9ea79bf

Please sign in to comment.