diff --git a/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/EditPartnershipInfoModal/EditPartnershipInfoModal.test.tsx b/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/EditPartnershipInfoModal/EditPartnershipInfoModal.test.tsx index 6fb629e48..8e1572f4e 100644 --- a/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/EditPartnershipInfoModal/EditPartnershipInfoModal.test.tsx +++ b/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/EditPartnershipInfoModal/EditPartnershipInfoModal.test.tsx @@ -18,6 +18,11 @@ import { ContactDonorAccountsFragment, ContactDonorAccountsFragmentDoc, } from '../../ContactDonationsTab.generated'; +import { UserOrganizationAccountsQuery } from '../PartnershipInfo.generated'; +import { + organizationAccountsMock, + organizationAccountsWithCruSwitzerlandMock, +} from '../PartnershipInfoMocks'; import { EditPartnershipInfoModal } from './EditPartnershipInfoModal'; jest.mock('notistack', () => ({ @@ -115,21 +120,29 @@ const newContactMock = gqlMock( interface ComponentsProps { isNewContact?: boolean; - showRelationshipCode?: boolean; + includeCruSwitzerland?: boolean; } const Components = ({ isNewContact = false, - showRelationshipCode = false, + includeCruSwitzerland = false, }: ComponentsProps) => ( - + + mocks={{ + UserOrganizationAccounts: includeCruSwitzerland + ? organizationAccountsWithCruSwitzerlandMock + : organizationAccountsMock, + }} + onCall={mutationSpy} + > @@ -559,11 +572,11 @@ describe('EditPartnershipInfoModal', () => { ).not.toBeInTheDocument(); }); - it('should render relationshipCode', () => { - const { getByRole } = render(); + it('should render relationshipCode', async () => { + const { findByRole } = render(); expect( - getByRole('textbox', { + await findByRole('textbox', { name: 'Relationship Code', }), ).toBeInTheDocument(); diff --git a/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/EditPartnershipInfoModal/EditPartnershipInfoModal.tsx b/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/EditPartnershipInfoModal/EditPartnershipInfoModal.tsx index fc24cd0a6..f292dca91 100644 --- a/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/EditPartnershipInfoModal/EditPartnershipInfoModal.tsx +++ b/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/EditPartnershipInfoModal/EditPartnershipInfoModal.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useMemo, useState } from 'react'; import InfoIcon from '@mui/icons-material/InfoOutlined'; import { Alert, @@ -46,6 +46,8 @@ import { useAccountListId } from '../../../../../../hooks/useAccountListId'; import { useApiConstants } from '../../../../../Constants/UseApiConstants'; import Modal from '../../../../../common/Modal/Modal'; import { ContactDonorAccountsFragment } from '../../ContactDonationsTab.generated'; +import { isApartOfSwitzerlandOrganization } from '../PartnershipInfo'; +import { useUserOrganizationAccountsQuery } from '../PartnershipInfo.generated'; import { useUpdateContactPartnershipMutation } from './EditPartnershipInfoModal.generated'; const ContactInputWrapper = styled(Box)(({ theme }) => ({ @@ -109,17 +111,17 @@ type Attributes = yup.InferType; interface EditPartnershipInfoModalProps { contact: ContactDonorAccountsFragment; - showRelationshipCode: boolean; handleClose: () => void; } export const EditPartnershipInfoModal: React.FC< EditPartnershipInfoModalProps -> = ({ contact, showRelationshipCode, handleClose }) => { +> = ({ contact, handleClose }) => { const { t } = useTranslation(); const { appName } = useGetAppSettings(); const accountListId = useAccountListId(); const constants = useApiConstants(); + const { enqueueSnackbar } = useSnackbar(); const { getLocalizedContactStatus, getLocalizedPledgeFrequency } = useLocalizedConstants(); @@ -127,18 +129,26 @@ export const EditPartnershipInfoModal: React.FC< const [showRemoveCommitmentWarning, setShowRemoveCommitmentWarning] = useState(false); - const { enqueueSnackbar } = useSnackbar(); + const { data } = useUserOrganizationAccountsQuery(); + const userOrganizationAccounts = data?.userOrganizationAccounts; + + const showRelationshipCode = useMemo( + () => isApartOfSwitzerlandOrganization(userOrganizationAccounts), + [userOrganizationAccounts], + ); const [updateContactPartnership, { loading: updating }] = useUpdateContactPartnershipMutation(); const pledgeCurrencies = constants?.pledgeCurrency; const onSubmit = async (attributes: Attributes) => { + // Remove and just use "attributes" when the UpdateContact mutation operation is updated to include the relationshipCode + const { relationshipCode: _, ...newAttributes } = attributes; await updateContactPartnership({ variables: { accountListId: accountListId ?? '', attributes: { - ...attributes, + ...newAttributes, pledgeStartDate: attributes.pledgeStartDate?.toISODate() ?? null, nextAsk: attributes.nextAsk?.toISODate() ?? null, primaryPersonId: attributes.primaryPersonId, diff --git a/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/PartnershipInfo.tsx b/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/PartnershipInfo.tsx index 4873c7a3e..f1748974a 100644 --- a/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/PartnershipInfo.tsx +++ b/src/components/Contacts/ContactDetails/ContactDonationsTab/PartnershipInfo/PartnershipInfo.tsx @@ -16,7 +16,10 @@ import { currencyFormat } from '../../../../../lib/intlFormat'; import { HandshakeIcon } from '../../ContactDetailsHeader/ContactHeaderSection/HandshakeIcon'; import { ContactDonorAccountsFragment } from '../ContactDonationsTab.generated'; import { EditPartnershipInfoModal } from './EditPartnershipInfoModal/EditPartnershipInfoModal'; -import { useUserOrganizationAccountsQuery } from './PartnershipInfo.generated'; +import { + UserOrganizationAccountsQuery, + useUserOrganizationAccountsQuery, +} from './PartnershipInfo.generated'; export const SwitzerlandOrganizationName = 'Campus fuer Christus Switzerland'; @@ -68,6 +71,17 @@ interface PartnershipInfoProp { contact: ContactDonorAccountsFragment | null; } +export const isApartOfSwitzerlandOrganization = ( + userOrganizationAccounts?: UserOrganizationAccountsQuery['userOrganizationAccounts'], +) => { + return ( + userOrganizationAccounts?.some( + (organizationAccount) => + organizationAccount.organization.name === SwitzerlandOrganizationName, + ) ?? false + ); +}; + export const PartnershipInfo: React.FC = ({ contact }) => { const { t } = useTranslation(); const locale = useLocale(); @@ -81,11 +95,7 @@ export const PartnershipInfo: React.FC = ({ contact }) => { const userOrganizationAccounts = data?.userOrganizationAccounts; const showRelationshipCode = useMemo( - () => - userOrganizationAccounts?.some( - (organizationAccount) => - organizationAccount.organization.name === SwitzerlandOrganizationName, - ) ?? false, + () => isApartOfSwitzerlandOrganization(userOrganizationAccounts), [userOrganizationAccounts], ); @@ -269,7 +279,6 @@ export const PartnershipInfo: React.FC = ({ contact }) => { {contact && editPartnershipModalOpen ? ( setEditPartnershipModalOpen(false)} - showRelationshipCode={showRelationshipCode} contact={contact} /> ) : null}