Skip to content

Commit

Permalink
fix: able to save age in future
Browse files Browse the repository at this point in the history
  • Loading branch information
alaa-yahia committed Jan 6, 2025
1 parent 8dd1110 commit 2182347
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
// @flow
import moment from 'moment';
import { orientations } from '../../../../FormFields/New';
import { createFieldConfig, createProps } from '../base/configBaseDefaultForm';
import { AgeFieldForForm } from '../../Components';
import { type DataElement } from '../../../../../metaData';
import { convertDateObjectToDateFormatString } from '../../../../../../capture-core/utils/converters/date';
import { type DateDataElement } from '../../../../../metaData';
import type { QuerySingleResource } from '../../../../../utils/api/api.types';

const getCalendarAnchorPosition = (formHorizontal: ?boolean) => (formHorizontal ? 'center' : 'left');

export const getAgeFieldConfig = (metaData: DataElement, options: Object, querySingleResource: QuerySingleResource) => {
export const getAgeFieldConfig = (metaData: DateDataElement, options: Object, querySingleResource: QuerySingleResource) => {
const props = createProps({
formHorizontal: options.formHorizontal,
fieldLabelMediaBasedClass: options.fieldLabelMediaBasedClass,
orientation: options.formHorizontal ? orientations.VERTICAL : orientations.HORIZONTAL,
shrinkDisabled: options.formHorizontal,
dateCalendarWidth: options.formHorizontal ? 250 : 350,
datePopupAnchorPosition: getCalendarAnchorPosition(options.formHorizontal),
calendarMax: !metaData.allowFutureDate ? convertDateObjectToDateFormatString(moment()) : undefined,
}, options, metaData);

return createFieldConfig({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ const fieldForTypes: FieldForTypes = {
[dataElementTypes.TIME_RANGE]: getTextRangeFieldConfig,
[dataElementTypes.PERCENTAGE]: getTextFieldConfig,
[dataElementTypes.URL]: getTextFieldConfig,
[dataElementTypes.AGE]: getAgeFieldConfig,
[dataElementTypes.AGE]: (metaData: any, options: Object, querySingleResource: QuerySingleResource) =>
getAgeFieldConfig(metaData, options, querySingleResource),
[dataElementTypes.ORGANISATION_UNIT]: getOrgUnitFieldConfig,
[dataElementTypes.COORDINATE]: getCoordinateFieldConfig,
[dataElementTypes.POLYGON]: getPolygonFieldConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from 'capture-core-utils/validators/form';
import {
isValidAge,
isValidNonFutureAge,
isValidDate,
isValidNonFutureDate,
isValidDateTime,
Expand Down Expand Up @@ -153,6 +154,11 @@ const validatorsForTypes = {
validator: isValidAge,
message: errorMessages.AGE,
type: validatorTypes.TYPE_BASE,
},
{
validator: isValidNonFutureAge,
type: validatorTypes.TYPE_EXTENDED,
message: errorMessages.DATE_FUTURE_NOT_ALLOWED,
}],
[dataElementTypes.PHONE_NUMBER]: [{
validator: isValidPhoneNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,21 @@ export function isValidAge(value: Object, internalComponentError?: ?{error: ?str
return false;
}

const numberResult = validateNumbers(
if (internalComponentError && internalComponentError?.errorCode === 'INVALID_DATE_MORE_THAN_MAX') {
return { valid: true, errorMessage: null };
}

const dateValidation = value.date
? validateDate(value.date, internalComponentError)
: { valid: true, errorMessage: null };

if (!dateValidation.valid) {
return dateValidation;
}

return validateNumbers(
value.years,
value.months,
value.days,
);

if (!numberResult.valid) return numberResult;

return validateDate(value.date, internalComponentError);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export { getDateRangeValidator } from './getDateRangeValidator';
export { getDateTimeRangeValidator } from './getDateTimeRangeValidator';
export { getNumberRangeValidator } from './getNumberRangeValidator';
export { getTimeRangeValidator } from './getTimeRangeValidator';
export { isValidNonFutureAge } from './isValidNonFutureAge';
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @flow
import i18n from '@dhis2/d2-i18n';

const CUSTOM_VALIDATION_MESSAGES = {
INVALID_DATE_MORE_THAN_MAX: i18n.t('A date in the future is not allowed'),
};

export const isValidNonFutureAge = (value: string, internalComponentError?: ?{error: ?string, errorCode: ?string}) => {
if (!value) {
return true;
}

if (internalComponentError && internalComponentError?.errorCode === 'INVALID_DATE_MORE_THAN_MAX') {
return {
valid: false,
errorMessage: { date: CUSTOM_VALIDATION_MESSAGES.INVALID_DATE_MORE_THAN_MAX },
};
}

return true;
};
1 change: 1 addition & 0 deletions src/core_modules/capture-ui/AgeField/ageField.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
}

.ageDateInputContainerHorizontal {
width: 150px;
}

.ageClearHorizontal {
Expand Down

0 comments on commit 2182347

Please sign in to comment.