fix(site): send build parameters over the confirmation dialog on restart (#8660)

This commit is contained in:
Bruno Quaresma
2023-07-21 12:35:28 -06:00
committed by GitHub
parent 30fe153296
commit 7f67000b90
4 changed files with 98 additions and 21 deletions
@@ -47,6 +47,7 @@ export const BuildParametersPopover = ({
return (
<>
<Button
data-testid="build-parameters-button"
disabled={disabled}
color="neutral"
sx={{ px: 0 }}
@@ -159,7 +160,7 @@ const Form = ({
const getFieldHelpers = getFormHelpers(form)
return (
<form onSubmit={form.handleSubmit}>
<form onSubmit={form.handleSubmit} data-testid="build-parameters-form">
<FormFields>
{ephemeralParameters.map((parameter, index) => {
return (
@@ -182,6 +183,7 @@ const Form = ({
</FormFields>
<Box sx={{ py: 3, pb: 1 }}>
<Button
data-testid="build-parameters-submit"
type="submit"
variant="contained"
color="primary"
@@ -411,4 +411,80 @@ describe("WorkspacePage", () => {
await renderWorkspacePage()
await screen.findByTestId("error-unsupported-workspaces")
})
it("restart the workspace with one time parameters when having the confirmation dialog", async () => {
window.localStorage.removeItem(`${MockUser.id}_ignoredWarnings`)
jest.spyOn(api, "getWorkspaceParameters").mockResolvedValue({
templateVersionRichParameters: [
{
...MockTemplateVersionParameter1,
ephemeral: true,
name: "rebuild",
description: "Rebuild",
required: false,
},
],
buildParameters: [{ name: "rebuild", value: "false" }],
})
const restartWorkspaceSpy = jest.spyOn(api, "restartWorkspace")
const user = userEvent.setup()
await renderWorkspacePage()
await user.click(screen.getByTestId("build-parameters-button"))
const buildParametersForm = await screen.findByTestId(
"build-parameters-form",
)
const rebuildField = within(buildParametersForm).getByLabelText("Rebuild", {
exact: false,
})
await user.clear(rebuildField)
await user.type(rebuildField, "true")
await user.click(screen.getByTestId("build-parameters-submit"))
await user.click(screen.getByTestId("confirm-button"))
await waitFor(() => {
expect(restartWorkspaceSpy).toBeCalledWith({
workspace: MockWorkspace,
buildParameters: [{ name: "rebuild", value: "true" }],
})
})
})
it("restart the workspace with one time parameters without the confirmation dialog", async () => {
window.localStorage.setItem(
`${MockUser.id}_ignoredWarnings`,
JSON.stringify({
restart: new Date().toISOString(),
}),
)
jest.spyOn(api, "getWorkspaceParameters").mockResolvedValue({
templateVersionRichParameters: [
{
...MockTemplateVersionParameter1,
ephemeral: true,
name: "rebuild",
description: "Rebuild",
required: false,
},
],
buildParameters: [{ name: "rebuild", value: "false" }],
})
const restartWorkspaceSpy = jest.spyOn(api, "restartWorkspace")
const user = userEvent.setup()
await renderWorkspacePage()
await user.click(screen.getByTestId("build-parameters-button"))
const buildParametersForm = await screen.findByTestId(
"build-parameters-form",
)
const rebuildField = within(buildParametersForm).getByLabelText("Rebuild", {
exact: false,
})
await user.clear(rebuildField)
await user.type(rebuildField, "true")
await user.click(screen.getByTestId("build-parameters-submit"))
await waitFor(() => {
expect(restartWorkspaceSpy).toBeCalledWith({
workspace: MockWorkspace,
buildParameters: [{ name: "rebuild", value: "true" }],
})
})
})
})
@@ -27,9 +27,8 @@ import {
} from "../../xServices/workspace/workspaceXService"
import { UpdateBuildParametersDialog } from "./UpdateBuildParametersDialog"
import { ChangeVersionDialog } from "./ChangeVersionDialog"
import { useQuery } from "@tanstack/react-query"
import { getTemplateVersions } from "api/api"
import { useRestartWorkspace } from "./hooks"
import { useMutation, useQuery } from "@tanstack/react-query"
import { getTemplateVersions, restartWorkspace } from "api/api"
import {
ConfirmDialog,
ConfirmDialogProps,
@@ -89,7 +88,10 @@ export const WorkspaceReadyPage = ({
enabled: changeVersionDialogOpen,
})
const [isConfirmingUpdate, setIsConfirmingUpdate] = useState(false)
const [isConfirmingRestart, setIsConfirmingRestart] = useState(false)
const [confirmingRestart, setConfirmingRestart] = useState<{
open: boolean
buildParameters?: TypesGen.WorkspaceBuildParameter[]
}>({ open: false })
const user = useMe()
const { isWarningIgnored, ignoreWarning } = useIgnoreWarnings(user.id)
const buildLogs = useBuildLogs(workspace)
@@ -99,10 +101,12 @@ export const WorkspaceReadyPage = ({
workspace.latest_build.status,
)
const {
mutate: restartWorkspace,
mutate: mutateRestartWorkspace,
error: restartBuildError,
isLoading: isRestarting,
} = useRestartWorkspace()
} = useMutation({
mutationFn: restartWorkspace,
})
// keep banner machine in sync with workspace
useEffect(() => {
bannerSend({ type: "REFRESH_WORKSPACE", workspace })
@@ -154,9 +158,9 @@ export const WorkspaceReadyPage = ({
handleDelete={() => workspaceSend({ type: "ASK_DELETE" })}
handleRestart={(buildParameters) => {
if (isWarningIgnored("restart")) {
restartWorkspace({ workspace, buildParameters })
mutateRestartWorkspace({ workspace, buildParameters })
} else {
setIsConfirmingRestart(true)
setConfirmingRestart({ open: true, buildParameters })
}
}}
handleUpdate={() => {
@@ -253,15 +257,18 @@ export const WorkspaceReadyPage = ({
/>
<WarningDialog
open={isConfirmingRestart}
open={confirmingRestart.open}
onConfirm={(shouldIgnore) => {
if (shouldIgnore) {
ignoreWarning("restart")
}
restartWorkspace({ workspace })
setIsConfirmingRestart(false)
mutateRestartWorkspace({
workspace,
buildParameters: confirmingRestart.buildParameters,
})
setConfirmingRestart({ open: false })
}}
onClose={() => setIsConfirmingRestart(false)}
onClose={() => setConfirmingRestart({ open: false })}
title="Confirm restart"
confirmText="Restart"
description="Are you sure you want to restart your workspace? Updating your workspace will stop all running processes and delete non-persistent data."
-8
View File
@@ -1,8 +0,0 @@
import { restartWorkspace } from "api/api"
import { useMutation } from "@tanstack/react-query"
export const useRestartWorkspace = () => {
return useMutation({
mutationFn: restartWorkspace,
})
}