From 48713020465d8764a1ea3104a8d873216b9157d6 Mon Sep 17 00:00:00 2001 From: alaa-yahia Date: Mon, 4 Nov 2024 17:09:33 +0200 Subject: [PATCH] fix: schedule date in related stages to display calendarInput internal errors --- i18n/en.pot | 10 +++++----- .../DateFieldForRelatedStages.js | 9 ++++----- .../ScheduleInOrgUnit.component.js | 6 +++--- .../WidgetRelatedStages.component.js | 4 +++- .../WidgetRelatedStages.types.js | 1 + .../ValidationFunctions.js | 18 ++++++++++++++---- .../relatedStageEventIsValid.js | 2 ++ .../relatedStageEventIsValid.types.js | 1 + 8 files changed, 33 insertions(+), 18 deletions(-) diff --git a/i18n/en.pot b/i18n/en.pot index a1b2e88489..51d2f1bcc6 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -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-10-25T18:18:11.518Z\n" -"PO-Revision-Date: 2024-10-25T18:18:11.518Z\n" +"POT-Creation-Date: 2024-11-04T14:40:30.714Z\n" +"PO-Revision-Date: 2024-11-04T14:40:30.715Z\n" msgid "Choose one or more dates..." msgstr "Choose one or more dates..." @@ -1432,6 +1432,9 @@ msgstr "Scheduled date" msgid "Report date" msgstr "Report date" +msgid "Please enter a date" +msgstr "Please enter a date" + msgid "Please select a valid event" msgstr "Please select a valid event" @@ -1739,9 +1742,6 @@ msgstr "Please enter a valid time" msgid "Please enter a time" msgstr "Please enter a time" -msgid "Please enter a date" -msgstr "Please enter a date" - msgid "Set coordinate" msgstr "Set coordinate" diff --git a/src/core_modules/capture-core/components/WidgetRelatedStages/FormComponents/DateFieldForRelatedStages.js b/src/core_modules/capture-core/components/WidgetRelatedStages/FormComponents/DateFieldForRelatedStages.js index d605f177cc..cbb55a1ef5 100644 --- a/src/core_modules/capture-core/components/WidgetRelatedStages/FormComponents/DateFieldForRelatedStages.js +++ b/src/core_modules/capture-core/components/WidgetRelatedStages/FormComponents/DateFieldForRelatedStages.js @@ -1,6 +1,5 @@ // @flow import React, { useState } from 'react'; -import { convertStringToDateFormat } from '../../../utils/converters/date'; import { DateField, withDefaultFieldContainer, @@ -16,7 +15,7 @@ import type { RelatedStageDataValueStates } from '../WidgetRelatedStages.types'; type Props = {| scheduledLabel: string, relatedStagesDataValues: RelatedStageDataValueStates, - onBlurDateField: (value: string) => void, + onBlurDateField: (value: string, internalComponentError?: {error: ?string, errorCode: ?string}) => void, saveAttempted: boolean, errorMessages: ErrorMessagesForRelatedStages, |} @@ -43,16 +42,16 @@ export const DateFieldForRelatedStages = ({ }: Props) => { const [touched, setTouched] = useState(false); - const onBlur = (event) => { + const onBlur = (event, internalComponentError) => { setTouched(true); - onBlurDateField(event); + onBlurDateField(event, internalComponentError); }; const shouldShowError = (touched || saveAttempted); return ( {}} onFocus={() => {}} diff --git a/src/core_modules/capture-core/components/WidgetRelatedStages/ScheduleInOrgUnit/ScheduleInOrgUnit.component.js b/src/core_modules/capture-core/components/WidgetRelatedStages/ScheduleInOrgUnit/ScheduleInOrgUnit.component.js index 33c556f0ed..310e5f7100 100644 --- a/src/core_modules/capture-core/components/WidgetRelatedStages/ScheduleInOrgUnit/ScheduleInOrgUnit.component.js +++ b/src/core_modules/capture-core/components/WidgetRelatedStages/ScheduleInOrgUnit/ScheduleInOrgUnit.component.js @@ -3,7 +3,6 @@ import React from 'react'; import type { ComponentType } from 'react'; import { withStyles } from '@material-ui/core'; import { colors, spacers, spacersNum } from '@dhis2/ui'; -import { convertStringToDateFormat } from '../../../utils/converters/date'; import { DateFieldForRelatedStages, OrgUnitSelectorForRelatedStages } from '../FormComponents'; import type { ErrorMessagesForRelatedStages } from '../RelatedStagesActions'; import type { RelatedStageDataValueStates } from '../WidgetRelatedStages.types'; @@ -52,10 +51,11 @@ export const ScheduleInOrgUnitPlain = ({ scheduledLabel, classes, }: Props) => { - const onBlurDateField = (e) => { + const onBlurDateField = (e, internalComponentError) => { setRelatedStagesDataValues(prevValues => ({ ...prevValues, - scheduledAt: convertStringToDateFormat(e), + scheduledAt: e, + scheduledAtFormatError: internalComponentError, })); }; diff --git a/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.component.js b/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.component.js index 722515d992..4c3013ca3c 100644 --- a/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.component.js +++ b/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.component.js @@ -34,6 +34,7 @@ const WidgetRelatedStagesPlain = ({ const [relatedStageDataValues, setRelatedStageDataValues] = useState({ linkMode: undefined, scheduledAt: '', + scheduledAtFormatError: undefined, orgUnit: undefined, linkedEventId: undefined, }); @@ -62,10 +63,11 @@ const WidgetRelatedStagesPlain = ({ }; const formIsValid = useCallback(() => { - const { scheduledAt, orgUnit, linkedEventId, linkMode } = relatedStageDataValues; + const { scheduledAt, scheduledAtFormatError, orgUnit, linkedEventId, linkMode } = relatedStageDataValues; return relatedStageWidgetIsValid({ linkMode, scheduledAt, + scheduledAtFormatError, orgUnit, linkedEventId, setErrorMessages: addErrorMessage, diff --git a/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.types.js b/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.types.js index 6f9288bb30..0274e06456 100644 --- a/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.types.js +++ b/src/core_modules/capture-core/components/WidgetRelatedStages/WidgetRelatedStages.types.js @@ -24,6 +24,7 @@ export type Props = {| export type RelatedStageDataValueStates = {| linkMode: ?$Keys, scheduledAt: string, + scheduledAtFormatError: ?{error: ?string, errorCode: ?string}, orgUnit: ?{ path: string, id: string, diff --git a/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/ValidationFunctions.js b/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/ValidationFunctions.js index 0733b60498..dff514d0bf 100644 --- a/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/ValidationFunctions.js +++ b/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/ValidationFunctions.js @@ -5,21 +5,31 @@ import { actions as RelatedStageModes } from '../constants'; type Props = { scheduledAt: ?string, + scheduledAtFormatError: ?{error: ?string, errorCode: ?string}, orgUnit: ?Object, linkedEventId: ?string, setErrorMessages: (messages: Object) => void, }; -export const isScheduledDateValid = (scheduledDate: string) => isValidDate(scheduledDate); +export const isScheduledDateValid = (scheduledDate: ?string, scheduledAtFormatError: ?{error: ?string, errorCode: ?string}) => { + if (!scheduledDate) { + return { valid: false, errorMessage: i18n.t('Please enter a date') }; + } + const { valid, errorMessage } = isValidDate(scheduledDate, scheduledAtFormatError); + return { + valid, + errorMessage, + }; +}; const scheduleInOrgUnit = (props) => { - const { scheduledAt, orgUnit, setErrorMessages } = props ?? {}; - const scheduledAtIsValid = !!scheduledAt && isScheduledDateValid(scheduledAt); + const { scheduledAt, scheduledAtFormatError, orgUnit, setErrorMessages } = props ?? {}; + const { valid: scheduledAtIsValid, errorMessage } = isScheduledDateValid(scheduledAt, scheduledAtFormatError); const orgUnitIsValid = isValidOrgUnit(orgUnit); if (!scheduledAtIsValid) { setErrorMessages({ - scheduledAt: i18n.t('Please provide a valid date'), + scheduledAt: errorMessage, }); } else { setErrorMessages({ diff --git a/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/relatedStageEventIsValid.js b/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/relatedStageEventIsValid.js index 21e7693f2f..f9f63ac48c 100644 --- a/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/relatedStageEventIsValid.js +++ b/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/relatedStageEventIsValid.js @@ -8,6 +8,7 @@ import { ValidationFunctionsByLinkMode } from './ValidationFunctions'; export const relatedStageWidgetIsValid = ({ linkMode, scheduledAt, + scheduledAtFormatError, orgUnit, linkedEventId, setErrorMessages, @@ -25,6 +26,7 @@ export const relatedStageWidgetIsValid = ({ return validationFunction({ scheduledAt, + scheduledAtFormatError, orgUnit, linkedEventId, setErrorMessages, diff --git a/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/relatedStageEventIsValid.types.js b/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/relatedStageEventIsValid.types.js index c97648e958..1c5e56a676 100644 --- a/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/relatedStageEventIsValid.types.js +++ b/src/core_modules/capture-core/components/WidgetRelatedStages/relatedStageEventIsValid/relatedStageEventIsValid.types.js @@ -5,6 +5,7 @@ import { actions as LinkModes } from '../constants'; export type RelatedStageIsValidProps = {| linkMode: ?$Keys, scheduledAt: ?string, + scheduledAtFormatError: ?{error: ?string, errorCode: ?string}, orgUnit: ?{ id: string, name: string,