From 3729e64b2b765d40ac338f6cb2521c9a036661a3 Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 4 Oct 2024 10:37:08 +0200 Subject: [PATCH 1/3] fix: remove on mutate for edit mutation, update settled, provide path --- src/item/mutations.test.ts | 5 ++- src/item/mutations.ts | 72 +++----------------------------------- 2 files changed, 8 insertions(+), 69 deletions(-) diff --git a/src/item/mutations.test.ts b/src/item/mutations.test.ts index cf584316..8d14af26 100644 --- a/src/item/mutations.test.ts +++ b/src/item/mutations.test.ts @@ -51,7 +51,10 @@ describe('Items Mutations', () => { const item = FolderItemFactory(); const mutation = mutations.useEditItem; const itemKey = itemKeys.single(item.id).content; - const payload = { id: item.id, description: 'new description' }; + const payload = { + id: item.id, + description: 'new description', + }; it('Edit item in root', async () => { // set default data diff --git a/src/item/mutations.ts b/src/item/mutations.ts index e796f6a0..42364072 100644 --- a/src/item/mutations.ts +++ b/src/item/mutations.ts @@ -122,57 +122,6 @@ export default (queryConfig: QueryClientConfig) => { > >, ) => Api.editItem(item.id, item, queryConfig), - // newItem contains all updatable properties - onMutate: async ( - newItem: Partial & - Pick & { - extra?: DiscriminatedItem['extra']; - }, - ) => { - const itemKey = itemKeys.single(newItem.id).content; - - // invalidate key - await queryClient.cancelQueries({ queryKey: itemKey }); - - // build full item with new values - const prevItem = queryClient.getQueryData(itemKey); - - // if the item is not in the cache, we don't need to continue with optimistic mutation - if (!prevItem) { - return {}; - } - - // trim manually names because it does it in the backend - const newFullItem = { - ...prevItem, - name: prevItem.name.trim(), - displayName: prevItem.displayName.trim(), - }; - queryClient.setQueryData(itemKey, newFullItem); - - const previousItems = { - ...(Boolean(prevItem) && { - parent: await mutateParentChildren( - { - childPath: prevItem?.path, - value: (old: DiscriminatedItem[]) => { - if (!old?.length) { - return old; - } - const idx = old.findIndex(({ id }) => id === newItem.id); - if (newFullItem && idx >= 0) { - old[idx] = newFullItem; - } - return old; - }, - }, - queryClient, - ), - item: prevItem, - }), - }; - return previousItems; - }, onSuccess: () => { notifier?.( { @@ -182,30 +131,17 @@ export default (queryConfig: QueryClientConfig) => { { enableNotifications }, ); }, - onError: (error: Error, newItem, context) => { - if (context?.parent && context?.item) { - const prevItem = context?.item; - const parentKey = getKeyForParentId( - getParentFromPath(prevItem?.path), - ); - queryClient.setQueryData(parentKey, context.parent); - } - - const itemKey = itemKeys.single(newItem.id).content; - queryClient.setQueryData(itemKey, context?.item); - + onError: (error: Error) => { notifier?.( { type: editItemRoutine.FAILURE, payload: { error } }, { enableNotifications }, ); }, - onSettled: (_newItem, _error, { id }, context) => { - const prevItem = context?.item; - if (prevItem) { - const parentKey = getKeyForParentId(getParentFromPath(prevItem.path)); + onSettled: (newItem, _error, { id }) => { + if (newItem) { + const parentKey = getKeyForParentId(getParentFromPath(newItem.path)); queryClient.invalidateQueries({ queryKey: parentKey }); } - const itemKey = itemKeys.single(id).content; queryClient.invalidateQueries({ queryKey: itemKey }); }, From aa5de5c78874423912cfb95fc21c98a902e40003 Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 4 Oct 2024 13:17:04 +0200 Subject: [PATCH 2/3] refactor: fix test --- src/item/mutations.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/item/mutations.test.ts b/src/item/mutations.test.ts index 8d14af26..856d7eb8 100644 --- a/src/item/mutations.test.ts +++ b/src/item/mutations.test.ts @@ -102,7 +102,7 @@ describe('Items Mutations', () => { queryClient.setQueryData(parentKey, [FolderItemFactory()]); const route = `/${buildEditItemRoute(editedItem.id)}`; - const response = item; + const response = editedItem; const endpoints = [ { response, From 2921b90805e56260a45b8e5f67cabc757c139f6f Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 4 Oct 2024 13:20:46 +0200 Subject: [PATCH 3/3] test: add cache test --- src/item/mutations.test.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/item/mutations.test.ts b/src/item/mutations.test.ts index 856d7eb8..53ea240e 100644 --- a/src/item/mutations.test.ts +++ b/src/item/mutations.test.ts @@ -88,6 +88,36 @@ describe('Items Mutations', () => { ).toBeTruthy(); }); + it('Edit item in root without item in cache', async () => { + // set default data + queryClient.setQueryData(itemKeys.allAccessible(), [FolderItemFactory()]); + + const route = `/${buildEditItemRoute(item.id)}`; + const response = item; + const endpoints = [ + { + response, + method: HttpMethod.Patch, + route, + }, + ]; + + const mockedMutation = await mockMutation({ + endpoints, + mutation, + wrapper, + }); + + await act(async () => { + mockedMutation.mutate(payload); + await waitForMutation(); + }); + + expect( + queryClient.getQueryState(itemKeys.allAccessible())?.isInvalidated, + ).toBeTruthy(); + }); + it('Edit item in item', async () => { // set default data const parentItem = FolderItemFactory();