Skip to content

Commit

Permalink
Add logic to check if sources are from third party
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-bizz committed Dec 5, 2024
1 parent 3a61a72 commit 78d761a
Showing 1 changed file with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import {
CircularProgress,
DialogActions,
Expand All @@ -13,6 +13,8 @@ import {
} from 'src/components/common/Modal/ActionButtons/ActionButtons';
import Modal from 'src/components/common/Modal/Modal';
import { useAccountListId } from 'src/hooks/useAccountListId';
import { isEditableSource } from 'src/utils/sourceHelper';
import { useContactSourceQuery } from './ContactSource.generated';

const LoadingIndicator = styled(CircularProgress)(({ theme }) => ({
margin: theme.spacing(0, 1, 0, 0),
Expand All @@ -35,11 +37,59 @@ export const DeleteContactModal: React.FC<DeleteContactModalProps> = ({
}) => {
const { t } = useTranslation();
const accountListId = useAccountListId() ?? '';
const [canDelete, setCanDelete] = useState(true);

const { data } = useContactSourceQuery({
variables: { accountListId, contactId },
skip: !open && !contactId,
});
const contactSources = data?.contact;

useEffect(() => {
if (!contactSources) {
return;
}
// We ensure the contact was created on MPDX and that all the data is editable.
// If any data is not editable, this means it was created by a third party.
// Which will only recreate the data after deleting it on MPDX.
// To prevent this confusion, we do not allow a contact to be deleted if it has non editable data.

const isContactNonEditable = isEditableSource(contactSources.source ?? '');

const isAddressNonEditable = contactSources.addresses?.nodes.some(
(address) => !isEditableSource(address.source ?? ''),
);

const hasNonEditablePersonData = contactSources.people?.nodes?.map(
(people) => {
const foundNonEditableEmailAddress = people.emailAddresses.nodes.some(
(email) => !isEditableSource(email.source),
);
const foundNonEditablePhone = people.phoneNumbers.nodes.some(
(phone) => !isEditableSource(phone.source),
);
return {
foundNonEditableEmailAddress,
foundNonEditablePhone,
};
},
);
const isPersonNonEditable = hasNonEditablePersonData.some((person) => {
return (
person.foundNonEditableEmailAddress || person.foundNonEditablePhone
);
});

const contactIsNotEditable =
isContactNonEditable || isAddressNonEditable || isPersonNonEditable;

if (contactIsNotEditable) {
setCanDelete(false);
} else {
setCanDelete(true);
}
}, [contactSources]);

return (
<Modal
isOpen={open}
Expand Down

0 comments on commit 78d761a

Please sign in to comment.