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
This commit is contained in:
Adrien de Peretti
2022-06-30 11:01:31 +02:00
committed by GitHub
parent 5077cdf0da
commit 9e0f65dee3
2 changed files with 10 additions and 8 deletions

View File

@@ -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)
)
}

View File

@@ -8,7 +8,7 @@ export const buildOptions = <
TKey extends Array<QueryKey>
>(
queryClient: QueryClient,
queryKey: TKey[] | TKey,
queryKey?: TKey[] | TKey,
options?: UseMutationOptions<TData, TError, TVariables, TContext>
): UseMutationOptions<TData, TError, TVariables, TContext> => {
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)
}
}
},
}