Skip to content

Commit 4842ffa

Browse files
committed
Add stories for TemplateScheduleAutostart and move it to where it is used
1 parent 0ccc627 commit 4842ffa

File tree

4 files changed

+61
-28
lines changed

4 files changed

+61
-28
lines changed

site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
FormFooter,
1919
} from "components/Form/Form";
2020
import { IconField } from "components/IconField/IconField";
21-
import { sortedDays } from "modules/templates/TemplateScheduleAutostart/TemplateScheduleAutostart";
21+
import { sortedDays } from "pages/TemplateSettingsPage/TemplateSchedulePage/TemplateScheduleAutostart/TemplateScheduleAutostart";
2222
import { SelectedTemplate } from "pages/CreateWorkspacePage/SelectedTemplate";
2323
import {
2424
nameValidator,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { Meta, StoryObj } from "@storybook/react";
2+
import { userEvent, within } from "@storybook/test";
3+
import { useState } from "react";
4+
import type { TemplateAutostartRequirementDaysValue } from "utils/schedule";
5+
import { TemplateScheduleAutostart } from "./TemplateScheduleAutostart";
6+
7+
const meta: Meta<typeof TemplateScheduleAutostart> = {
8+
title: "modules/templates/TemplateScheduleAutostart",
9+
component: TemplateScheduleAutostart,
10+
args: {
11+
value: [],
12+
},
13+
};
14+
15+
export default meta;
16+
type Story = StoryObj<typeof TemplateScheduleAutostart>;
17+
18+
export const AllowAutoStart: Story = {
19+
args: {
20+
enabled: true,
21+
},
22+
render: function TemplateScheduleAutost(args) {
23+
const [value, setValue] = useState<TemplateAutostartRequirementDaysValue[]>(
24+
args.value,
25+
);
26+
return (
27+
<TemplateScheduleAutostart {...args} value={value} onChange={setValue} />
28+
);
29+
},
30+
play: async ({ canvasElement, step }) => {
31+
const canvas = within(canvasElement);
32+
33+
await step("select days of week", async () => {
34+
const daysToSelect = ["Mon", "Tue", "Wed", "Thu", "Fri"];
35+
for (const day of daysToSelect) {
36+
await userEvent.click(canvas.getByRole("button", { name: day }));
37+
}
38+
});
39+
},
40+
};
41+
42+
export const DisabledAutoStart: Story = {
43+
args: {
44+
enabled: false,
45+
},
46+
};

site/src/modules/templates/TemplateScheduleAutostart/TemplateScheduleAutostart.tsx renamed to site/src/pages/TemplateSettingsPage/TemplateSchedulePage/TemplateScheduleAutostart.tsx

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import { Stack } from "components/Stack/Stack";
55
import type { TemplateAutostartRequirementDaysValue } from "utils/schedule";
66

77
export interface TemplateScheduleAutostartProps {
8-
allow_user_autostart?: boolean;
9-
autostart_requirement_days_of_week: TemplateAutostartRequirementDaysValue[];
8+
enabled: boolean;
9+
value: TemplateAutostartRequirementDaysValue[];
1010
isSubmitting: boolean;
11-
onChange: (newDaysOfWeek: TemplateAutostartRequirementDaysValue[]) => void;
11+
onChange: (value: TemplateAutostartRequirementDaysValue[]) => void;
1212
}
1313

1414
export const TemplateScheduleAutostart: FC<TemplateScheduleAutostartProps> = ({
15-
autostart_requirement_days_of_week,
15+
value,
1616
isSubmitting,
17-
allow_user_autostart,
17+
enabled,
1818
onChange,
1919
}) => {
2020
return (
@@ -49,21 +49,13 @@ export const TemplateScheduleAutostart: FC<TemplateScheduleAutostartProps> = ({
4949
key={day.key}
5050
css={{ borderRadius: 0 }}
5151
// TODO: Adding a background color would also help
52-
color={
53-
autostart_requirement_days_of_week.includes(day.value)
54-
? "primary"
55-
: "secondary"
56-
}
57-
disabled={isSubmitting || !allow_user_autostart}
52+
color={value.includes(day.value) ? "primary" : "secondary"}
53+
disabled={isSubmitting || !enabled}
5854
onClick={() => {
59-
if (!autostart_requirement_days_of_week.includes(day.value)) {
60-
onChange(autostart_requirement_days_of_week.concat(day.value));
55+
if (!value.includes(day.value)) {
56+
onChange(value.concat(day.value));
6157
} else {
62-
onChange(
63-
autostart_requirement_days_of_week.filter(
64-
(obj) => obj !== day.value,
65-
),
66-
);
58+
onChange(value.filter((obj) => obj !== day.value));
6759
}
6860
}}
6961
>
@@ -72,10 +64,7 @@ export const TemplateScheduleAutostart: FC<TemplateScheduleAutostartProps> = ({
7264
))}
7365
</Stack>
7466
<FormHelperText>
75-
<AutostartHelperText
76-
allowed={allow_user_autostart}
77-
days={autostart_requirement_days_of_week}
78-
/>
67+
<AutostartHelperText allowed={enabled} days={value} />
7968
</FormHelperText>
8069
</Stack>
8170
);

site/src/pages/TemplateSettingsPage/TemplateSchedulePage/TemplateScheduleForm.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
FormFields,
1616
} from "components/Form/Form";
1717
import { Stack } from "components/Stack/Stack";
18-
import { TemplateScheduleAutostart } from "modules/templates/TemplateScheduleAutostart/TemplateScheduleAutostart";
1918
import { docs } from "utils/docs";
2019
import { getFormHelpers } from "utils/formUtils";
2120
import {
@@ -32,6 +31,7 @@ import {
3231
type TemplateScheduleFormValues,
3332
} from "./formHelpers";
3433
import { ScheduleDialog } from "./ScheduleDialog";
34+
import { TemplateScheduleAutostart } from "./TemplateScheduleAutostart";
3535
import {
3636
ActivityBumpHelperText,
3737
DefaultTTLHelperText,
@@ -535,10 +535,8 @@ export const TemplateScheduleForm: FC<TemplateScheduleForm> = ({
535535
</Stack>
536536
{allowAdvancedScheduling && (
537537
<TemplateScheduleAutostart
538-
allow_user_autostart={form.values.allow_user_autostart}
539-
autostart_requirement_days_of_week={
540-
form.values.autostart_requirement_days_of_week
541-
}
538+
enabled={Boolean(form.values.allow_user_autostart)}
539+
value={form.values.autostart_requirement_days_of_week}
542540
isSubmitting={isSubmitting}
543541
onChange={async (
544542
newDaysOfWeek: TemplateAutostartRequirementDaysValue[],

0 commit comments

Comments
 (0)