mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
Format
This commit is contained in:
@@ -209,80 +209,80 @@ export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({
|
||||
<Stack>
|
||||
{submitScheduleError && <ErrorSummary error={submitScheduleError} />}
|
||||
<Section title="Start">
|
||||
<FormControlLabel
|
||||
control={<Switch checked={autoStart.enabled} onChange={toggleAutoStart} />}
|
||||
label="Auto-start"
|
||||
/>
|
||||
<TextField
|
||||
{...formHelpers("startTime", Language.startTimeHelperText)}
|
||||
disabled={isLoading || !autoStart.enabled}
|
||||
InputLabelProps={{
|
||||
shrink: true,
|
||||
}}
|
||||
label={Language.startTimeLabel}
|
||||
type="time"
|
||||
fullWidth
|
||||
/>
|
||||
<FormControlLabel
|
||||
control={<Switch checked={autoStart.enabled} onChange={toggleAutoStart} />}
|
||||
label="Auto-start"
|
||||
/>
|
||||
<TextField
|
||||
{...formHelpers("startTime", Language.startTimeHelperText)}
|
||||
disabled={isLoading || !autoStart.enabled}
|
||||
InputLabelProps={{
|
||||
shrink: true,
|
||||
}}
|
||||
label={Language.startTimeLabel}
|
||||
type="time"
|
||||
fullWidth
|
||||
/>
|
||||
|
||||
<TextField
|
||||
{...formHelpers("timezone")}
|
||||
disabled={isLoading || !autoStart.enabled}
|
||||
InputLabelProps={{
|
||||
shrink: true,
|
||||
}}
|
||||
label={Language.timezoneLabel}
|
||||
select
|
||||
fullWidth
|
||||
>
|
||||
{zones.map((zone) => (
|
||||
<MenuItem key={zone} value={zone}>
|
||||
{zone}
|
||||
</MenuItem>
|
||||
))}
|
||||
</TextField>
|
||||
|
||||
<FormControl component="fieldset" error={Boolean(form.errors.monday)}>
|
||||
<FormLabel className={styles.daysOfWeekLabel} component="legend">
|
||||
{Language.daysOfWeekLabel}
|
||||
</FormLabel>
|
||||
|
||||
<FormGroup>
|
||||
{checkboxes.map((checkbox) => (
|
||||
<FormControlLabel
|
||||
control={
|
||||
<Checkbox
|
||||
checked={checkbox.value}
|
||||
disabled={isLoading || !autoStart.enabled}
|
||||
onChange={form.handleChange}
|
||||
name={checkbox.name}
|
||||
color="primary"
|
||||
size="small"
|
||||
disableRipple
|
||||
/>
|
||||
}
|
||||
key={checkbox.name}
|
||||
label={checkbox.label}
|
||||
/>
|
||||
<TextField
|
||||
{...formHelpers("timezone")}
|
||||
disabled={isLoading || !autoStart.enabled}
|
||||
InputLabelProps={{
|
||||
shrink: true,
|
||||
}}
|
||||
label={Language.timezoneLabel}
|
||||
select
|
||||
fullWidth
|
||||
>
|
||||
{zones.map((zone) => (
|
||||
<MenuItem key={zone} value={zone}>
|
||||
{zone}
|
||||
</MenuItem>
|
||||
))}
|
||||
</FormGroup>
|
||||
</TextField>
|
||||
|
||||
{form.errors.monday && <FormHelperText>{Language.errorNoDayOfWeek}</FormHelperText>}
|
||||
</FormControl>
|
||||
<FormControl component="fieldset" error={Boolean(form.errors.monday)}>
|
||||
<FormLabel className={styles.daysOfWeekLabel} component="legend">
|
||||
{Language.daysOfWeekLabel}
|
||||
</FormLabel>
|
||||
|
||||
<FormGroup>
|
||||
{checkboxes.map((checkbox) => (
|
||||
<FormControlLabel
|
||||
control={
|
||||
<Checkbox
|
||||
checked={checkbox.value}
|
||||
disabled={isLoading || !autoStart.enabled}
|
||||
onChange={form.handleChange}
|
||||
name={checkbox.name}
|
||||
color="primary"
|
||||
size="small"
|
||||
disableRipple
|
||||
/>
|
||||
}
|
||||
key={checkbox.name}
|
||||
label={checkbox.label}
|
||||
/>
|
||||
))}
|
||||
</FormGroup>
|
||||
|
||||
{form.errors.monday && <FormHelperText>{Language.errorNoDayOfWeek}</FormHelperText>}
|
||||
</FormControl>
|
||||
</Section>
|
||||
|
||||
<Section title="Stop">
|
||||
<FormControlLabel
|
||||
control={<Switch checked={autoStop.enabled} onChange={toggleAutoStop} />}
|
||||
label="Auto-Stop"
|
||||
/>
|
||||
<TextField
|
||||
{...formHelpers("ttl", ttlShutdownAt(form.values.ttl), "ttl_ms")}
|
||||
disabled={isLoading || !autoStop.enabled}
|
||||
inputProps={{ min: 0, step: 1 }}
|
||||
label={Language.ttlLabel}
|
||||
type="number"
|
||||
fullWidth
|
||||
/>
|
||||
<FormControlLabel
|
||||
control={<Switch checked={autoStop.enabled} onChange={toggleAutoStop} />}
|
||||
label="Auto-Stop"
|
||||
/>
|
||||
<TextField
|
||||
{...formHelpers("ttl", ttlShutdownAt(form.values.ttl), "ttl_ms")}
|
||||
disabled={isLoading || !autoStop.enabled}
|
||||
inputProps={{ min: 0, step: 1 }}
|
||||
label={Language.ttlLabel}
|
||||
type="number"
|
||||
fullWidth
|
||||
/>
|
||||
</Section>
|
||||
|
||||
<FormFooter onCancel={onCancel} isLoading={isLoading} />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useMachine, useSelector } from "@xstate/react"
|
||||
import { defaultSchedule, emptySchedule, scheduleToAutoStart } from "pages/WorkspacesPage/schedule"
|
||||
import { ttlMsToAutoStop, emptyTTL, defaultTTL } from "pages/WorkspacesPage/ttl"
|
||||
import { defaultTTL, emptyTTL, ttlMsToAutoStop } from "pages/WorkspacesPage/ttl"
|
||||
import React, { useContext, useEffect, useState } from "react"
|
||||
import { useNavigate, useParams } from "react-router-dom"
|
||||
import * as TypesGen from "../../api/typesGenerated"
|
||||
@@ -116,7 +116,8 @@ export const WorkspaceSchedulePage: React.FC = () => {
|
||||
username && workspaceName && scheduleSend({ type: "GET_WORKSPACE", username, workspaceName })
|
||||
}, [username, workspaceName, scheduleSend])
|
||||
|
||||
const getAutoStart = (workspace?: TypesGen.Workspace) => scheduleToAutoStart(workspace?.autostart_schedule)
|
||||
const getAutoStart = (workspace?: TypesGen.Workspace) =>
|
||||
scheduleToAutoStart(workspace?.autostart_schedule)
|
||||
const getAutoStop = (workspace?: TypesGen.Workspace) => ttlMsToAutoStop(workspace?.ttl_ms)
|
||||
|
||||
const [autoStart, setAutoStart] = useState(getAutoStart(workspace))
|
||||
@@ -131,20 +132,20 @@ export const WorkspaceSchedulePage: React.FC = () => {
|
||||
if (autoStart.enabled) {
|
||||
setAutoStart({
|
||||
enabled: false,
|
||||
schedule: emptySchedule
|
||||
schedule: emptySchedule,
|
||||
})
|
||||
} else {
|
||||
if (workspace?.autostart_schedule) {
|
||||
// repopulate saved schedule
|
||||
setAutoStart({
|
||||
enabled: true,
|
||||
schedule: getAutoStart(workspace).schedule
|
||||
schedule: getAutoStart(workspace).schedule,
|
||||
})
|
||||
} else {
|
||||
// populate with defaults
|
||||
setAutoStart({
|
||||
enabled: true,
|
||||
schedule: defaultSchedule()
|
||||
schedule: defaultSchedule(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -154,20 +155,20 @@ export const WorkspaceSchedulePage: React.FC = () => {
|
||||
if (autoStop.enabled) {
|
||||
setAutoStop({
|
||||
enabled: false,
|
||||
ttl: emptyTTL
|
||||
ttl: emptyTTL,
|
||||
})
|
||||
} else {
|
||||
if (workspace?.ttl_ms) {
|
||||
// repopulate saved ttl
|
||||
setAutoStop({
|
||||
enabled: true,
|
||||
ttl: getAutoStop(workspace).ttl
|
||||
ttl: getAutoStop(workspace).ttl,
|
||||
})
|
||||
} else {
|
||||
// set default
|
||||
setAutoStop({
|
||||
enabled: true,
|
||||
ttl: defaultTTL
|
||||
ttl: defaultTTL,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,7 @@ export const emptySchedule = {
|
||||
timezone: "",
|
||||
}
|
||||
|
||||
export const defaultSchedule = (
|
||||
): AutoStartSchedule => ({
|
||||
export const defaultSchedule = (): AutoStartSchedule => ({
|
||||
sunday: false,
|
||||
monday: true,
|
||||
tuesday: true,
|
||||
@@ -85,7 +84,7 @@ export const scheduleToAutoStart = (schedule?: string): AutoStart => {
|
||||
if (schedule) {
|
||||
return {
|
||||
enabled: true,
|
||||
schedule: transformSchedule(schedule)
|
||||
schedule: transformSchedule(schedule),
|
||||
}
|
||||
} else {
|
||||
return { enabled: false, schedule: emptySchedule }
|
||||
|
||||
@@ -9,6 +9,5 @@ export const defaultTTL = 8
|
||||
|
||||
const msToHours = (ms: number) => Math.round(ms / (1000 * 60 * 60))
|
||||
|
||||
export const ttlMsToAutoStop = (ttl_ms?: number): AutoStop => (
|
||||
export const ttlMsToAutoStop = (ttl_ms?: number): AutoStop =>
|
||||
ttl_ms ? { enabled: true, ttl: msToHours(ttl_ms) } : { enabled: false, ttl: 0 }
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user