({
workspace,
latestVersion,
onSuccess,
onError,
}: UseWorkspaceUpdateOptions)
| 30 | }; |
| 31 | |
| 32 | export const useWorkspaceUpdate = ({ |
| 33 | workspace, |
| 34 | latestVersion, |
| 35 | onSuccess, |
| 36 | onError, |
| 37 | }: UseWorkspaceUpdateOptions): UseWorkspaceUpdateResult => { |
| 38 | const queryClient = useQueryClient(); |
| 39 | const [isConfirmingUpdate, setIsConfirmingUpdate] = useState(false); |
| 40 | |
| 41 | const updateWorkspaceOptions = updateWorkspace(workspace, queryClient); |
| 42 | const updateWorkspaceMutation = useMutation({ |
| 43 | ...updateWorkspaceOptions, |
| 44 | onSuccess: (build: WorkspaceBuild) => { |
| 45 | updateWorkspaceOptions.onSuccess(build); |
| 46 | onSuccess?.(build); |
| 47 | }, |
| 48 | onError, |
| 49 | }); |
| 50 | |
| 51 | const update = () => { |
| 52 | setIsConfirmingUpdate(true); |
| 53 | }; |
| 54 | |
| 55 | const confirmUpdate = (buildParameters: WorkspaceBuildParameter[] = []) => { |
| 56 | updateWorkspaceMutation.mutate({ |
| 57 | buildParameters, |
| 58 | isDynamicParametersEnabled: |
| 59 | !workspace.template_use_classic_parameter_flow, |
| 60 | }); |
| 61 | setIsConfirmingUpdate(false); |
| 62 | }; |
| 63 | |
| 64 | return { |
| 65 | update, |
| 66 | isUpdating: updateWorkspaceMutation.isPending, |
| 67 | dialogs: { |
| 68 | updateConfirmation: { |
| 69 | open: isConfirmingUpdate, |
| 70 | onClose: () => setIsConfirmingUpdate(false), |
| 71 | onConfirm: () => confirmUpdate(), |
| 72 | latestVersion, |
| 73 | }, |
| 74 | missingBuildParameters: { |
| 75 | workspace, |
| 76 | error: updateWorkspaceMutation.error, |
| 77 | onClose: () => { |
| 78 | updateWorkspaceMutation.reset(); |
| 79 | }, |
| 80 | onUpdate: (buildParameters: WorkspaceBuildParameter[]) => { |
| 81 | if ( |
| 82 | updateWorkspaceMutation.error instanceof MissingBuildParameters || |
| 83 | updateWorkspaceMutation.error instanceof ParameterValidationError |
| 84 | ) { |
| 85 | confirmUpdate(buildParameters); |
| 86 | } |
| 87 | }, |
| 88 | }, |
| 89 | }, |
no test coverage detected