Skip to content

Commit

Permalink
fix: enrollement date input fields
Browse files Browse the repository at this point in the history
  • Loading branch information
alaa-yahia committed Nov 11, 2024
1 parent 48bf0eb commit 2a5cd1e
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 249 deletions.
7 changes: 2 additions & 5 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-11-07T20:29:15.378Z\n"
"PO-Revision-Date: 2024-11-07T20:29:15.379Z\n"
"POT-Creation-Date: 2024-11-11T01:17:03.829Z\n"
"PO-Revision-Date: 2024-11-11T01:17:03.830Z\n"

msgid "Choose one or more dates..."
msgstr "Choose one or more dates..."
Expand Down Expand Up @@ -164,9 +164,6 @@ msgstr "{{ stageName }} - Status"
msgid "Please select {{categoryName}}"
msgstr "Please select {{categoryName}}"

msgid "A future date is not allowed"
msgstr "A future date is not allowed"

msgid "Saving a new enrollment in {{programName}} in {{orgUnitName}}."
msgstr "Saving a new enrollment in {{programName}} in {{orgUnitName}}."

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions src/core_modules/capture-core-utils/validators/form/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @flow
export { hasValue } from './compulsory.validator';
export { isValidDate } from './date.validator';
export { isValidDateTime } from './dateTime.validator';
export { isValidEmail } from './email.validator';
export { isValidInteger } from './integer.validator';
export { isValidPositiveInteger } from './integerPositive.validator';
Expand All @@ -11,7 +9,6 @@ export { isValidNumber } from './number.validator';
export { isValidPercentage } from './percentage.validator';
export { isValidTime } from './time.validator';
export { isValidUrl } from './url.validator';
export { isValidAge } from './age.validator';
export { isValidPhoneNumber } from './phone.validator';
export { isValidOrgUnit } from './orgUnit.validator';
export { isValidCoordinate } from './coordinate.validator';
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export class FormBuilder extends React.Component<Props> {
if (result instanceof Promise) {
result = onIsValidatingInternal ?
onIsValidatingInternal(currentValidator.validatingMessage, result) :
result = await result;
result;
result = await result;
}

if (result === true || (result && result.valid)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ const getEnrollmentDateSettings = () => {
calendarMaxMoment: !props.enrollmentMetadata.allowFutureEnrollmentDate ? moment() : undefined,
}),
getPropName: () => 'enrolledAt',
getValidatorContainers: (props: Object) =>
getEnrollmentDateValidatorContainer(props.enrollmentMetadata.allowFutureEnrollmentDate),
getValidatorContainers: getEnrollmentDateValidatorContainer,
getPassOnFieldData: () => true,
getMeta: () => ({
placement: placements.TOP,
Expand Down Expand Up @@ -164,8 +163,7 @@ const getIncidentDateSettings = () => {
}),
getPropName: () => 'occurredAt',
getPassOnFieldData: () => true,
getValidatorContainers: (props: Object) =>
getIncidentDateValidatorContainer(props.enrollmentMetadata.allowFutureIncidentDate),
getValidatorContainers: getIncidentDateValidatorContainer,
getMeta: () => ({
placement: placements.TOP,
section: sectionKeysForEnrollmentDataEntry.ENROLLMENT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// @flow
import { hasValue } from 'capture-core-utils/validators/form';
import i18n from '@dhis2/d2-i18n';
import { hasValue } from 'capture-core-utils/validators/form';
import { isValidDate } from '../../../../../utils/validators/form';

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

return isValidDate(value);
return isValidDate(value, internalComponentError);
};

export const getEventDateValidatorContainers = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
// @flow
import { hasValue } from 'capture-core-utils/validators/form';
import i18n from '@dhis2/d2-i18n';
import moment from 'moment';
import { parseDate } from '../../../../utils/converters/date';

const isValidEnrollmentDate = (value: string, isFutureDateAllowed: boolean) => {
const dateContainer = parseDate(value);
if (!dateContainer.isValid) {
return false;
}
import { hasValue } from 'capture-core-utils/validators/form';
import { isValidDate, isValidNonFutureDate } from '../../../../utils/validators/form';

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

const momentDate = dateContainer.momentDate;
const momentToday = moment();
// $FlowFixMe -> if parseDate returns isValid true, there should always be a momentDate
const isNotFutureDate = momentDate.isSameOrBefore(momentToday);
return {
valid: isNotFutureDate,
message: i18n.t('A future date is not allowed'),
};
return isValidDate(value, internalComponentError);
};

export const getEnrollmentDateValidatorContainer = (isFutureEnrollmentDateAllowed: boolean) => {
export const getEnrollmentDateValidatorContainer = () => {
const validatorContainers = [
{
validator: hasValue,
message:
i18n.t('A value is required'),
},
{
validator: (value: string) => isValidEnrollmentDate(value, isFutureEnrollmentDateAllowed),
validator: isValidEnrollmentDate,
message: i18n.t('Please provide a valid date'),
},
{ validator: isValidNonFutureDate,
message: i18n.t('A date in the future is not allowed'),
},
];
return validatorContainers;
};
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
// @flow
import { hasValue } from 'capture-core-utils/validators/form';
import i18n from '@dhis2/d2-i18n';
import moment from 'moment';
import { parseDate } from '../../../../utils/converters/date';

const isValidIncidentDate = (value: string, isFutureDateAllowed: boolean) => {
const dateContainer = parseDate(value);
if (!dateContainer.isValid) {
return false;
const CUSTOM_VALIDATION_MESSAGES = {
INVALID_DATE_MORE_THAN_MAX: i18n.t('A date in the future is not allowed'),
};

const isValidIncidentDate = (value: string, internalComponentError) => {
if (!internalComponentError || !internalComponentError?.error) {
return { valid: true };
}

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

const momentDate = dateContainer.momentDate;
const momentToday = moment();
// $FlowFixMe -> if parseDate returns isValid true, there should always be a momentDate
const isNotFutureDate = momentDate.isSameOrBefore(momentToday);
return {
valid: isNotFutureDate,
message: i18n.t('A future date is not allowed'),
};
return true;
};


export const getIncidentDateValidatorContainer = (isFutureIncidentDateAllowed: boolean) => {
export const getIncidentDateValidatorContainer = () => {
const validatorContainers = [
{
validator: hasValue,
message:
i18n.t('A value is required'),
},
{
validator: (value: string) => isValidIncidentDate(value, isFutureIncidentDateAllowed),
validator: (value: string, internalComponentError) => isValidIncidentDate(value, internalComponentError),
message: i18n.t('Please provide a valid date'),
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { hasValue } from 'capture-core-utils/validators/form';
import i18n from '@dhis2/d2-i18n';
import { isValidDate } from '../../../../utils/validators/form';

const preValidateDate = (value?: ?string) => {
const preValidateDate = (value?: ?string, x) => {

Check failure on line 6 in src/core_modules/capture-core/components/WidgetEnrollmentEventNew/DataEntry/fieldValidators/eventDate.validatorContainersGetter.js

View workflow job for this annotation

GitHub Actions / lint

'x' is defined but never used
if (!value) {
return true;
}
Expand Down
Loading

0 comments on commit 2a5cd1e

Please sign in to comment.