-
Notifications
You must be signed in to change notification settings - Fork 407
upcoming: [M3-7840] - Placement Groups Query Key Factory #10314
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
Changes from all commits
609d416
66981b2
cf7a3ee
4bd63f0
03bbff5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@linode/manager": Upcoming Features | ||
| --- | ||
|
|
||
| Implement Placement Groups Query Key Factory ([#10314](https://github.com/linode/manager/pull/10314)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,10 @@ import { | |
| Params, | ||
| ResourcePage, | ||
| } from '@linode/api-v4/lib/types'; | ||
| import { createQueryKeys } from '@lukemorales/query-key-factory'; | ||
| import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | ||
|
|
||
| import { queryKey as linodeQueryKey } from 'src/queries/linodes/linodes'; | ||
| import { getAll } from 'src/utilities/getAll'; | ||
|
|
||
| import { profileQueries } from './profile'; | ||
|
|
@@ -27,20 +29,32 @@ import type { | |
| UpdatePlacementGroupPayload, | ||
| } from '@linode/api-v4'; | ||
|
|
||
| export const queryKey = 'placement-groups'; | ||
|
|
||
| export const useUnpaginatedPlacementGroupsQuery = (enabled = true) => | ||
| useQuery<PlacementGroup[], APIError[]>({ | ||
| enabled, | ||
| queryFn: () => getAllPlacementGroupsRequest(), | ||
| queryKey: [queryKey, 'all'], | ||
| }); | ||
|
|
||
| const getAllPlacementGroupsRequest = () => | ||
| getAll<PlacementGroup>((params, filters) => | ||
| getPlacementGroups(params, filters) | ||
| )().then((data) => data.data); | ||
|
|
||
| export const placementGroupQueries = createQueryKeys('placement-groups', { | ||
| all: { | ||
| queryFn: getAllPlacementGroupsRequest, | ||
| queryKey: null, | ||
| }, | ||
| paginated: (params: Params, filters: Filter) => ({ | ||
| queryFn: () => getPlacementGroups(params, filters), | ||
| queryKey: [params, filters], | ||
| }), | ||
| placementGroup: (placementGroupId: number) => ({ | ||
| queryFn: () => getPlacementGroup(placementGroupId), | ||
| queryKey: [placementGroupId], | ||
| }), | ||
| }); | ||
|
|
||
| export const useAllPlacementGroupsQuery = (enabled = true) => | ||
| useQuery<PlacementGroup[], APIError[]>({ | ||
| enabled, | ||
| ...placementGroupQueries.all, | ||
| }); | ||
|
|
||
| export const usePlacementGroupsQuery = ( | ||
| params: Params, | ||
| filter: Filter, | ||
|
|
@@ -49,8 +63,7 @@ export const usePlacementGroupsQuery = ( | |
| useQuery<ResourcePage<PlacementGroup>, APIError[]>({ | ||
| enabled, | ||
| keepPreviousData: true, | ||
| queryFn: () => getPlacementGroups(params, filter), | ||
| queryKey: [queryKey, 'paginated', params, filter], | ||
| ...placementGroupQueries.paginated(params, filter), | ||
| }); | ||
|
|
||
| export const usePlacementGroupQuery = ( | ||
|
|
@@ -59,8 +72,7 @@ export const usePlacementGroupQuery = ( | |
| ) => { | ||
| return useQuery<PlacementGroup, APIError[]>({ | ||
| enabled, | ||
| queryFn: () => getPlacementGroup(placementGroupId), | ||
| queryKey: [queryKey, 'placement-group', placementGroupId], | ||
| ...placementGroupQueries.placementGroup(placementGroupId), | ||
| }); | ||
| }; | ||
|
|
||
|
|
@@ -70,12 +82,13 @@ export const useCreatePlacementGroup = () => { | |
| return useMutation<PlacementGroup, APIError[], CreatePlacementGroupPayload>({ | ||
| mutationFn: createPlacementGroup, | ||
| onSuccess: (placementGroup) => { | ||
| queryClient.invalidateQueries([queryKey, 'paginated']); | ||
| queryClient.invalidateQueries([queryKey, 'all']); | ||
| queryClient.setQueryData( | ||
| [queryKey, 'placement-group', placementGroup.id], | ||
| queryClient.invalidateQueries(placementGroupQueries.paginated._def); | ||
| queryClient.invalidateQueries(placementGroupQueries.all.queryKey); | ||
| queryClient.setQueryData<PlacementGroup>( | ||
| placementGroupQueries.placementGroup(placementGroup.id).queryKey, | ||
| placementGroup | ||
| ); | ||
|
|
||
| // If a restricted user creates an entity, we must make sure grants are up to date. | ||
| queryClient.invalidateQueries(profileQueries.grants.queryKey); | ||
| }, | ||
|
|
@@ -88,10 +101,10 @@ export const useMutatePlacementGroup = (id: number) => { | |
| return useMutation<PlacementGroup, APIError[], UpdatePlacementGroupPayload>({ | ||
| mutationFn: (data) => updatePlacementGroup(id, data), | ||
| onSuccess: (placementGroup) => { | ||
| queryClient.invalidateQueries([queryKey, 'paginated']); | ||
| queryClient.invalidateQueries([queryKey, 'all']); | ||
| queryClient.invalidateQueries(placementGroupQueries.paginated._def); | ||
| queryClient.invalidateQueries(placementGroupQueries.all.queryKey); | ||
| queryClient.setQueryData( | ||
| [queryKey, 'placement-group', id], | ||
| placementGroupQueries.placementGroup(id).queryKey, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When editing a PG, we're only editing its label, so no need to invalidate any Linode query here since the PG is not represented in the Linode details UI |
||
| placementGroup | ||
| ); | ||
| }, | ||
|
|
@@ -104,9 +117,11 @@ export const useDeletePlacementGroup = (id: number) => { | |
| return useMutation<{}, APIError[]>({ | ||
| mutationFn: () => deletePlacementGroup(id), | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries([queryKey, 'paginated']); | ||
| queryClient.invalidateQueries([queryKey, 'all']); | ||
| queryClient.removeQueries([queryKey, 'placement-group', id]); | ||
| queryClient.invalidateQueries(placementGroupQueries.paginated._def); | ||
| queryClient.invalidateQueries(placementGroupQueries.all.queryKey); | ||
| queryClient.removeQueries( | ||
| placementGroupQueries.placementGroup(id).queryKey | ||
| ); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't delete a PG with Linodes in it, so at the time of deletion, it is empty therefore no need to invalidate any Linode |
||
| }, | ||
| }); | ||
| }; | ||
|
|
@@ -120,12 +135,15 @@ export const useAssignLinodesToPlacementGroup = (placementGroupId: number) => { | |
| AssignLinodesToPlacementGroupPayload | ||
| >({ | ||
| mutationFn: (data) => assignLinodesToPlacementGroup(placementGroupId, data), | ||
| onSuccess: (updatedPlacementGroup) => { | ||
| queryClient.invalidateQueries([queryKey, 'paginated']); | ||
| queryClient.setQueryData( | ||
| [queryKey, 'placement-group', placementGroupId], | ||
| updatedPlacementGroup | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries(placementGroupQueries.paginated._def); | ||
| queryClient.invalidateQueries(placementGroupQueries.all.queryKey); | ||
| queryClient.invalidateQueries( | ||
| placementGroupQueries.placementGroup(placementGroupId).queryKey | ||
| ); | ||
|
|
||
| // Invalidate all linodes query since we use the list to populate the PG linode select | ||
| queryClient.invalidateQueries([linodeQueryKey, 'all']); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
@@ -141,12 +159,15 @@ export const useUnassignLinodesFromPlacementGroup = ( | |
| >({ | ||
| mutationFn: (data) => | ||
| unassignLinodesFromPlacementGroup(placementGroupId, data), | ||
| onSuccess: (updatedPlacementGroup) => { | ||
| queryClient.invalidateQueries([queryKey, 'paginated']); | ||
| queryClient.setQueryData( | ||
| [queryKey, 'placement-group', placementGroupId], | ||
| updatedPlacementGroup | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries(placementGroupQueries.paginated._def); | ||
| queryClient.invalidateQueries(placementGroupQueries.all.queryKey); | ||
| queryClient.invalidateQueries( | ||
| placementGroupQueries.placementGroup(placementGroupId).queryKey | ||
| ); | ||
|
|
||
| // Invalidate all linodes query since we use the list to populate the PG linode select | ||
| queryClient.invalidateQueries([linodeQueryKey, 'all']); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For assign and unassign, we're only worried about invalidating the allLinodesQuery because that's the only relationship query we use at the moment (for the PG select). There is no representation of the PG in the linode details UI as it stands |
||
| }, | ||
| }); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when creating a new PG, we're really only concerned about the Placement Group queries since at the time of creation the PG will be empty