Skip to content

Commit d61a332

Browse files
committed
refactor to map + add loading/disabled
1 parent c0f28c3 commit d61a332

File tree

2 files changed

+29
-81
lines changed

2 files changed

+29
-81
lines changed

site/src/components/WorkspaceStats/WorkspaceScheduleForm.tsx

Lines changed: 27 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,23 @@ export const WorkspaceScheduleForm: React.FC<WorkspaceScheduleFormProps> = ({ is
100100
})
101101
const formHelpers = getFormHelpers<WorkspaceScheduleFormValues>(form)
102102

103+
const checkboxes: Array<{ value: boolean; name: string; label: string }> = [
104+
{ value: form.values.sunday, name: "sunday", label: Language.daySundayLabel },
105+
{ value: form.values.monday, name: "monday", label: Language.dayMondayLabel },
106+
{ value: form.values.tuesday, name: "tuesday", label: Language.dayTuesdayLabel },
107+
{ value: form.values.wednesday, name: "wednesday", label: Language.dayWednesdayLabel },
108+
{ value: form.values.thursday, name: "thursday", label: Language.dayThursdayLabel },
109+
{ value: form.values.friday, name: "friday", label: Language.dayFridayLabel },
110+
{ value: form.values.saturday, name: "saturday", label: Language.daySaturdayLabel },
111+
]
112+
103113
return (
104114
<FullPageForm onCancel={onCancel} title="Workspace Schedule">
105115
<form className={styles.form} onSubmit={form.handleSubmit}>
106116
<Stack className={styles.stack}>
107117
<TextField
108118
{...formHelpers("startTime", Language.startTimeHelperText)}
119+
disabled={form.isSubmitting || isLoading}
109120
InputLabelProps={{
110121
shrink: true,
111122
}}
@@ -120,89 +131,28 @@ export const WorkspaceScheduleForm: React.FC<WorkspaceScheduleFormProps> = ({ is
120131
</FormLabel>
121132

122133
<FormGroup>
123-
<FormControlLabel
124-
control={
125-
<Checkbox
126-
checked={form.values.sunday}
127-
disabled={!form.values.startTime}
128-
onChange={form.handleChange}
129-
name="sunday"
130-
/>
131-
}
132-
label={Language.daySundayLabel}
133-
/>
134-
<FormControlLabel
135-
control={
136-
<Checkbox
137-
checked={form.values.monday}
138-
disabled={!form.values.startTime}
139-
onChange={form.handleChange}
140-
name="monday"
141-
/>
142-
}
143-
label={Language.dayMondayLabel}
144-
/>
145-
<FormControlLabel
146-
control={
147-
<Checkbox
148-
checked={form.values.tuesday}
149-
disabled={!form.values.startTime}
150-
onChange={form.handleChange}
151-
name="tuesday"
152-
/>
153-
}
154-
label={Language.dayTuesdayLabel}
155-
/>
156-
<FormControlLabel
157-
control={
158-
<Checkbox
159-
checked={form.values.wednesday}
160-
disabled={!form.values.startTime}
161-
onChange={form.handleChange}
162-
name="wednesday"
163-
/>
164-
}
165-
label={Language.dayWednesdayLabel}
166-
/>
167-
<FormControlLabel
168-
control={
169-
<Checkbox
170-
checked={form.values.thursday}
171-
disabled={!form.values.startTime}
172-
onChange={form.handleChange}
173-
name="thursday"
174-
/>
175-
}
176-
label={Language.dayThursdayLabel}
177-
/>
178-
<FormControlLabel
179-
control={
180-
<Checkbox
181-
checked={form.values.friday}
182-
disabled={!form.values.startTime}
183-
onChange={form.handleChange}
184-
name="friday"
185-
/>
186-
}
187-
label={Language.dayFridayLabel}
188-
/>
189-
<FormControlLabel
190-
control={
191-
<Checkbox
192-
checked={form.values.saturday}
193-
disabled={!form.values.startTime}
194-
onChange={form.handleChange}
195-
name="saturday"
196-
/>
197-
}
198-
label={Language.daySaturdayLabel}
199-
/>
134+
{checkboxes.map((checkbox) => (
135+
<FormControlLabel
136+
control={
137+
<Checkbox
138+
checked={checkbox.value}
139+
disabled={!form.values.startTime || form.isSubmitting || isLoading}
140+
onChange={form.handleChange}
141+
name={checkbox.name}
142+
/>
143+
}
144+
key={checkbox.name}
145+
label={checkbox.label}
146+
/>
147+
))}
200148
</FormGroup>
149+
201150
{form.errors.monday && <FormHelperText>{Language.errorNoDayOfWeek}</FormHelperText>}
202151
</FormControl>
203152

204153
<TextField
205154
{...formHelpers("ttl", Language.ttlHelperText)}
155+
disabled={form.isSubmitting || isLoading}
206156
inputProps={{ min: 0, step: 30 }}
207157
label={Language.ttlLabel}
208158
type="number"

site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ export const WorkspaceSchedulePage: React.FC = () => {
5050
const [scheduleState, scheduleSend] = useMachine(workspaceSchedule)
5151
const { getWorkspaceError, workspace } = scheduleState.context
5252

53-
/**
54-
* Get workspace on mount and whenever workspaceId changes (scheduleSend
55-
* should not change).
56-
*/
53+
// Get workspace on mount and whenever workspaceId changes.
54+
// scheduleSend should not change.
5755
useEffect(() => {
5856
workspaceId && scheduleSend({ type: "GET_WORKSPACE", workspaceId })
5957
}, [workspaceId, scheduleSend])

0 commit comments

Comments
 (0)