forked from PrefectHQ/prefect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI v2] Edit variables (PrefectHQ#16035)
- Loading branch information
1 parent
d64c0d7
commit db1288a
Showing
8 changed files
with
385 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import type { components } from "@/api/prefect"; | ||
import { getQueryService } from "@/api/service"; | ||
import { useToast } from "@/hooks/use-toast"; | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
|
||
type UseCreateVariableProps = { | ||
onSuccess: () => void; | ||
onError: (error: Error) => void; | ||
}; | ||
|
||
export const useCreateVariable = ({ | ||
onSuccess, | ||
onError, | ||
}: UseCreateVariableProps) => { | ||
const queryClient = useQueryClient(); | ||
const { toast } = useToast(); | ||
|
||
return useMutation({ | ||
mutationFn: (variable: components["schemas"]["VariableCreate"]) => { | ||
return getQueryService().POST("/variables/", { | ||
body: variable, | ||
}); | ||
}, | ||
onSettled: async () => { | ||
return await Promise.all([ | ||
queryClient.invalidateQueries({ | ||
predicate: (query) => query.queryKey[0] === "variables", | ||
}), | ||
queryClient.invalidateQueries({ | ||
predicate: (query) => query.queryKey[0] === "total-variable-count", | ||
}), | ||
]); | ||
}, | ||
onSuccess: () => { | ||
toast({ | ||
title: "Variable created", | ||
}); | ||
onSuccess(); | ||
}, | ||
onError, | ||
}); | ||
}; | ||
|
||
type UseUpdateVariableProps = { | ||
onSuccess: () => void; | ||
onError: (error: Error) => void; | ||
}; | ||
|
||
type VariableUpdateWithId = components["schemas"]["VariableUpdate"] & { | ||
id: string; | ||
}; | ||
|
||
export const useUpdateVariable = ({ | ||
onSuccess, | ||
onError, | ||
}: UseUpdateVariableProps) => { | ||
const queryClient = useQueryClient(); | ||
const { toast } = useToast(); | ||
|
||
return useMutation({ | ||
mutationFn: (variable: VariableUpdateWithId) => { | ||
const { id, ...body } = variable; | ||
return getQueryService().PATCH("/variables/{id}", { | ||
params: { path: { id } }, | ||
body, | ||
}); | ||
}, | ||
onSettled: async () => { | ||
return await Promise.all([ | ||
queryClient.invalidateQueries({ | ||
predicate: (query) => query.queryKey[0] === "variables", | ||
}), | ||
queryClient.invalidateQueries({ | ||
predicate: (query) => query.queryKey[0] === "total-variable-count", | ||
}), | ||
]); | ||
}, | ||
onSuccess: () => { | ||
toast({ | ||
title: "Variable updated", | ||
}); | ||
onSuccess(); | ||
}, | ||
onError, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.