Skip to content

Commit

Permalink
Add flags to disable future and past dates
Browse files Browse the repository at this point in the history
  • Loading branch information
kajambiya committed Oct 16, 2024
1 parent 0cd381b commit 32486f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 43 deletions.
5 changes: 3 additions & 2 deletions packages/esm-patient-registration-app/src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import classNames from 'classnames';
import { Field } from 'formik';
import { useTranslation } from 'react-i18next';
import { InlineNotification, Layer, Select, SelectItem } from '@carbon/react';
import { OpenmrsDatePicker, translateFrom, useConfig } from '@openmrs/esm-framework';
import { OpenmrsDatePicker, useConfig } from '@openmrs/esm-framework';
import { type ConceptResponse } from '../../patient-registration.types';
import { type FieldDefinition, type RegistrationConfig } from '../../../config-schema';
import { Input } from '../../input/basic-input/input/input.component';
import { useConcept, useConceptAnswers } from '../field.resource';
import { PatientRegistrationContext } from '../../patient-registration-context';
import styles from './../field.scss';
import dayjs from 'dayjs';
import { moduleName } from '../../../constants';

export interface ObsFieldProps {
fieldDefinition: FieldDefinition;
Expand Down Expand Up @@ -59,8 +57,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':
Expand Down Expand Up @@ -161,43 +159,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);
Expand All @@ -216,11 +185,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}
/>
</>
);
Expand Down

0 comments on commit 32486f2

Please sign in to comment.