@@ -4,10 +4,11 @@ import type {
44} from '@/common/api/generated'
55import {
66 postApiV1BetaWorkloadsByNameStopMutation ,
7- getApiV1BetaWorkloadsByNameQueryKey ,
87 getApiV1BetaWorkloadsQueryKey ,
8+ getApiV1BetaWorkloadsByNameOptions ,
99} from '@/common/api/generated/@tanstack/react-query.gen'
1010import { useToastMutation } from '@/common/hooks/use-toast-mutation'
11+ import { pollBatchServerStatus } from '@/common/lib/polling'
1112import { useQueryClient } from '@tanstack/react-query'
1213
1314const getMutationData = ( name : string ) => ( {
@@ -20,9 +21,6 @@ const getMutationData = (name: string) => ({
2021export function useMutationStopServerList ( { name } : { name : string } ) {
2122 const queryClient = useQueryClient ( )
2223 const queryKey = getApiV1BetaWorkloadsQueryKey ( { query : { all : true } } )
23- const serverQueryKey = getApiV1BetaWorkloadsByNameQueryKey ( {
24- path : { name } ,
25- } )
2624 return useToastMutation ( {
2725 ...getMutationData ( name ) ,
2826
@@ -31,18 +29,7 @@ export function useMutationStopServerList({ name }: { name: string }) {
3129 queryKey,
3230 } )
3331
34- const previousServer = queryClient . getQueryData ( serverQueryKey )
35-
36- queryClient . setQueryData (
37- serverQueryKey ,
38- ( oldData : WorkloadsWorkload | undefined ) => {
39- if ( ! oldData ) return oldData
40- return {
41- ...oldData ,
42- status : 'stopping' ,
43- }
44- }
45- )
32+ const previousServersList = queryClient . getQueryData ( queryKey )
4633
4734 queryClient . setQueryData (
4835 queryKey ,
@@ -59,79 +46,30 @@ export function useMutationStopServerList({ name }: { name: string }) {
5946 }
6047 )
6148
62- return { previousServer }
49+ return { previousServersList }
6350 } ,
64- onSuccess : ( ) => {
65- queryClient . setQueryData (
66- queryKey ,
67- ( oldData : V1WorkloadListResponse | undefined ) => {
68- if ( ! oldData ) return oldData
69- return { ...oldData , status : 'stopped' }
70- }
71- )
72- queryClient . setQueryData (
73- serverQueryKey ,
74- ( oldData : WorkloadsWorkload | undefined ) => {
75- if ( ! oldData ) return oldData
76- return { ...oldData , status : 'stopped' }
77- }
51+ onSuccess : async ( ) => {
52+ // Poll until server stopped
53+ await pollBatchServerStatus (
54+ async ( names ) => {
55+ const servers = await Promise . all (
56+ names . map ( ( name ) =>
57+ queryClient . fetchQuery (
58+ getApiV1BetaWorkloadsByNameOptions ( { path : { name } } )
59+ )
60+ )
61+ )
62+ return servers
63+ } ,
64+ [ name ] ,
65+ 'stopped'
7866 )
67+ queryClient . invalidateQueries ( { queryKey } )
7968 } ,
8069 onError : ( _error , _variables , context ) => {
81- if ( context ?. previousServer ) {
82- queryClient . setQueryData ( queryKey , context . previousServer )
70+ if ( context ?. previousServersList ) {
71+ queryClient . setQueryData ( queryKey , context . previousServersList )
8372 }
8473 } ,
8574 } )
8675}
87-
88- export function useMutationStopServer ( { name } : { name : string } ) {
89- const queryClient = useQueryClient ( )
90- const queryKey = getApiV1BetaWorkloadsByNameQueryKey ( { path : { name } } )
91-
92- return useToastMutation ( {
93- ...getMutationData ( name ) ,
94-
95- onMutate : async ( ) => {
96- await queryClient . cancelQueries ( { queryKey } )
97-
98- const previousServerData = queryClient . getQueryData ( queryKey )
99-
100- queryClient . setQueryData (
101- queryKey ,
102- ( oldData : WorkloadsWorkload | undefined ) => {
103- if ( ! oldData ) return oldData
104-
105- const updatedData = {
106- ...oldData ,
107- status : 'stopping' ,
108- } as WorkloadsWorkload
109- return updatedData
110- }
111- )
112-
113- return { previousServerData }
114- } ,
115-
116- onError : ( _error , _variables , context ) => {
117- if ( context ?. previousServerData ) {
118- queryClient . setQueryData ( queryKey , context . previousServerData )
119- }
120- } ,
121- onSuccess : ( ) => {
122- queryClient . setQueryData (
123- queryKey ,
124- ( oldData : WorkloadsWorkload | undefined ) => {
125- if ( ! oldData ) return oldData
126-
127- return { ...oldData , status : 'stopped' }
128- }
129- )
130- } ,
131- onSettled : ( ) => {
132- queryClient . invalidateQueries ( {
133- queryKey : getApiV1BetaWorkloadsQueryKey ( { query : { all : true } } ) ,
134- } )
135- } ,
136- } )
137- }
0 commit comments