Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add params arg to invalidateQueries #2410

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions app/api/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
useQueryClient,
type DefaultError,
type FetchQueryOptions,
type InvalidateQueryFilters,
type QueryClient,
type UndefinedInitialDataOptions,
type UseMutationOptions,
Expand Down Expand Up @@ -241,16 +240,12 @@ export const getUseApiMutation =

export const wrapQueryClient = <A extends ApiClient>(api: A, queryClient: QueryClient) => ({
/**
* Note that we only take a single argument, `method`, rather than allowing
* the full query key `[query, params]` to be specified. This is to avoid
* accidentally overspecifying and therefore failing to match the desired
* query. The params argument can be added back in if we ever have a use case
* for it.
*
* Passing no arguments will invalidate all queries.
* Even though it's possible to specify params, prefer invalidating by method
* alone to avoid accidentally overspecifying and therefore failing to match
* the desired query.
*/
invalidateQueries: <M extends keyof A>(method?: M, filters?: InvalidateQueryFilters) =>
queryClient.invalidateQueries(method ? { queryKey: [method], ...filters } : undefined),
invalidateQueries: <M extends keyof A>(method: M, params?: Params<A[M]>) =>
queryClient.invalidateQueries({ queryKey: params ? [method, params] : [method] }),
setQueryData: <M extends keyof A>(method: M, params: Params<A[M]>, data: Result<A[M]>) =>
queryClient.setQueryData([method, params], data),
setQueryDataErrorsAllowed: <M extends keyof A>(
Expand Down
15 changes: 5 additions & 10 deletions app/forms/ip-pool-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,13 @@ export function EditIpPoolSideModalForm() {

const editPool = useApiMutation('ipPoolUpdate', {
onSuccess(updatedPool) {
queryClient.invalidateQueries('ipPoolList')
navigate(pb.ipPool({ pool: updatedPool.name }))
queryClient.invalidateQueries('ipPoolList')
addToast({ content: 'Your IP pool has been updated' })

// Only invalidate if we're staying on the same page. If the name
// _has_ changed, invalidating ipPoolView causes an error page to flash
// while the loader for the target page is running because the current
// page's pool gets cleared out while we're still on the page. If we're
// navigating to a different page, its query will fetch anew regardless.
if (pool.name === updatedPool.name) {
queryClient.invalidateQueries('ipPoolView')
}
// specify params so we only invalidate the one we're navigating to,
// avoiding an error page flash due to clearly the current page's pool
// while navigating to another one
queryClient.invalidateQueries('ipPoolView', { path: { pool: updatedPool.name } })
},
})

Expand Down
Loading