Skip to content

Commit

Permalink
fix(medusa-react): Allow to not invalidate any cache (medusajs#1756)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
adrien2p authored Jun 30, 2022
1 parent 5077cdf commit 9e0f65d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/medusa-react/src/hooks/admin/uploads/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -39,7 +39,7 @@ export const useAdminCreatePresignedDownloadUrl = (
return useMutation(
(payload: AdminPostUploadsDownloadUrlReq) =>
client.admin.uploads.getPresignedDownloadUrl(payload),
buildOptions(queryClient, [], options)
buildOptions(queryClient, undefined, options)
)
}

Expand All @@ -55,6 +55,6 @@ export const useAdminDeleteFile = (

return useMutation(
(payload: AdminDeleteUploadsReq) => client.admin.uploads.delete(payload),
buildOptions(queryClient, [], options)
buildOptions(queryClient, undefined, options)
)
}
12 changes: 7 additions & 5 deletions packages/medusa-react/src/hooks/utils/buildOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
},
}
Expand Down

0 comments on commit 9e0f65d

Please sign in to comment.