Skip to content

Commit

Permalink
fix: deduplicate item ids on add favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh committed Mar 16, 2023
1 parent 4edf294 commit 38d0e7a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
43 changes: 43 additions & 0 deletions src/mutations/member.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,49 @@ describe('Member Mutations', () => {
expect(newData).toEqualImmutable(response);
});

it(`Adding duplicated favorite item should dedupe`, async () => {
const response = MEMBER_RESPONSE.set(
'extra',
convertJs({ favoriteItems: ['item-id'] }),
);
// set random data in cache
queryClient.setQueryData(CURRENT_MEMBER_KEY, response);
const endpoints = [
{
response,
method: HttpMethod.PATCH,
route,
},
];
const mockedMutation = await mockMutation({
mutation,
wrapper,
endpoints,
});

await act(async () => {
await mockedMutation.mutate({
memberId: id,
itemId,
extra: { favoriteItems: ['item-id'] },
});
await waitForMutation();
});

expect(
queryClient.getQueryState(CURRENT_MEMBER_KEY)?.isInvalidated,
).toBeTruthy();
expect(mockedNotifier).toHaveBeenCalledWith({
type: addFavoriteItemRoutine.SUCCESS,
});

// verify cache keys
const newData = queryClient.getQueryData(
CURRENT_MEMBER_KEY,
) as MemberRecord;
expect(newData).toEqualImmutable(response);
});

it(`Unauthorized`, async () => {
// set random data in cache
queryClient.setQueryData(CURRENT_MEMBER_KEY, MEMBER_RESPONSE);
Expand Down
19 changes: 13 additions & 6 deletions src/mutations/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,18 @@ export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => {
// mutation to update favorite items of given member
queryClient.setMutationDefaults(ADD_FAVORITE_ITEM, {
mutationFn: ({ memberId, itemId, extra: prevExtra }) => {
const newFavoriteItems = prevExtra.favoriteItems
? prevExtra.favoriteItems.concat([itemId])
const favoriteItems = prevExtra.favoriteItems
? prevExtra.favoriteItems.concat(
prevExtra.favoriteItems.includes(itemId) ? [] : [itemId],
)
: [itemId];
return Api.editMember(
{
id: memberId,
extra: { ...prevExtra, favoriteItems: newFavoriteItems },
extra: {
...prevExtra,
favoriteItems,
},
},
queryConfig,
).then((member) => convertJs(member));
Expand All @@ -164,12 +169,14 @@ export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => {

// Optimistically update to the new value
const { itemId, extra } = payload;
const newFavoriteItems = extra.favoriteItems
? extra.favoriteItems.concat([itemId])
const favoriteItems = extra.favoriteItems
? extra.favoriteItems.concat(
extra.favoriteItems.includes(itemId) ? [] : [itemId],
)
: [itemId];

const member = convertJs({
extra: { ...extra, favoriteItems: newFavoriteItems },
extra: { ...extra, favoriteItems },
});

queryClient.setQueryData(
Expand Down

0 comments on commit 38d0e7a

Please sign in to comment.