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 fa593f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 40 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 @@ -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':
Expand Down Expand Up @@ -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);
Expand All @@ -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}
/>
</>
);
Expand Down

0 comments on commit fa593f4

Please sign in to comment.