From e40f4c07731eb5fe6a7d4afe4edadf88b2ca09a0 Mon Sep 17 00:00:00 2001 From: Jovan Ssebaggala Date: Wed, 31 Jul 2024 07:37:14 +0300 Subject: [PATCH] Add min and max date validations to obsDate field --- .../src/config-schema.ts | 3 +- .../field/obs/obs-field.component.tsx | 35 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/packages/esm-patient-registration-app/src/config-schema.ts b/packages/esm-patient-registration-app/src/config-schema.ts index 1e2cadd78..6b9d6f866 100644 --- a/packages/esm-patient-registration-app/src/config-schema.ts +++ b/packages/esm-patient-registration-app/src/config-schema.ts @@ -12,7 +12,8 @@ export interface FieldDefinition { label?: string; uuid: string; placeholder?: string; - dateFormat?: string; + maxDate?: string; + minDate?: string; showHeading: boolean; validation?: { required: boolean; diff --git a/packages/esm-patient-registration-app/src/patient-registration/field/obs/obs-field.component.tsx b/packages/esm-patient-registration-app/src/patient-registration/field/obs/obs-field.component.tsx index 7eecbf520..c05b37ade 100644 --- a/packages/esm-patient-registration-app/src/patient-registration/field/obs/obs-field.component.tsx +++ b/packages/esm-patient-registration-app/src/patient-registration/field/obs/obs-field.component.tsx @@ -57,8 +57,8 @@ export function ObsField({ fieldDefinition }: ObsFieldProps) { concept={concept} label={fieldDefinition.label} required={fieldDefinition.validation.required} - dateFormat={fieldDefinition.dateFormat} - placeholder={fieldDefinition.placeholder} + minDate={fieldDefinition.minDate} + maxDate={fieldDefinition.maxDate} /> ); case 'Coded': @@ -159,15 +159,38 @@ interface DateObsFieldProps { concept: ConceptResponse; label: string; required?: boolean; - dateFormat?: string; - placeholder?: string; + minDate?: string; + maxDate?: string; } -function DateObsField({ concept, label, required, placeholder }: DateObsFieldProps) { +const evaluateConfigDate = (dateString: string): Date | string | null => { + if (!dateString) { + return null; + } + + if (dateString === 'today') { + return new Date(); + } + + const date = new Date(dateString); + if (!isNaN(date.getTime())) { + return date; + } + + return 'Invalid min or max date!'; +}; + +function DateObsField({ concept, label, required, maxDate, minDate }: DateObsFieldProps) { const { t } = useTranslation(); const fieldName = `obs.${concept.uuid}`; const { setFieldValue } = useContext(PatientRegistrationContext); + const evaluatedMinDate = evaluateConfigDate(minDate); + const evaluatedMaxDate = evaluateConfigDate(maxDate); + const configDateError = + (typeof evaluatedMinDate === 'string' && evaluatedMinDate) || + (typeof evaluatedMaxDate === 'string' && evaluatedMaxDate); + const onDateChange = (date: Date) => { setFieldValue(fieldName, date); }; @@ -188,6 +211,8 @@ function DateObsField({ concept, label, required, placeholder }: DateObsFieldPro isInvalid={errors[fieldName] && touched[fieldName]} invalidText={t(meta.error)} value={field.value} + minDate={typeof evaluatedMinDate !== 'string' && evaluatedMinDate} + maxDate={typeof evaluatedMaxDate !== 'string' && evaluatedMaxDate} /> );