From fa593f4569d7faa7fb7ed3a66580815ef1863525 Mon Sep 17 00:00:00 2001 From: Jovan Ssebaggala Date: Wed, 16 Oct 2024 14:35:43 +0300 Subject: [PATCH] Add flags to disable future and past dates --- .../src/config-schema.ts | 5 +- .../field/obs/obs-field.component.tsx | 47 ++++--------------- 2 files changed, 12 insertions(+), 40 deletions(-) diff --git a/packages/esm-patient-registration-app/src/config-schema.ts b/packages/esm-patient-registration-app/src/config-schema.ts index 234d319a46..12a72dca67 100644 --- a/packages/esm-patient-registration-app/src/config-schema.ts +++ b/packages/esm-patient-registration-app/src/config-schema.ts @@ -11,8 +11,9 @@ export interface FieldDefinition { type: string; label?: string; uuid: string; - maxDate?: string; - minDate?: string; + placeholder?: string; + disableFutureDates?: boolean; + disablePastDates?: boolean; 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 cbad9c8be3..8369724792 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 @@ -59,8 +59,8 @@ export function ObsField({ fieldDefinition }: ObsFieldProps) { concept={concept} label={fieldDefinition.label} required={fieldDefinition.validation.required} - minDate={fieldDefinition.minDate} - maxDate={fieldDefinition.maxDate} + disablePastDates={fieldDefinition.disablePastDates} + disableFutureDates={fieldDefinition.disableFutureDates} /> ); case 'Coded': @@ -161,43 +161,14 @@ interface DateObsFieldProps { concept: ConceptResponse; label: string; required?: boolean; - minDate?: string; - maxDate?: string; + disablePastDates?: boolean; + disableFutureDates?: boolean; } -const evaluateConfigDate = (dateString: string, dateType: string): Date | null => { - if (!dateString) { - return null; - } - if (dateString === 'today') { - return dayjs(new Date()).toDate(); - } - - const parsedDate = dayjs(dateString); - if (!parsedDate.isValid()) { - throw new Error( - translateFrom(moduleName, 'invalidObsDateErrorMessage', `The ${dateType} date value provided is invalid!`), - ); - } - - return parsedDate.toDate(); -}; - -function DateObsField({ concept, label, required, maxDate, minDate }: DateObsFieldProps) { +function DateObsField({ concept, label, required, disablePastDates, disableFutureDates }: DateObsFieldProps) { const { t } = useTranslation(); const fieldName = `obs.${concept.uuid}`; const { setFieldValue } = useContext(PatientRegistrationContext); - let evaluatedMinDate = null; - let evaluatedMaxDate = null; - let configDateError = ''; - - try { - evaluatedMinDate = evaluateConfigDate(minDate, 'Min'); - evaluatedMaxDate = evaluateConfigDate(maxDate, 'Max'); - } catch (error) { - configDateError = error.message; - console.error(configDateError); - } const onDateChange = (date: Date) => { setFieldValue(fieldName, date); @@ -216,11 +187,11 @@ function DateObsField({ concept, label, required, maxDate, minDate }: DateObsFie isRequired={required} onChange={onDateChange} labelText={label ?? concept.display} - isInvalid={(errors[fieldName] && touched[fieldName]) || configDateError} - invalidText={t(meta.error) || configDateError} + isInvalid={errors[fieldName] && touched[fieldName]} + invalidText={t(meta.error)} value={field.value} - minDate={evaluatedMinDate} - maxDate={evaluatedMaxDate} + minDate={disablePastDates ? new Date() : undefined} + maxDate={disableFutureDates ? new Date() : undefined} /> );