Skip to content

feat: add quiet hours settings page #9676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
🧹
  • Loading branch information
aslilac committed Sep 15, 2023
commit 409efffe256b453d4f88b7f7c1a2244dfbcfa0b3
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const ExampleDefault: Story = {};

export const ExampleUserSet: Story = {
args: {
...defaultArgs,
initialValues: {
raw_schedule: "CRON_TZ=America/Chicago 0 2 * * *",
user_set: true,
Expand All @@ -44,15 +43,13 @@ export const ExampleUserSet: Story = {

export const Submitting: Story = {
args: {
...defaultArgs,
isLoading: true,
},
};

export const WithError: Story = {
args: {
...defaultArgs,
mutationError: mockApiError({
submitError: mockApiError({
message: "Invalid schedule",
validations: [
{
Expand Down
16 changes: 5 additions & 11 deletions site/src/pages/UserSettingsPage/SchedulePage/ScheduleForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ const validationSchema = Yup.object({
time: Yup.string()
.ensure()
.test("is-time-string", "Time must be in HH:mm format.", (value) => {
if (value === "") {
return true;
}
if (!/^[0-9][0-9]:[0-9][0-9]$/.test(value)) {
return false;
}
Expand All @@ -42,7 +39,7 @@ const validationSchema = Yup.object({
export interface ScheduleFormProps {
isLoading: boolean;
initialValues: UserQuietHoursScheduleResponse;
mutationError: unknown;
submitError: unknown;
onSubmit: (data: UpdateUserQuietHoursScheduleRequest) => void;
// now can be set to force the time used for "Next occurrence" in tests.
now?: Date;
Expand All @@ -51,7 +48,7 @@ export interface ScheduleFormProps {
export const ScheduleForm: FC<React.PropsWithChildren<ScheduleFormProps>> = ({
isLoading,
initialValues,
mutationError,
submitError,
onSubmit,
now,
}) => {
Expand All @@ -75,21 +72,18 @@ export const ScheduleForm: FC<React.PropsWithChildren<ScheduleFormProps>> = ({
useFormik<ScheduleFormValues>({
initialValues: formInitialValues,
validationSchema,
onSubmit: async (values) => {
onSubmit: (values) => {
onSubmit({
schedule: timeToCron(values.time, values.timezone),
});
},
});
const getFieldHelpers = getFormHelpers<ScheduleFormValues>(
form,
mutationError,
);
const getFieldHelpers = getFormHelpers<ScheduleFormValues>(form, submitError);

return (
<Form onSubmit={form.handleSubmit}>
<FormFields>
{Boolean(mutationError) && <ErrorAlert error={mutationError} />}
{Boolean(submitError) && <ErrorAlert error={submitError} />}

{!initialValues.user_set && (
<Alert severity="info">
Expand Down
21 changes: 10 additions & 11 deletions site/src/pages/UserSettingsPage/SchedulePage/SchedulePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,11 @@ export const SchedulePage: FC = () => {
isError,
} = useQuery(userQuietHoursSchedule(me.id));

const updateSchedule = updateUserQuietHoursSchedule(me.id, queryClient);
const {
mutate: onSubmit,
error: mutationError,
error: submitError,
isLoading: mutationLoading,
} = useMutation({
...updateSchedule,
onSuccess: async () => {
await updateSchedule.onSuccess();
displaySuccess("Schedule updated successfully");
},
});
} = useMutation(updateUserQuietHoursSchedule(me.id, queryClient));

if (isLoading) {
return <Loader />;
Expand All @@ -52,8 +45,14 @@ export const SchedulePage: FC = () => {
<ScheduleForm
isLoading={mutationLoading}
initialValues={quietHoursSchedule}
mutationError={mutationError}
onSubmit={onSubmit}
submitError={submitError}
onSubmit={(values) => {
onSubmit(values, {
onSuccess: () => {
displaySuccess("Schedule updated successfully");
},
});
}}
/>
</Section>
);
Expand Down