Skip to content

Commit e55d3b3

Browse files
committed
feat: restrict period from birthday to current date
1 parent 6b3eebc commit e55d3b3

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Button, Flex, formatDate, getNext, InputError, MonthPicker, Stack, TextInput, useForm } from "@quassel/ui";
1+
import { Button, Flex, formatDate, InputError, MonthPicker, Stack, TextInput, useForm } from "@quassel/ui";
22
import { i18n } from "../../stores/i18n";
33
import { useStore } from "@nanostores/react";
44
import { useEffect } from "react";
@@ -12,7 +12,7 @@ export type PeriodFormValues = {
1212
type PeriodFormProps = {
1313
onSave: (form: PeriodFormValues) => void;
1414
period?: PeriodFormValues;
15-
prevEndDate?: Date;
15+
startDate: Date;
1616
actionLabel: string;
1717
};
1818

@@ -22,26 +22,24 @@ const messages = i18n("periodForm", {
2222
validationStartDate: "There are no gaps allowed between questionnaires. The questionnaire must start when the previous ended.",
2323
});
2424

25-
export function PeriodForm({ onSave, actionLabel, period, prevEndDate }: PeriodFormProps) {
25+
export function PeriodForm({ onSave, actionLabel, period, startDate }: PeriodFormProps) {
2626
const t = useStore(messages);
2727

2828
const f = useForm<PeriodFormValues>({
29-
mode: "uncontrolled",
3029
initialValues: {
31-
range: [prevEndDate ? getNext("month", prevEndDate) : null, null],
30+
range: [startDate, null],
3231
title: "",
3332
},
3433
validate: {
3534
range([start]) {
36-
if (prevEndDate && +getNext("month", prevEndDate) !== +start!) {
35+
if (+startDate !== +start!) {
3736
return t.validationStartDate;
3837
}
3938
},
4039
},
4140
onValuesChange(newValues, prevValues) {
4241
const [newStart, newEnd] = newValues.range ?? [];
4342
const [prevStart, prevEnd] = prevValues.range ?? [];
44-
4543
if ((!prevStart || !prevEnd) && newStart && newEnd) {
4644
f.setFieldValue("title", t.defaultTitle({ start: formatDate(newStart, "M/YY"), end: formatDate(newEnd, "M/YY") }));
4745
}
@@ -50,22 +48,26 @@ export function PeriodForm({ onSave, actionLabel, period, prevEndDate }: PeriodF
5048

5149
useEffect(() => {
5250
if (period) {
53-
f.setValues(period);
54-
f.resetDirty();
51+
f.initialize(period);
5552
}
5653
}, [period]);
5754

55+
useEffect(() => {
56+
if (startDate) f.reset();
57+
}, [startDate]);
58+
5859
return (
59-
<form onSubmit={f.onSubmit((values) => onSave(values))}>
60+
<form onSubmit={f.onSubmit(onSave)}>
6061
<Stack>
6162
<Flex justify="center">
6263
<Stack>
6364
<MonthPicker
6465
{...f.getInputProps("range")}
6566
size="md"
6667
type="range"
67-
minDate={prevEndDate}
68-
defaultDate={prevEndDate}
68+
minDate={startDate}
69+
maxDate={new Date()}
70+
defaultDate={startDate}
6971
numberOfColumns={2}
7072
columnsToScroll={1}
7173
allowSingleDateInRange

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function QuestionnairePeriod() {
4949
return (
5050
<>
5151
<h3>{t.title}</h3>
52-
<PeriodForm onSave={onSave} period={period} actionLabel={t.formAction} />
52+
<PeriodForm onSave={onSave} startDate={period.range[0]!} period={period} actionLabel={t.formAction} />
5353
</>
5454
);
5555
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useStore } from "@nanostores/react";
55
import { $api } from "../../../../../stores/api";
66
import { useEffect } from "react";
77
import { components } from "../../../../../api.gen";
8+
import { useQueryClient } from "@tanstack/react-query";
89

910
export const messages = i18n("questionnaireRemarks", {
1011
title: "Add remarks",
@@ -22,6 +23,8 @@ function QuestionnaireRemarks() {
2223
const n = useNavigate();
2324
const p = Route.useParams();
2425

26+
const c = useQueryClient();
27+
2528
const t = useStore(messages);
2629

2730
const f = useForm<FormValues>({
@@ -49,6 +52,9 @@ function QuestionnaireRemarks() {
4952
if (isSameOrAfter(new Date(Date.parse(questionnaire.endedAt)), new Date(), "month")) {
5053
n({ to: "/questionnaire/$id/overview", params: p });
5154
} else {
55+
await c.invalidateQueries(
56+
$api.queryOptions("get", "/participants/{id}", { params: { path: { id: questionnaire.participant.id.toString() } } })
57+
);
5258
n({ to: "/questionnaire/new" });
5359
}
5460
};

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { PeriodForm, PeriodFormValues } from "../../../../components/questionnai
55
import { $api } from "../../../../stores/api";
66
import { $questionnaire } from "../../../../stores/questionnaire";
77
import { useEffect } from "react";
8+
import { getNext } from "@quassel/ui";
89

910
const messages = i18n("questionnaireNew", {
1011
title: "Create new period of life",
@@ -33,6 +34,8 @@ function QuestionnaireNew() {
3334

3435
const prevEndDate = questionnaire?.participant.latestQuestionnaire?.endedAt;
3536

37+
const startDate = prevEndDate ? getNext("month", new Date(prevEndDate)) : participant?.birthday ? new Date(participant.birthday) : undefined;
38+
3639
const onSave = (form: PeriodFormValues) => {
3740
const {
3841
title,
@@ -50,7 +53,7 @@ function QuestionnaireNew() {
5053
return (
5154
<>
5255
<h3>{t.title}</h3>
53-
<PeriodForm onSave={onSave} prevEndDate={prevEndDate ? new Date(prevEndDate) : undefined} actionLabel={t.formAction} />
56+
{startDate && <PeriodForm onSave={onSave} startDate={startDate} actionLabel={t.formAction} />}
5457
</>
5558
);
5659
}

0 commit comments

Comments
 (0)