Skip to content

feat: offer to restart workspace when ttl is changed #5391

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 19 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix second test
  • Loading branch information
presleyp committed Dec 12, 2022
commit 0c4d52c60c716b42ebfd46683fc498572bc70fef
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ import {
} from "pages/WorkspaceSchedulePage/schedule"
import { AutoStop, ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl"
import * as TypesGen from "../../api/typesGenerated"
import { WorkspaceScheduleFormValues } from "../../components/WorkspaceScheduleForm/WorkspaceScheduleForm"
import {
WorkspaceScheduleFormValues,
Language as FormLanguage,
} from "../../components/WorkspaceScheduleForm/WorkspaceScheduleForm"
import {
WorkspaceSchedulePage,
Language as PageLanguage,
} from "./WorkspaceSchedulePage"

const validValues: WorkspaceScheduleFormValues = {
autoStartEnabled: true,
Expand Down Expand Up @@ -262,6 +269,20 @@ describe("WorkspaceSchedulePage", () => {
expect(dialog).toBeInTheDocument()
})


it("doesn't show if autoStop is not changed", async () => {
renderWithAuth(<WorkspaceSchedulePage />, {
route: `/@${MockUser.username}/${MockWorkspace.name}/schedule`,
path: "/@:username/:workspace/schedule"
})
const user = userEvent.setup()
const autoStartToggle = await screen.findByLabelText(
FormLanguage.startSwitch,
)
await user.click(autoStartToggle)
const submitButton = await screen.findByRole("button", { name: /submit/i })
await user.click(submitButton)
const dialog = screen.queryByText(PageLanguage.dialogTitle)
expect(dialog).not.toBeInTheDocument()
})
})
})
26 changes: 9 additions & 17 deletions site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AlertBanner } from "components/AlertBanner/AlertBanner"
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
import { scheduleToAutoStart } from "pages/WorkspaceSchedulePage/schedule"
import { ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl"
import React, { useEffect, useState } from "react"
import React, { useEffect } from "react"
import { Navigate, useNavigate, useParams } from "react-router-dom"
import { scheduleChanged } from "util/schedule"
import * as TypesGen from "../../api/typesGenerated"
Expand All @@ -28,6 +28,11 @@ export const Language = {
applyLater: "Apply update later",
}

const getAutoStart = (workspace?: TypesGen.Workspace) =>
scheduleToAutoStart(workspace?.autostart_schedule)
const getAutoStop = (workspace?: TypesGen.Workspace) =>
ttlMsToAutoStop(workspace?.ttl_ms)

export const WorkspaceSchedulePage: React.FC = () => {
const { username: usernameQueryParam, workspace: workspaceQueryParam } =
useParams()
Expand All @@ -52,19 +57,6 @@ export const WorkspaceSchedulePage: React.FC = () => {
scheduleSend({ type: "GET_WORKSPACE", username, workspaceName })
}, [username, workspaceName, scheduleSend])

const getAutoStart = (workspace?: TypesGen.Workspace) =>
scheduleToAutoStart(workspace?.autostart_schedule)
const getAutoStop = (workspace?: TypesGen.Workspace) =>
ttlMsToAutoStop(workspace?.ttl_ms)

const [autoStart, setAutoStart] = useState(getAutoStart(workspace))
const [autoStop, setAutoStop] = useState(getAutoStop(workspace))

useEffect(() => {
setAutoStart(getAutoStart(workspace))
setAutoStop(getAutoStop(workspace))
}, [workspace])

if (!username || !workspaceName) {
return <Navigate to="/workspaces" />
}
Expand Down Expand Up @@ -108,7 +100,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
return (
<WorkspaceScheduleForm
submitScheduleError={submitScheduleError}
initialValues={{ ...autoStart, ...autoStop }}
initialValues={{ ...getAutoStart(workspace), ...getAutoStop(workspace) }}
isLoading={scheduleState.tags.has("loading")}
onCancel={() => {
navigate(`/@${username}/${workspaceName}`)
Expand All @@ -118,8 +110,8 @@ export const WorkspaceSchedulePage: React.FC = () => {
type: "SUBMIT_SCHEDULE",
autoStart: formValuesToAutoStartRequest(values),
ttl: formValuesToTTLRequest(values),
autoStartChanged: scheduleChanged(autoStart, values),
autoStopChanged: scheduleChanged(autoStop, values),
autoStartChanged: scheduleChanged(getAutoStart(workspace), values),
autoStopChanged: scheduleChanged(getAutoStop(workspace), values),
})
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export const workspaceSchedule =
}
},
submitSchedule: async (context, event) => {
console.log(event)
if (!context.workspace?.id) {
// This state is theoretically impossible, but helps TS
throw new Error("Failed to load workspace.")
Expand Down