-
Notifications
You must be signed in to change notification settings - Fork 365
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
feat: [M3-6723] β VPC Subnet endpoints, validation, & React Query queries #9390
Changes from 5 commits
395a32f
8dbab56
de8a66e
6af8f4b
4d1d225
745fafe
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/api-v4": Added | ||
--- | ||
|
||
Endpoints for VPC Subnets ([#9390](https://github.com/linode/manager/pull/9390)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/manager": Tech Stories | ||
--- | ||
|
||
React Query queries for VPC Subnets ([#9390](https://github.com/linode/manager/pull/9390)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
import { | ||
CreateSubnetPayload, | ||
CreateVPCPayload, | ||
ModifySubnetPayload, | ||
Subnet, | ||
UpdateVPCPayload, | ||
VPC, | ||
createSubnet, | ||
createVPC, | ||
deleteSubnet, | ||
deleteVPC, | ||
getSubnet, | ||
getSubnets, | ||
getVPC, | ||
getVPCs, | ||
modifySubnet, | ||
updateVPC, | ||
} from '@linode/api-v4'; | ||
import { | ||
|
@@ -16,26 +24,28 @@ import { | |
} from '@linode/api-v4/lib/types'; | ||
import { useMutation, useQuery, useQueryClient } from 'react-query'; | ||
|
||
export const queryKey = 'vpcs'; | ||
export const vpcQueryKey = 'vpcs'; | ||
export const subnetQueryKey = 'subnets'; | ||
|
||
// VPC queries | ||
export const useVPCsQuery = (params: Params, filter: Filter) => { | ||
return useQuery<ResourcePage<VPC>, APIError[]>( | ||
[queryKey, 'paginated', params, filter], | ||
[vpcQueryKey, 'paginated', params, filter], | ||
() => getVPCs(params, filter), | ||
{ keepPreviousData: true } | ||
); | ||
}; | ||
|
||
export const useVPCQuery = (id: number) => { | ||
return useQuery<VPC, APIError[]>([queryKey, 'vpc', id], () => getVPC(id)); | ||
return useQuery<VPC, APIError[]>([vpcQueryKey, 'vpc', id], () => getVPC(id)); | ||
}; | ||
|
||
export const useCreateVPCMutation = () => { | ||
const queryClient = useQueryClient(); | ||
return useMutation<VPC, APIError[], CreateVPCPayload>(createVPC, { | ||
onSuccess: (VPC) => { | ||
queryClient.invalidateQueries([queryKey, 'paginated']); | ||
queryClient.setQueryData([queryKey, 'vpc', VPC.id], VPC); | ||
queryClient.invalidateQueries([vpcQueryKey, 'paginated']); | ||
queryClient.setQueryData([vpcQueryKey, 'vpc', VPC.id], VPC); | ||
}, | ||
}); | ||
}; | ||
|
@@ -46,8 +56,8 @@ export const useUpdateVPCMutation = (id: number) => { | |
(data) => updateVPC(id, data), | ||
{ | ||
onSuccess: (VPC) => { | ||
queryClient.invalidateQueries([queryKey, 'paginated']); | ||
queryClient.setQueryData<VPC>([queryKey, 'vpc', VPC.id], VPC); | ||
queryClient.invalidateQueries([vpcQueryKey, 'paginated']); | ||
queryClient.setQueryData<VPC>([vpcQueryKey, 'vpc', VPC.id], VPC); | ||
}, | ||
} | ||
); | ||
|
@@ -57,8 +67,101 @@ export const useDeleteVPCMutation = (id: number) => { | |
const queryClient = useQueryClient(); | ||
return useMutation<{}, APIError[]>(() => deleteVPC(id), { | ||
onSuccess: () => { | ||
queryClient.invalidateQueries([queryKey, 'paginated']); | ||
queryClient.removeQueries([queryKey, 'vpc', id]); | ||
queryClient.invalidateQueries([vpcQueryKey, 'paginated']); | ||
queryClient.removeQueries([vpcQueryKey, 'vpc', id]); | ||
}, | ||
}); | ||
}; | ||
|
||
// Subnet queries | ||
export const useSubnetsQuery = ( | ||
vpcID: number, | ||
params: Params, | ||
filter: Filter | ||
) => { | ||
return useQuery<ResourcePage<Subnet>, APIError[]>( | ||
[vpcQueryKey, 'vpc', vpcID, subnetQueryKey, 'paginated', params, filter], | ||
() => getSubnets(vpcID, params, filter), | ||
{ keepPreviousData: true } | ||
); | ||
}; | ||
|
||
export const useSubnetQuery = (vpcID: number, subnetID: number) => { | ||
return useQuery<Subnet, APIError[]>( | ||
[vpcQueryKey, 'vpc', vpcID, subnetQueryKey, 'subnet', subnetID], | ||
() => getSubnet(vpcID, subnetID) | ||
); | ||
}; | ||
|
||
export const useCreateSubnetMutation = (vpcID: number) => { | ||
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. The invalidation in the |
||
const queryClient = useQueryClient(); | ||
return useMutation<Subnet, APIError[], CreateSubnetPayload>( | ||
(data) => createSubnet(vpcID, data), | ||
{ | ||
onSuccess: () => { | ||
// New subnet created --> refresh the paginated and individual VPC queries, plus the /subnets VPC query | ||
queryClient.invalidateQueries([vpcQueryKey, 'paginated']); | ||
queryClient.invalidateQueries([vpcQueryKey, 'vpc', vpcID]); | ||
queryClient.invalidateQueries([ | ||
vpcQueryKey, | ||
'vpc', | ||
vpcID, | ||
subnetQueryKey, | ||
]); | ||
}, | ||
} | ||
); | ||
}; | ||
|
||
export const useUpdateSubnetMutation = (vpcID: number, subnetID: number) => { | ||
const queryClient = useQueryClient(); | ||
return useMutation<Subnet, APIError[], ModifySubnetPayload>( | ||
(data) => modifySubnet(vpcID, subnetID, data), | ||
{ | ||
onSuccess: () => { | ||
// Subnet modified --> refresh the paginated and individual VPC queries, plus the paginated & individual subnet queries | ||
queryClient.invalidateQueries([vpcQueryKey, 'paginated']); | ||
queryClient.invalidateQueries([vpcQueryKey, 'vpc', vpcID]); | ||
queryClient.invalidateQueries([ | ||
vpcQueryKey, | ||
'vpc', | ||
vpcID, | ||
subnetQueryKey, | ||
]); | ||
queryClient.invalidateQueries([ | ||
vpcQueryKey, | ||
'vpc', | ||
vpcID, | ||
subnetQueryKey, | ||
'subnet', | ||
subnetID, | ||
]); | ||
}, | ||
} | ||
); | ||
}; | ||
|
||
export const useDeleteSubnetMutation = (vpcID: number, subnetID: number) => { | ||
const queryClient = useQueryClient(); | ||
return useMutation<{}, APIError[]>(() => deleteSubnet(vpcID, subnetID), { | ||
onSuccess: () => { | ||
// Subnet deleted --> refresh the paginated and individual VPC queries, plus the paginated subnet query, & clear the individual subnet query | ||
queryClient.invalidateQueries([vpcQueryKey, 'paginated']); | ||
queryClient.invalidateQueries([vpcQueryKey, 'vpc', vpcID]); | ||
queryClient.invalidateQueries([ | ||
vpcQueryKey, | ||
'vpc', | ||
vpcID, | ||
subnetQueryKey, | ||
]); | ||
queryClient.removeQueries([ | ||
vpcQueryKey, | ||
'vpc', | ||
vpcID, | ||
subnetQueryKey, | ||
'subnet', | ||
subnetID, | ||
]); | ||
}, | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/validation": Added | ||
--- | ||
|
||
Validation for VPC subnet creation and modifications ([#9390](https://github.com/linode/manager/pull/9390)) |
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.
Like last time, all of the changes in this file are temporary for testing purposes and will be removed prior to merging.
You can uncomment out the lines and replace
vpcID
andsubnetID
with real IDs to check the data is coming through.