Skip to content

Commit 4b61caf

Browse files
authored
Merge pull request #1313 from CruGlobal/MPDX-8449-delete-appeal-contact
MPDX-8449 - deleteAppealContact mutation FE fixes
2 parents 43fe02b + 9802cab commit 4b61caf

File tree

3 files changed

+10
-86
lines changed

3 files changed

+10
-86
lines changed

src/components/Tool/Appeal/Modals/DeleteAppealContact/DeleteAppealContact.graphql

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,3 @@ mutation DeleteAppealContact($input: AppealContactDeleteMutationInput!) {
33
id
44
}
55
}
6-
7-
query AppealContacts($appealId: ID!, $after: String) {
8-
appealContacts(appealId: $appealId, first: 50, after: $after) {
9-
nodes {
10-
...AppealContactsInfo
11-
}
12-
pageInfo {
13-
hasNextPage
14-
endCursor
15-
}
16-
}
17-
}
18-
19-
fragment AppealContactsInfo on AppealContact {
20-
id
21-
contact {
22-
id
23-
}
24-
}

src/components/Tool/Appeal/Modals/DeleteAppealContact/DeleteAppealContactModal.test.tsx

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -188,25 +188,9 @@ describe('DeleteAppealContactModal', () => {
188188
expect(handleClose).toHaveBeenCalledTimes(2);
189189
});
190190

191-
it('fetches all the appealContacts and matches up the correct ID to send to the API', async () => {
191+
it('should successfully delete a contact from an appeal', async () => {
192192
const { getByRole } = render(<Components />);
193193

194-
// Call AppealContacts 3 times getting all contacts.
195-
await waitFor(() =>
196-
expect(mutationSpy).toHaveGraphqlOperation('AppealContacts', {
197-
after: 'endCursor2',
198-
appealId: 'appealId',
199-
}),
200-
);
201-
expect(mutationSpy).toHaveGraphqlOperation('AppealContacts', {
202-
after: null,
203-
appealId: 'appealId',
204-
});
205-
expect(mutationSpy).toHaveGraphqlOperation('AppealContacts', {
206-
after: 'endCursor1',
207-
appealId: 'appealId',
208-
});
209-
210194
userEvent.click(getByRole('button', { name: 'Yes' }));
211195

212196
await waitFor(() => {
@@ -221,7 +205,8 @@ describe('DeleteAppealContactModal', () => {
221205
await waitFor(() => {
222206
expect(mutationSpy).toHaveGraphqlOperation('DeleteAppealContact', {
223207
input: {
224-
id: 'appealContactId',
208+
contactId: 'contact-1',
209+
appealId: 'appealId',
225210
},
226211
});
227212
});

src/components/Tool/Appeal/Modals/DeleteAppealContact/DeleteAppealContactModal.tsx

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React from 'react';
22
import {
33
Box,
44
CircularProgress,
@@ -19,11 +19,7 @@ import {
1919
AppealsType,
2020
TableViewModeEnum,
2121
} from '../../AppealsContext/AppealsContext';
22-
import {
23-
AppealContactsInfoFragment,
24-
useAppealContactsQuery,
25-
useDeleteAppealContactMutation,
26-
} from './DeleteAppealContact.generated';
22+
import { useDeleteAppealContactMutation } from './DeleteAppealContact.generated';
2723

2824
const LoadingIndicator = styled(CircularProgress)(({ theme }) => ({
2925
margin: theme.spacing(0, 1, 0, 0),
@@ -44,48 +40,9 @@ export const DeleteAppealContactModal: React.FC<
4440
) as AppealsType;
4541
const [deleteAppealContact, { loading: mutating }] =
4642
useDeleteAppealContactMutation();
47-
const { data, fetchMore } = useAppealContactsQuery({
48-
variables: {
49-
appealId: appealId ?? '',
50-
},
51-
});
52-
const [loading, setLoading] = useState(false);
53-
const [appealContactsIds, setAppealContactsIds] = useState<
54-
AppealContactsInfoFragment[]
55-
>([]);
56-
57-
const loadAllAppealContacts = async () => {
58-
let allContacts = data?.appealContacts.nodes ?? [];
59-
let hasNextPage = true;
60-
let cursor: string | null = null;
61-
62-
while (hasNextPage) {
63-
const response = await fetchMore({
64-
variables: {
65-
after: cursor,
66-
},
67-
});
68-
69-
const newContacts = response.data.appealContacts.nodes;
70-
allContacts = [...allContacts, ...newContacts];
71-
hasNextPage = response.data.appealContacts.pageInfo.hasNextPage;
72-
cursor = response.data.appealContacts.pageInfo.endCursor ?? null;
73-
}
74-
75-
setAppealContactsIds(allContacts);
76-
setLoading(false);
77-
return allContacts;
78-
};
79-
80-
useEffect(() => {
81-
loadAllAppealContacts();
82-
}, []);
8343

8444
const handleRemoveContact = async () => {
85-
const appealContactId = appealContactsIds.find(
86-
(appealContact) => appealContact.contact.id === contactId,
87-
)?.id;
88-
if (!appealContactId) {
45+
if (!appealId) {
8946
enqueueSnackbar('Error while removing contact from appeal.', {
9047
variant: 'error',
9148
});
@@ -94,7 +51,8 @@ export const DeleteAppealContactModal: React.FC<
9451
await deleteAppealContact({
9552
variables: {
9653
input: {
97-
id: appealContactId,
54+
contactId,
55+
appealId,
9856
},
9957
},
10058
update: (cache) => {
@@ -136,13 +94,13 @@ export const DeleteAppealContactModal: React.FC<
13694
)}
13795
</DialogContent>
13896
<DialogActions>
139-
<CancelButton onClick={onClickDecline} disabled={mutating || loading}>
97+
<CancelButton onClick={onClickDecline} disabled={mutating}>
14098
{t('No')}
14199
</CancelButton>
142100
<SubmitButton
143101
type="button"
144102
onClick={handleRemoveContact}
145-
disabled={mutating || loading}
103+
disabled={mutating}
146104
>
147105
{t('Yes')}
148106
</SubmitButton>

0 commit comments

Comments
 (0)