|
| 1 | +import Checkbox from "@material-ui/core/Checkbox" |
| 2 | +import FormControl from "@material-ui/core/FormControl" |
| 3 | +import FormControlLabel from "@material-ui/core/FormControlLabel" |
| 4 | +import FormGroup from "@material-ui/core/FormGroup" |
| 5 | +import FormHelperText from "@material-ui/core/FormHelperText" |
| 6 | +import FormLabel from "@material-ui/core/FormLabel" |
| 7 | +import makeStyles from "@material-ui/core/styles/makeStyles" |
| 8 | +import TextField from "@material-ui/core/TextField" |
| 9 | +import { useFormik } from "formik" |
| 10 | +import React from "react" |
| 11 | +import * as Yup from "yup" |
| 12 | +import { getFormHelpers } from "../../util/formUtils" |
| 13 | +import { FormFooter } from "../FormFooter/FormFooter" |
| 14 | +import { FullPageForm } from "../FullPageForm/FullPageForm" |
| 15 | +import { Stack } from "../Stack/Stack" |
| 16 | + |
| 17 | +export const Language = { |
| 18 | + errorNoDayOfWeek: "Must set at least one day of week", |
| 19 | + daysOfWeekLabel: "Days of Week", |
| 20 | + daySundayLabel: "Sunday", |
| 21 | + dayMondayLabel: "Monday", |
| 22 | + dayTuesdayLabel: "Tuesday", |
| 23 | + dayWednesdayLabel: "Wednesday", |
| 24 | + dayThursdayLabel: "Thursday", |
| 25 | + dayFridayLabel: "Friday", |
| 26 | + daySaturdayLabel: "Saturday", |
| 27 | + startTimeLabel: "Start time", |
| 28 | + startTimeHelperText: "Your workspace will automatically start at this time.", |
| 29 | + ttlLabel: "Runtime (minutes)", |
| 30 | + ttlHelperText: "Your workspace will automatically shutdown after the runtime.", |
| 31 | +} |
| 32 | + |
| 33 | +export interface WorkspaceScheduleFormProps { |
| 34 | + onCancel: () => void |
| 35 | + onSubmit: (values: WorkspaceScheduleFormValues) => Promise<void> |
| 36 | +} |
| 37 | + |
| 38 | +export interface WorkspaceScheduleFormValues { |
| 39 | + sunday: boolean |
| 40 | + monday: boolean |
| 41 | + tuesday: boolean |
| 42 | + wednesday: boolean |
| 43 | + thursday: boolean |
| 44 | + friday: boolean |
| 45 | + saturday: boolean |
| 46 | + |
| 47 | + startTime: string |
| 48 | + ttl: number |
| 49 | +} |
| 50 | + |
| 51 | +export const validationSchema = Yup.object({ |
| 52 | + sunday: Yup.boolean(), |
| 53 | + monday: Yup.boolean().test("at-least-one-day", Language.errorNoDayOfWeek, function (value) { |
| 54 | + const parent = this.parent as WorkspaceScheduleFormValues |
| 55 | + |
| 56 | + if (!parent.startTime) { |
| 57 | + return true |
| 58 | + } else { |
| 59 | + return ![ |
| 60 | + parent.sunday, |
| 61 | + value, |
| 62 | + parent.tuesday, |
| 63 | + parent.wednesday, |
| 64 | + parent.thursday, |
| 65 | + parent.friday, |
| 66 | + parent.saturday, |
| 67 | + ].every((day) => day === false) |
| 68 | + } |
| 69 | + }), |
| 70 | + tuesday: Yup.boolean(), |
| 71 | + wednesday: Yup.boolean(), |
| 72 | + thursday: Yup.boolean(), |
| 73 | + friday: Yup.boolean(), |
| 74 | + saturday: Yup.boolean(), |
| 75 | + |
| 76 | + startTime: Yup.string(), |
| 77 | + ttl: Yup.number().min(0).integer(), |
| 78 | +}) |
| 79 | + |
| 80 | +export const WorkspaceScheduleForm: React.FC<WorkspaceScheduleFormProps> = ({ onCancel, onSubmit }) => { |
| 81 | + const styles = useStyles() |
| 82 | + |
| 83 | + const form = useFormik<WorkspaceScheduleFormValues>({ |
| 84 | + initialValues: { |
| 85 | + sunday: false, |
| 86 | + monday: true, |
| 87 | + tuesday: true, |
| 88 | + wednesday: true, |
| 89 | + thursday: true, |
| 90 | + friday: true, |
| 91 | + saturday: false, |
| 92 | + |
| 93 | + startTime: "09:30", |
| 94 | + ttl: 120, |
| 95 | + }, |
| 96 | + onSubmit, |
| 97 | + validationSchema, |
| 98 | + }) |
| 99 | + const formHelpers = getFormHelpers<WorkspaceScheduleFormValues>(form) |
| 100 | + |
| 101 | + return ( |
| 102 | + <FullPageForm onCancel={onCancel} title="Workspace Schedule"> |
| 103 | + <form className={styles.form} onSubmit={form.handleSubmit}> |
| 104 | + <Stack className={styles.stack}> |
| 105 | + <TextField |
| 106 | + {...formHelpers("startTime", Language.startTimeHelperText)} |
| 107 | + InputLabelProps={{ |
| 108 | + shrink: true, |
| 109 | + }} |
| 110 | + label={Language.startTimeLabel} |
| 111 | + type="time" |
| 112 | + variant="standard" |
| 113 | + /> |
| 114 | + |
| 115 | + <FormControl component="fieldset" error={Boolean(form.errors.monday)}> |
| 116 | + <FormLabel className={styles.daysOfWeekLabel} component="legend"> |
| 117 | + {Language.daysOfWeekLabel} |
| 118 | + </FormLabel> |
| 119 | + |
| 120 | + <FormGroup> |
| 121 | + <FormControlLabel |
| 122 | + control={ |
| 123 | + <Checkbox |
| 124 | + checked={form.values.sunday} |
| 125 | + disabled={!form.values.startTime} |
| 126 | + onChange={form.handleChange} |
| 127 | + name="sunday" |
| 128 | + /> |
| 129 | + } |
| 130 | + label={Language.daySundayLabel} |
| 131 | + /> |
| 132 | + <FormControlLabel |
| 133 | + control={ |
| 134 | + <Checkbox |
| 135 | + checked={form.values.monday} |
| 136 | + disabled={!form.values.startTime} |
| 137 | + onChange={form.handleChange} |
| 138 | + name="monday" |
| 139 | + /> |
| 140 | + } |
| 141 | + label={Language.dayMondayLabel} |
| 142 | + /> |
| 143 | + <FormControlLabel |
| 144 | + control={ |
| 145 | + <Checkbox |
| 146 | + checked={form.values.tuesday} |
| 147 | + disabled={!form.values.startTime} |
| 148 | + onChange={form.handleChange} |
| 149 | + name="tuesday" |
| 150 | + /> |
| 151 | + } |
| 152 | + label={Language.dayTuesdayLabel} |
| 153 | + /> |
| 154 | + <FormControlLabel |
| 155 | + control={ |
| 156 | + <Checkbox |
| 157 | + checked={form.values.wednesday} |
| 158 | + disabled={!form.values.startTime} |
| 159 | + onChange={form.handleChange} |
| 160 | + name="wednesday" |
| 161 | + /> |
| 162 | + } |
| 163 | + label={Language.dayWednesdayLabel} |
| 164 | + /> |
| 165 | + <FormControlLabel |
| 166 | + control={ |
| 167 | + <Checkbox |
| 168 | + checked={form.values.thursday} |
| 169 | + disabled={!form.values.startTime} |
| 170 | + onChange={form.handleChange} |
| 171 | + name="thursday" |
| 172 | + /> |
| 173 | + } |
| 174 | + label={Language.dayThursdayLabel} |
| 175 | + /> |
| 176 | + <FormControlLabel |
| 177 | + control={ |
| 178 | + <Checkbox |
| 179 | + checked={form.values.friday} |
| 180 | + disabled={!form.values.startTime} |
| 181 | + onChange={form.handleChange} |
| 182 | + name="friday" |
| 183 | + /> |
| 184 | + } |
| 185 | + label={Language.dayFridayLabel} |
| 186 | + /> |
| 187 | + <FormControlLabel |
| 188 | + control={ |
| 189 | + <Checkbox |
| 190 | + checked={form.values.saturday} |
| 191 | + disabled={!form.values.startTime} |
| 192 | + onChange={form.handleChange} |
| 193 | + name="saturday" |
| 194 | + /> |
| 195 | + } |
| 196 | + label={Language.daySaturdayLabel} |
| 197 | + /> |
| 198 | + </FormGroup> |
| 199 | + {form.errors.monday && <FormHelperText>{Language.errorNoDayOfWeek}</FormHelperText>} |
| 200 | + </FormControl> |
| 201 | + |
| 202 | + <TextField |
| 203 | + {...formHelpers("ttl", Language.ttlHelperText)} |
| 204 | + inputProps={{ min: 0, step: 30 }} |
| 205 | + label={Language.ttlLabel} |
| 206 | + type="number" |
| 207 | + variant="standard" |
| 208 | + /> |
| 209 | + |
| 210 | + <FormFooter onCancel={onCancel} isLoading={form.isSubmitting} /> |
| 211 | + </Stack> |
| 212 | + </form> |
| 213 | + </FullPageForm> |
| 214 | + ) |
| 215 | +} |
| 216 | + |
| 217 | +const useStyles = makeStyles({ |
| 218 | + form: { |
| 219 | + display: "flex", |
| 220 | + justifyContent: "center", |
| 221 | + }, |
| 222 | + stack: { |
| 223 | + // REMARK: 360 is 'arbitrary' in that it gives the helper text enough room |
| 224 | + // to render on one line. If we change the text, we might want to |
| 225 | + // adjust these. Without constraining the width, the date picker |
| 226 | + // and number inputs aren't visually appealing or maximally usable. |
| 227 | + maxWidth: 360, |
| 228 | + minWidth: 360, |
| 229 | + }, |
| 230 | + daysOfWeekLabel: { |
| 231 | + fontSize: 12, |
| 232 | + }, |
| 233 | +}) |
0 commit comments