Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const BuildParametersPopover = ({
return (
<>
<Button
data-testid="build-parameters-button"
disabled={disabled}
color="neutral"
sx={{ px: 0 }}
Expand Down Expand Up @@ -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 (
Expand All @@ -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"
Expand Down
76 changes: 76 additions & 0 deletions site/src/pages/WorkspacePage/WorkspacePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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" }],
})
})
})
})
31 changes: 19 additions & 12 deletions site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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 })
Expand Down Expand Up @@ -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={() => {
Expand Down Expand Up @@ -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."
Expand Down
8 changes: 0 additions & 8 deletions site/src/pages/WorkspacePage/hooks.ts

This file was deleted.