Skip to content

Commit 82cd298

Browse files
committed
feat(frontend): auto generate title of period
1 parent 8942c8a commit 82cd298

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

apps/frontend/src/components/questionnaire/PeriodForm.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { useForm } from "@mantine/form";
2-
import { Button, Flex, MonthPicker, Stack, TextInput } from "@quassel/ui";
2+
import { Button, Flex, formatDate, MonthPicker, Stack, TextInput } from "@quassel/ui";
33
import { i18n } from "../../stores/i18n";
44
import { useStore } from "@nanostores/react";
55
import { useEffect } from "react";
6+
import { params } from "@nanostores/i18n";
67

78
export type PeriodFormValues = {
89
title: string;
@@ -17,14 +18,19 @@ type PeriodFormProps = {
1718

1819
const messages = i18n("periodForm", {
1920
labelTitle: "Title",
21+
defaultTitle: params("Period ({start} - {end})"),
2022
});
2123

2224
export function PeriodForm({ onSave, actionLabel, period }: PeriodFormProps) {
2325
const t = useStore(messages);
2426

2527
const f = useForm<PeriodFormValues>({
2628
initialValues: period,
27-
mode: "uncontrolled",
29+
onValuesChange({ range: [newStart, newEnd] }, { range: [prevStart, prevEnd] }) {
30+
if ((!prevStart || !prevEnd) && newStart && newEnd) {
31+
f.setFieldValue("title", t.defaultTitle({ start: formatDate(newStart, "M/YY"), end: formatDate(newEnd, "M/YY") }));
32+
}
33+
},
2834
});
2935

3036
useEffect(() => {

libs/ui/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import "@mantine/core/styles/Combobox.css";
2929
import "@mantine/core/styles/Stack.css";
3030
import "@mantine/dates/styles.css";
3131

32+
import { formatDate } from "./utils/date";
33+
3234
export { ThemeProvider } from "./theme/ThemeProvider";
3335

3436
// custom components
@@ -69,3 +71,5 @@ export {
6971
IconCalendarWeek,
7072
IconMapSearch,
7173
} from "@tabler/icons-react";
74+
75+
export { formatDate };

libs/ui/src/utils/date.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import dayjs from "dayjs";
2+
3+
export function formatDate(date: Date, dayjsFormatTemplate: string) {
4+
return dayjs(date).format(dayjsFormatTemplate);
5+
}

0 commit comments

Comments
 (0)