From 4c3a655fa8302f3c397ad67969b85c5b6bdce143 Mon Sep 17 00:00:00 2001 From: G r e y Date: Thu, 2 Jun 2022 23:11:30 +0000 Subject: [PATCH] fix: ws schedule top-down restriction Resolves: #1958 Summary: The workspace schedule form no longer disables certain fields based on whether or not a start time is filled out. Instead, we validate that a start time is provided if any of the days are checked. --- .../WorkspaceScheduleForm.test.ts | 16 +++++++++++++ .../WorkspaceScheduleForm.tsx | 24 +++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.test.ts b/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.test.ts index 31cc6a4eac553..84764dd42e543 100644 --- a/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.test.ts +++ b/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.test.ts @@ -57,6 +57,22 @@ describe("validationSchema", () => { expect(validate).toThrowError(Language.errorNoDayOfWeek) }) + it("disallows empty startTime when at least one day is set", () => { + const values: WorkspaceScheduleFormValues = { + ...valid, + sunday: false, + monday: true, + tuesday: false, + wednesday: false, + thursday: false, + friday: false, + saturday: false, + startTime: "", + } + const validate = () => validationSchema.validateSync(values) + expect(validate).toThrowError(Language.errorNoTime) + }) + it("allows startTime 16:20", () => { const values: WorkspaceScheduleFormValues = { ...valid, diff --git a/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx b/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx index bea970134c5b5..9ca762f34348a 100644 --- a/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx +++ b/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx @@ -27,6 +27,7 @@ dayjs.extend(timezone) export const Language = { errorNoDayOfWeek: "Must set at least one day of week", + errorNoTime: "Start time is required", errorTime: "Time must be in HH:mm format (24 hours)", errorTimezone: "Invalid timezone", daysOfWeekLabel: "Days of Week", @@ -93,6 +94,25 @@ export const validationSchema = Yup.object({ startTime: Yup.string() .ensure() + .test("required-if-day-selected", Language.errorNoTime, function (value) { + const parent = this.parent as WorkspaceScheduleFormValues + + const isDaySelected = [ + parent.sunday, + parent.monday, + parent.tuesday, + parent.wednesday, + parent.thursday, + parent.friday, + parent.saturday, + ].some((day) => day) + + if (isDaySelected) { + return value !== "" + } else { + return true + } + }) .test("is-time-string", Language.errorTime, (value) => { if (value === "") { return true @@ -192,7 +212,7 @@ export const WorkspaceScheduleForm: FC = ({ , )} - disabled={isLoading || !form.values.startTime} + disabled={isLoading} InputLabelProps={{ shrink: true, }} @@ -210,7 +230,7 @@ export const WorkspaceScheduleForm: FC = ({ control={