Skip to content

Commit

Permalink
fix: invalidate children key on reorder (#359)
Browse files Browse the repository at this point in the history
* fix: invalidate children key on reorder
  • Loading branch information
pyphilia authored Jul 25, 2023
1 parent 375c0be commit 15005b9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/mutations/item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,47 @@ describe('Items Mutations', () => {
expect(queryClient.getQueryState(parentKey)?.isInvalidated).toBeTruthy();
});

it('Edit item extra children order invalidate children key', async () => {
// set default data
const editedItem = ITEMS.get(3)!;
const editedItemKey = buildItemKey(editedItem.id);
const editPayload = {
id: editedItem.id,
extra: { folder: { childrenOrder: [1, 2] } },
};
const childrenKey = buildItemChildrenKey(editedItem.id);
queryClient.setQueryData(childrenKey, List([ITEMS.get(1)!]));
queryClient.setQueryData(editedItemKey, editedItem);

const route = `/${buildEditItemRoute(editedItem.id)}`;
const response = item;
const endpoints = [
{
response,
method: HttpMethod.PATCH,
route,
},
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
await mockedMutation.mutate(editPayload);
await waitForMutation();
});

expect(
queryClient.getQueryState(editedItemKey)?.isInvalidated,
).toBeTruthy();
expect(
queryClient.getQueryState(childrenKey)?.isInvalidated,
).toBeTruthy();
});

it('Unauthorized', async () => {
const route = `/${buildEditItemRoute(item.id)}`;
queryClient.setQueryData(itemKey, item);
Expand Down
9 changes: 8 additions & 1 deletion src/mutations/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { QueryClient, useMutation, useQueryClient } from 'react-query';

import {
DiscriminatedItem,
FolderItemExtra,
Item,
ItemSettings,
ItemType,
MAX_TARGETS_FOR_MODIFY_REQUEST,
ThumbnailSize,
UUID,
Expand Down Expand Up @@ -231,7 +233,7 @@ export default (queryConfig: QueryClientConfig) => {
queryClient.setQueryData(itemKey, context?.item);
notifier?.({ type: editItemRoutine.FAILURE, payload: { error } });
},
onSettled: (_newItem, _error, { id }, context) => {
onSettled: (_newItem, _error, { id, extra }, context) => {
const prevItem = context?.item;
if (prevItem) {
const parentKey = getKeyForParentId(
Expand All @@ -240,6 +242,11 @@ export default (queryConfig: QueryClientConfig) => {
queryClient.invalidateQueries(parentKey);
}

// reorder affect children to change
if ((extra as FolderItemExtra)?.[ItemType.FOLDER]?.childrenOrder) {
queryClient.invalidateQueries(buildItemChildrenKey(id));
}

const itemKey = buildItemKey(id);
queryClient.invalidateQueries(itemKey);
},
Expand Down

0 comments on commit 15005b9

Please sign in to comment.