From 9e0f65dee3b3329eb4eabe093f44a8e3b7d6e820 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 30 Jun 2022 11:01:31 +0200 Subject: [PATCH] fix(medusa-react): Allow to not invalidate any cache (#1756) ### What At the moment, it is not possible to not invalidate any queries> Example, when we want to Create a new signed url, it does not require to invalidate any of the queries. But the way it is done in the buildOptions, require to give either a key or an array of keys. ### How The behaviour for empty array is to invalidate all the queries and here we would like to be able to just pass undefined in order to not trigger the invalidation. The update allow the arg to be optional and check for undefined explicitly before choosing the invalidation --- .../src/hooks/admin/uploads/mutations.ts | 6 +++--- .../medusa-react/src/hooks/utils/buildOptions.ts | 12 +++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/medusa-react/src/hooks/admin/uploads/mutations.ts b/packages/medusa-react/src/hooks/admin/uploads/mutations.ts index 32cfd4fa04e18..b5b136684eaac 100644 --- a/packages/medusa-react/src/hooks/admin/uploads/mutations.ts +++ b/packages/medusa-react/src/hooks/admin/uploads/mutations.ts @@ -23,7 +23,7 @@ export const useAdminUploadFile = ( return useMutation((payload: IAdminPostUploadsFileReq) => { return client.admin.uploads.create(payload) - }, buildOptions(queryClient, [], options)) + }, buildOptions(queryClient, undefined, options)) } export const useAdminCreatePresignedDownloadUrl = ( @@ -39,7 +39,7 @@ export const useAdminCreatePresignedDownloadUrl = ( return useMutation( (payload: AdminPostUploadsDownloadUrlReq) => client.admin.uploads.getPresignedDownloadUrl(payload), - buildOptions(queryClient, [], options) + buildOptions(queryClient, undefined, options) ) } @@ -55,6 +55,6 @@ export const useAdminDeleteFile = ( return useMutation( (payload: AdminDeleteUploadsReq) => client.admin.uploads.delete(payload), - buildOptions(queryClient, [], options) + buildOptions(queryClient, undefined, options) ) } diff --git a/packages/medusa-react/src/hooks/utils/buildOptions.ts b/packages/medusa-react/src/hooks/utils/buildOptions.ts index 0c8c9435e87a2..145ff13ff90d8 100644 --- a/packages/medusa-react/src/hooks/utils/buildOptions.ts +++ b/packages/medusa-react/src/hooks/utils/buildOptions.ts @@ -8,7 +8,7 @@ export const buildOptions = < TKey extends Array >( queryClient: QueryClient, - queryKey: TKey[] | TKey, + queryKey?: TKey[] | TKey, options?: UseMutationOptions ): UseMutationOptions => { return { @@ -18,10 +18,12 @@ export const buildOptions = < return options.onSuccess(...args) } - if (queryKey.filter(Array.isArray).length > 0) { - queryKey.forEach(key => queryClient.invalidateQueries(key)) - } else { - queryClient.invalidateQueries(queryKey) + if (queryKey !== undefined) { + if (queryKey.filter(Array.isArray).length > 0) { + queryKey.forEach(key => queryClient.invalidateQueries(key)) + } else { + queryClient.invalidateQueries(queryKey) + } } }, }