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

Skip to content

Commit 4180229

Browse files
authored
fix: use template default ttl when enabling auto-stop (#5494)
* Fetch default ttl - wip * Convert ms to hours * Format * Fix story * Add test
1 parent cfd02d9 commit 4180229

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
defaultSchedule,
88
emptySchedule,
99
} from "pages/WorkspaceSchedulePage/schedule"
10-
import { defaultTTL, emptyTTL } from "pages/WorkspaceSchedulePage/ttl"
10+
import { emptyTTL } from "pages/WorkspaceSchedulePage/ttl"
1111
import { makeMockApiError } from "testHelpers/entities"
1212
import {
1313
WorkspaceScheduleForm,
@@ -39,7 +39,7 @@ const defaultInitialValues = {
3939
autoStartEnabled: true,
4040
...defaultSchedule(),
4141
autoStopEnabled: true,
42-
ttl: defaultTTL,
42+
ttl: 24,
4343
}
4444

4545
export const AllDisabled = Template.bind({})

site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
defaultSchedule,
2222
emptySchedule,
2323
} from "pages/WorkspaceSchedulePage/schedule"
24-
import { defaultTTL } from "pages/WorkspaceSchedulePage/ttl"
2524
import { ChangeEvent, FC } from "react"
2625
import * as Yup from "yup"
2726
import { getFormHelpers } from "../../util/formUtils"
@@ -81,6 +80,7 @@ export interface WorkspaceScheduleFormProps {
8180
onSubmit: (values: WorkspaceScheduleFormValues) => void
8281
// for storybook
8382
initialTouched?: FormikTouched<WorkspaceScheduleFormValues>
83+
defaultTTL: number
8484
}
8585

8686
export interface WorkspaceScheduleFormValues {
@@ -192,6 +192,7 @@ export const WorkspaceScheduleForm: FC<
192192
onCancel,
193193
onSubmit,
194194
initialTouched,
195+
defaultTTL,
195196
}) => {
196197
const styles = useStyles()
197198

site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.test.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
} from "components/WorkspaceScheduleForm/WorkspaceScheduleForm"
2222
import { WorkspaceSchedulePage } from "./WorkspaceSchedulePage"
2323
import i18next from "i18next"
24+
import { server } from "testHelpers/server"
25+
import { rest } from "msw"
2426

2527
const { t } = i18next
2628

@@ -295,4 +297,37 @@ describe("WorkspaceSchedulePage", () => {
295297
expect(dialog).not.toBeInTheDocument()
296298
})
297299
})
300+
301+
describe("autostop", () => {
302+
it("uses template default ttl when first enabled", async () => {
303+
// have auto-stop disabled
304+
server.use(
305+
rest.get(
306+
"/api/v2/users/:userId/workspace/:workspaceName",
307+
(req, res, ctx) => {
308+
return res(
309+
ctx.status(200),
310+
ctx.json({ ...MockWorkspace, ttl_ms: 0 }),
311+
)
312+
},
313+
),
314+
)
315+
renderWithAuth(<WorkspaceSchedulePage />, {
316+
route: `/@${MockUser.username}/${MockWorkspace.name}/schedule`,
317+
path: "/@:username/:workspace/schedule",
318+
})
319+
const user = userEvent.setup()
320+
const autoStopToggle = await screen.findByLabelText(
321+
FormLanguage.stopSwitch,
322+
)
323+
// enable auto-stop
324+
await user.click(autoStopToggle)
325+
// find helper text that describes the mock template's 24 hour default
326+
const autoStopHelperText = await screen.findByText(
327+
"Your workspace will shut down a day after",
328+
{ exact: false },
329+
)
330+
expect(autoStopHelperText).toBeDefined()
331+
})
332+
})
298333
})

site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useMachine } from "@xstate/react"
33
import { AlertBanner } from "components/AlertBanner/AlertBanner"
44
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
55
import { Margins } from "components/Margins/Margins"
6+
import dayjs from "dayjs"
67
import { scheduleToAutoStart } from "pages/WorkspaceSchedulePage/schedule"
78
import { ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl"
89
import React, { useEffect } from "react"
@@ -46,6 +47,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
4647
getTemplateError,
4748
permissions,
4849
workspace,
50+
template,
4951
} = scheduleState.context
5052

5153
// Get workspace on mount and whenever the args for getting a workspace change.
@@ -60,7 +62,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
6062
return <Navigate to="/workspaces" />
6163
}
6264

63-
if (scheduleState.hasTag("loading")) {
65+
if (scheduleState.hasTag("loading") || !template) {
6466
return <FullScreenLoader />
6567
}
6668

@@ -104,6 +106,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
104106
...getAutoStop(workspace),
105107
}}
106108
isLoading={scheduleState.tags.has("loading")}
109+
defaultTTL={dayjs.duration(template.default_ttl_ms, "ms").asHours()}
107110
onCancel={() => {
108111
navigate(`/@${username}/${workspaceName}`)
109112
}}

site/src/pages/WorkspaceSchedulePage/ttl.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ export interface AutoStop {
55

66
export const emptyTTL = 0
77

8-
export const defaultTTL = 12
9-
108
const msToHours = (ms: number) => Math.round(ms / (1000 * 60 * 60))
119

1210
export const ttlMsToAutoStop = (ttl_ms?: number): AutoStop =>

0 commit comments

Comments
 (0)