Skip to content

Commit 8f1eae5

Browse files
committed
feat: adjust the max age check for dob with refine method
1 parent ba3183d commit 8f1eae5

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

apps/web/src/components/StartSessionForm/StartSessionForm.tsx

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import { z } from 'zod/v4';
1515

1616
const currentDate = new Date();
1717

18-
const EIGHTEEN_YEARS = 568025136000; // milliseconds
18+
// const EIGHTEEN_YEARS = 568025136000; // milliseconds
1919

20-
const MIN_DATE_OF_BIRTH = new Date(currentDate.getTime() - EIGHTEEN_YEARS);
20+
//const MIN_DATE_OF_BIRTH = new Date(currentDate.getTime() - EIGHTEEN_YEARS);
2121

2222
type StartSessionFormData = {
2323
sessionDate: Date;
@@ -46,6 +46,13 @@ export const StartSessionForm = ({
4646
onSubmit
4747
}: StartSessionFormProps) => {
4848
const { resolvedLanguage, t } = useTranslation();
49+
const MIN_DATE_OF_BIRTH = currentGroup?.settings.minimumAgeApplied
50+
? new Date(
51+
currentDate.getTime() -
52+
(currentGroup?.settings.minimumAge ? currentGroup?.settings.minimumAge * 31556952000 : 0)
53+
)
54+
: undefined;
55+
4956
return (
5057
<Form
5158
preventResetValuesOnReset
@@ -177,8 +184,26 @@ export const StartSessionForm = ({
177184
.optional(),
178185
subjectDateOfBirth: z
179186
.date()
180-
.max(MIN_DATE_OF_BIRTH, { message: t('session.errors.mustBeAdult') })
181-
.optional(),
187+
188+
// .max(MIN_DATE_OF_BIRTH, { message: t({
189+
// en: `Subject must be above age of ${currentGroup?.settings.minimumAge}`,
190+
// fr: `Le sujet doit être âgé de plus de ${currentGroup?.settings.minimumAge}`
191+
// }) })
192+
.optional()
193+
.refine(
194+
(date) => {
195+
// If there's no date picked or no limit defined, it's valid
196+
if (!date || !MIN_DATE_OF_BIRTH) return true;
197+
// Otherwise, ensure the date is not after the maximum allowed date
198+
return date <= MIN_DATE_OF_BIRTH;
199+
},
200+
{
201+
message: t({
202+
en: `Subject must be above age of ${currentGroup?.settings.minimumAge}`,
203+
fr: `Le sujet doit être âgé de plus de ${currentGroup?.settings.minimumAge}`
204+
})
205+
}
206+
),
182207
subjectSex: z.enum(['MALE', 'FEMALE']).optional(),
183208
sessionType: $SessionType.exclude(['REMOTE']),
184209
sessionDate: z

0 commit comments

Comments
 (0)