Skip to content

Commit

Permalink
Updated the caching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb Panza committed Sep 20, 2021
1 parent 85e9f9a commit 00602fa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,39 @@ import { GroupMemberDetails } from 'components';
import { Box, Modal, Loader } from 'ui-kit';

const GroupMemberDetailsModal = ({ id, onSave: callback }) => {
const client = useApolloClient();
const modalDispatch = useModalDispatch();
const { groupMemberStatuses, inactiveStatusReasons } =
useGroupMemberStatuses();
const [[updateStatus], [updateNote]] = useEditGroupMember();
const [[updateStatus], [updateNote]] = useEditGroupMember({
update: (cache, { data }) => {
if (data?.updateGroupMemberStatus) {
console.log({ data });
const { updateGroupMemberStatus } = data;
const { id, status: newStatus } = updateGroupMemberStatus;

cache.modify({
id: cache.identify({ __typename: 'GroupMember', id }),
fields: {
status() {
return newStatus;
},
},
});

cache.modify({
id: cache.identify({ __typename: 'GroupMemberSearchResult', id }),
fields: {
status() {
return newStatus.label;
},
},
});
}
},
});
const { groupMember, loading } = useGroupMember({ variables: { id } });
const [isLoading, setIsLoading] = useState(loading);

const { cache } = client;

const onCancel = () => {
modalDispatch(hideModal());
};
Expand All @@ -42,32 +65,34 @@ const GroupMemberDetailsModal = ({ id, onSave: callback }) => {
inactiveStatusReason,
}) => {
setIsLoading(true);
// note : only run the mutation if the value has actually changed
const statusPromise = () => {
if (status !== groupMember.status.id) {
return updateStatus({
variables: {
groupMemberId,
groupMemberStatusId: status,
inactiveStatusReasonId: inactiveStatusReason,
},
});
}

return Promise.resolve();
};
const notePromise = () => {
if (note !== groupMember.note) {
return updateNote({
variables: {
groupMemberId,
note,
},
});
}

await Promise.all([
updateStatus({
variables: {
groupMemberId,
groupMemberStatusId: status,
inactiveStatusReasonId: inactiveStatusReason,
},
}),
updateNote({
variables: {
groupMemberId,
note,
},
}),
]);
return Promise.resolve();
};

cache.modify({
id: cache.identify({ __typename: 'GroupMemberSearchResult', id }),
fields: {
status() {
const newStatus = groupMemberStatuses.find(({ id }) => id === status);
return newStatus.label;
},
},
});
await Promise.all([statusPromise(), notePromise()]);

onCancel();
};
Expand Down
5 changes: 5 additions & 0 deletions hooks/useEditGroupMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export const UPDATE_GROUP_MEMBER_STATUS = gql`
inactiveStatusReasonId: $inactiveStatusReasonId
) {
id
status {
id
label
}
}
}
`;
Expand All @@ -29,6 +33,7 @@ export const UPDATE_GROUP_MEMBER_NOTE = gql`
mutation updateGroupMemberNote($groupMemberId: ID!, $note: String) {
updateGroupMemberNote(groupMemberId: $groupMemberId, text: $note) {
id
note
}
}
`;
Expand Down
1 change: 1 addition & 0 deletions hooks/useSearchGroupMembers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function useSearchGroupMembers(options = {}) {
});
const [searchGroupMembers, query] = useLazyQuery(SEARCH_GROUP_MEMBERS, {
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first',
...options,
});

Expand Down

0 comments on commit 00602fa

Please sign in to comment.