-
Notifications
You must be signed in to change notification settings - Fork 943
feat: update workspace deadline when workspace ttl updated #2165
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
Changes from 1 commit
5c443de
c2127c1
07320a4
7846dd8
6d7187d
daa9a5f
fe79c0c
85da57b
8c38a74
a4c07df
582873a
9a7c623
0278a93
a372dce
ac1d29a
23b474c
de28477
a52d815
fd86474
2b9f93c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…e TTL
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,12 +8,14 @@ import MenuItem from "@material-ui/core/MenuItem" | |||||
import makeStyles from "@material-ui/core/styles/makeStyles" | ||||||
import TextField from "@material-ui/core/TextField" | ||||||
import dayjs from "dayjs" | ||||||
import advancedFormat from "dayjs/plugin/advancedFormat" | ||||||
import timezone from "dayjs/plugin/timezone" | ||||||
import utc from "dayjs/plugin/utc" | ||||||
import { useFormik } from "formik" | ||||||
import { FC } from "react" | ||||||
import * as Yup from "yup" | ||||||
import { FieldErrors } from "../../api/errors" | ||||||
import { Workspace, WorkspaceBuild } from "../../api/typesGenerated" | ||||||
import { getFormHelpers } from "../../util/formUtils" | ||||||
import { FormFooter } from "../FormFooter/FormFooter" | ||||||
import { FullPageForm } from "../FullPageForm/FullPageForm" | ||||||
|
@@ -23,6 +25,7 @@ import { zones } from "./zones" | |||||
// REMARK: timezone plugin depends on UTC | ||||||
// | ||||||
// SEE: https://day.js.org/docs/en/timezone/timezone | ||||||
dayjs.extend(advancedFormat) | ||||||
dayjs.extend(utc) | ||||||
dayjs.extend(timezone) | ||||||
|
||||||
|
@@ -44,14 +47,21 @@ export const Language = { | |||||
timezoneLabel: "Timezone", | ||||||
ttlLabel: "Time until shutdown (hours)", | ||||||
ttlHelperText: "Your workspace will automatically shut down after this amount of time has elapsed.", | ||||||
ttlCausesShutdownHelperText: "Your workspace will shut down ", | ||||||
ttlCausesShutdownAt: "at ", | ||||||
ttlCausesShutdownImmediately: "immediately!", | ||||||
ttlCausesShutdownSoon: "within 30 minutes.", | ||||||
ttlCausesNoShutdownHelperText: "Your workspace will not automatically shut down.", | ||||||
} | ||||||
|
||||||
export interface WorkspaceScheduleFormProps { | ||||||
fieldErrors?: FieldErrors | ||||||
initialValues?: WorkspaceScheduleFormValues | ||||||
isLoading: boolean | ||||||
now: dayjs.Dayjs | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. review: I'm not sure if there's a better way to do it, but I figured it's easiest to just pass in the current time as a prop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment(approving): I'm OK with this, it seems like we either don't do this and wrestle with mocks or spies in tests and storybook...or we just do this and live with it being a little weird. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion(nit): Just let's make it optional:
Suggested change
|
||||||
onCancel: () => void | ||||||
onSubmit: (values: WorkspaceScheduleFormValues) => void | ||||||
workspace: Workspace | ||||||
} | ||||||
|
||||||
export interface WorkspaceScheduleFormValues { | ||||||
|
@@ -68,6 +78,20 @@ export interface WorkspaceScheduleFormValues { | |||||
ttl: number | ||||||
} | ||||||
|
||||||
export const WorkspaceScheduleFormInitialValues = { | ||||||
sunday: false, | ||||||
monday: true, | ||||||
tuesday: true, | ||||||
wednesday: true, | ||||||
thursday: true, | ||||||
friday: true, | ||||||
saturday: false, | ||||||
|
||||||
startTime: "09:30", | ||||||
timezone: "", | ||||||
ttl: 5, | ||||||
} | ||||||
|
||||||
export const validationSchema = Yup.object({ | ||||||
sunday: Yup.boolean(), | ||||||
monday: Yup.boolean().test("at-least-one-day", Language.errorNoDayOfWeek, function (value) { | ||||||
|
@@ -154,21 +178,13 @@ export const validationSchema = Yup.object({ | |||||
export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({ | ||||||
fieldErrors, | ||||||
initialValues = { | ||||||
sunday: false, | ||||||
monday: true, | ||||||
tuesday: true, | ||||||
wednesday: true, | ||||||
thursday: true, | ||||||
friday: true, | ||||||
saturday: false, | ||||||
|
||||||
startTime: "09:30", | ||||||
timezone: "", | ||||||
ttl: 5, | ||||||
...WorkspaceScheduleFormInitialValues, | ||||||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
}, | ||||||
isLoading, | ||||||
8000 | now: now, | |||||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
onCancel, | ||||||
onSubmit, | ||||||
workspace, | ||||||
}) => { | ||||||
const styles = useStyles() | ||||||
|
||||||
|
@@ -248,7 +264,7 @@ export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({ | |||||
</FormControl> | ||||||
|
||||||
<TextField | ||||||
{...formHelpers("ttl", Language.ttlHelperText)} | ||||||
{...formHelpers("ttl", ttlShutdownAt(now, workspace, form.values.timezone, form.values.ttl))} | ||||||
disabled={isLoading} | ||||||
inputProps={{ min: 0, step: 1 }} | ||||||
label={Language.ttlLabel} | ||||||
|
@@ -262,6 +278,23 @@ export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({ | |||||
) | ||||||
} | ||||||
|
||||||
const ttlShutdownAt = (now: dayjs.Dayjs, workspace: Workspace, tz: string, newTTL: number): string => { | ||||||
if (workspace.latest_build.transition !== "start") { | ||||||
return Language.ttlHelperText | ||||||
} | ||||||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if (newTTL === 0) { | ||||||
return Language.ttlCausesNoShutdownHelperText | ||||||
} | ||||||
const newDeadline = dayjs(workspace.latest_build.updated_at).add(newTTL, "hour") | ||||||
if (newDeadline.isBefore(now)) { | ||||||
return Language.ttlCausesShutdownHelperText + Language.ttlCausesShutdownImmediately | ||||||
} | ||||||
if (newDeadline.isBefore(now.add(30, "minute"))) { | ||||||
return Language.ttlCausesShutdownHelperText + Language.ttlCausesShutdownSoon | ||||||
} | ||||||
return Language.ttlCausesShutdownHelperText + Language.ttlCausesShutdownAt + newDeadline.tz(tz).format("hh:mm A z") | ||||||
} | ||||||
|
||||||
const useStyles = makeStyles({ | ||||||
form: { | ||||||
"& input": { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Praise: Thank you for making this constant.