Skip to content

Commit 9921903

Browse files
committed
refactor: use string dates in mantine components
1 parent 1bb4ef7 commit 9921903

File tree

8 files changed

+36
-37
lines changed

8 files changed

+36
-37
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Button, Flex, InputError, MonthPicker, Stack, TextInput, useForm } from "@quassel/ui";
2-
import { formatDate } from "@quassel/utils";
2+
import { formatDate, getStartOf, isSame } from "@quassel/utils";
33
import { i18n } from "../../stores/i18n";
44
import { useStore } from "@nanostores/react";
55
import { useEffect } from "react";
66
import { params } from "@nanostores/i18n";
77

88
export type PeriodFormValues = {
99
title: string;
10-
range: [Date | null, Date | null];
10+
range: [string | null, string | null];
1111
};
1212

1313
type PeriodFormProps = {
@@ -32,7 +32,7 @@ export function PeriodForm({ onSave, actionLabel, period }: PeriodFormProps) {
3232
},
3333
validate: {
3434
range([start]) {
35-
if (period?.range[0] && +period.range[0] !== +start!) {
35+
if (period?.range[0] && !isSame("day", new Date(period.range[0]), new Date(start!))) {
3636
return t.validationStartDate;
3737
}
3838
},
@@ -41,14 +41,19 @@ export function PeriodForm({ onSave, actionLabel, period }: PeriodFormProps) {
4141
const [newStart, newEnd] = newValues.range ?? [];
4242
const [prevStart, prevEnd] = prevValues.range ?? [];
4343
if ((!prevStart || !prevEnd) && newStart && newEnd) {
44-
f.setFieldValue("title", t.defaultTitle({ start: formatDate(newStart, "M/YY"), end: formatDate(newEnd, "M/YY") }));
44+
f.setFieldValue("title", t.defaultTitle({ start: formatDate(new Date(newStart), "M/YY"), end: formatDate(new Date(newEnd), "M/YY") }));
4545
}
4646
},
4747
});
4848

4949
useEffect(() => {
5050
if (period) {
51-
f.setValues(period);
51+
const [start, end] = period.range;
52+
53+
f.setValues({
54+
...period,
55+
range: [start, end ? getStartOf("day", new Date(end)).toISOString() : null],
56+
});
5257
}
5358
}, [period]);
5459

apps/frontend/src/routes/_auth/administration/participants/edit.$id.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useEffect } from "react";
66

77
type FormValues = {
88
id: string;
9-
birthday: Date;
9+
birthday: string;
1010
};
1111

1212
function AdministrationParticipantsEdit() {
@@ -31,15 +31,15 @@ function AdministrationParticipantsEdit() {
3131
const f = useForm<FormValues>();
3232
const handleSubmit = ({ id, birthday }: FormValues) => {
3333
editParticipantMutation.mutate({
34-
body: { id: +id, birthday: birthday.toISOString() },
34+
body: { id: +id, birthday },
3535
params: { path: { id: p.id } },
3636
});
3737
};
3838

3939
useEffect(() => {
4040
const { birthday, id } = participant.data;
4141

42-
f.setValues({ birthday: new Date(birthday!), id: id.toString() });
42+
f.setValues({ birthday, id: id.toString() });
4343
}, [participant.isSuccess, participant.data]);
4444

4545
return (

apps/frontend/src/routes/_auth/administration/questionnaires/edit.$id.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { format, i18n } from "../../../../stores/i18n";
88
import { useStore } from "@nanostores/react";
99

1010
type FormValues = {
11-
startedAt: Date;
12-
endedAt: Date;
11+
startedAt: string;
12+
endedAt: string;
1313
title: string;
1414
remark?: string;
1515
};
@@ -49,16 +49,15 @@ function AdministrationQuestionnairesEdit() {
4949
});
5050

5151
const f = useForm<FormValues>();
52-
const handleSubmit = ({ startedAt, endedAt, ...rest }: FormValues) => {
52+
const handleSubmit = (values: FormValues) => {
5353
editQuestionnaireMutation.mutate({
54-
body: { ...rest, startedAt: startedAt.toISOString(), endedAt: endedAt.toISOString() },
54+
body: values,
5555
params: { path: { id: p.id } },
5656
});
5757
};
5858

5959
useEffect(() => {
60-
const { title, remark, startedAt, endedAt } = data;
61-
f.initialize({ title, remark, startedAt: new Date(startedAt), endedAt: new Date(endedAt) });
60+
f.initialize(data);
6261
}, [isSuccess, data]);
6362

6463
return (

apps/frontend/src/routes/_auth/questionnaire/_questionnaire/$id/period.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { PeriodForm, PeriodFormValues } from "../../../../../components/question
55
import { $api } from "../../../../../stores/api";
66
import { useSuspenseQuery } from "@tanstack/react-query";
77
import { Title } from "@quassel/ui";
8+
import { getStartOf, getEndOf } from "@quassel/utils";
89

910
export const messages = i18n("questionnairePeriod", {
1011
title: "Period",
@@ -17,27 +18,23 @@ function QuestionnairePeriod() {
1718

1819
const t = useStore(messages);
1920

20-
const { data: questionnaire } = useSuspenseQuery(
21+
const {
22+
data: { title, startedAt, endedAt },
23+
} = useSuspenseQuery(
2124
$api.queryOptions("get", "/questionnaires/{id}", {
2225
params: { path: { id } },
2326
})
2427
);
2528

26-
const period: PeriodFormValues = {
27-
title: questionnaire.title!,
28-
range: [new Date(Date.parse(questionnaire.startedAt!)), new Date(Date.parse(questionnaire.endedAt!))],
29-
};
29+
const period: PeriodFormValues = { title, range: [startedAt, endedAt] };
3030

3131
const updateQuestionnaireMutation = $api.useMutation("patch", "/questionnaires/{id}");
3232

33-
const onSave = async (form: PeriodFormValues) => {
34-
const {
35-
title,
36-
range: [localStartedAt, localEndedAt],
37-
} = form;
33+
const onSave = async ({ title, range: [startedAt, endedAt] }: PeriodFormValues) => {
34+
if (!startedAt || !endedAt) return;
3835

39-
const startedAt = localStartedAt!.toISOString();
40-
const endedAt = localEndedAt!.toISOString();
36+
startedAt = getStartOf("day", new Date(startedAt)).toISOString();
37+
endedAt = getEndOf("day", new Date(endedAt)).toISOString();
4138

4239
await updateQuestionnaireMutation.mutateAsync({
4340
params: { path: { id } },

apps/frontend/src/routes/_auth/questionnaire/_questionnaire/new.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { $api } from "../../../../stores/api";
66
import { $questionnaire } from "../../../../stores/questionnaire";
77
import { useEffect } from "react";
88
import { Title } from "@quassel/ui";
9-
import { getNext, getStartOf } from "@quassel/utils";
9+
import { getEndOf, getNext, getStartOf } from "@quassel/utils";
1010

1111
const messages = i18n("questionnaireNew", {
1212
title: "Create new period of life",
@@ -38,16 +38,13 @@ function QuestionnaireNew() {
3838
const startFromBirthday = participant?.birthday ? getStartOf("month", new Date(participant.birthday)) : undefined;
3939
const startFromPrevious = prevEndDate ? getNext("month", new Date(prevEndDate)) : undefined;
4040

41-
const startDate = startFromPrevious ?? startFromBirthday;
41+
const startDate = (startFromPrevious ?? startFromBirthday)?.toISOString();
4242

43-
const onSave = (form: PeriodFormValues) => {
44-
const {
45-
title,
46-
range: [localStartedAt, localEndedAt],
47-
} = form;
43+
const onSave = async ({ title, range: [startedAt, endedAt] }: PeriodFormValues) => {
44+
if (!startedAt || !endedAt) return;
4845

49-
const startedAt = localStartedAt!.toISOString();
50-
const endedAt = localEndedAt!.toISOString();
46+
startedAt = getStartOf("day", new Date(startedAt)).toISOString();
47+
endedAt = getEndOf("day", new Date(endedAt)).toISOString();
5148

5249
createQuestionnaireMutation.mutate({
5350
body: { title, startedAt, endedAt, participant: questionnaire!.participant.id },

libs/ui/src/components/MonthPicker.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function MonthPicker<Type extends DatePickerType = "default">({ selectEnd
1818
const onChange: MonthPickerProps<Type>["onChange"] = (date) => {
1919
if (rest.type === "range" && isRangeValue(date) && selectEndOfMonth) {
2020
const [start, end] = date;
21-
const newDate = [start, end ? dayjs(end).utc().endOf("month").toDate() : undefined];
21+
const newDate = [start, end ? dayjs(end).endOf("month").format("YYYY-MM-DD") : undefined];
2222
date = newDate as typeof date;
2323
}
2424
rest.onChange!(date);

libs/utils/src/date.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const getTime = (date: Date) => formatDate(date, "HH:mm");
1616
export const getNext = (unit: dayjs.ManipulateType, date: Date) => dayjs(date).utc().add(1, unit).startOf(unit).toDate();
1717

1818
export const getStartOf = (unit: dayjs.ManipulateType, date: Date) => dayjs(date).utc().startOf(unit).toDate();
19+
export const getEndOf = (unit: dayjs.ManipulateType, date: Date) => dayjs(date).utc().endOf(unit).toDate();
1920

2021
export const isSame = (unit: dayjs.ManipulateType, left: Date, right = new Date()) => dayjs(left).isSame(right, unit);
2122
export const isSameOrAfter = (left: Date, right: Date, unit: dayjs.ManipulateType) => dayjs(left).isAfter(dayjs(right).startOf(unit));

libs/utils/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { formatDate, getTime, getDateFromTimeAndWeekday, getNext, isSameOrAfter, isSame, getStartOf } from "./date";
1+
export { formatDate, getTime, getDateFromTimeAndWeekday, getNext, isSameOrAfter, isSame, getStartOf, getEndOf } from "./date";
22

33
export { type Gap, type GapsPerDay, groupByWeekday, resolveGaps, entriesByInterval } from "./entry";

0 commit comments

Comments
 (0)