Skip to content

Commit

Permalink
fix: schedule date in related stages to display calendarInput interna…
Browse files Browse the repository at this point in the history
…l errors
  • Loading branch information
alaa-yahia committed Nov 4, 2024
1 parent 9745ce7 commit 4871302
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 18 deletions.
10 changes: 5 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-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..."
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow
import React, { useState } from 'react';
import { convertStringToDateFormat } from '../../../utils/converters/date';
import {
DateField,
withDefaultFieldContainer,
Expand All @@ -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,
|}
Expand All @@ -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 (
<DateFieldForForm
label={scheduledLabel}
value={relatedStagesDataValues.scheduledAt ? convertStringToDateFormat(relatedStagesDataValues.scheduledAt) : ''}
value={relatedStagesDataValues.scheduledAt ? relatedStagesDataValues.scheduledAt : ''}
required
onSetFocus={() => {}}
onFocus={() => {}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
}));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const WidgetRelatedStagesPlain = ({
const [relatedStageDataValues, setRelatedStageDataValues] = useState<RelatedStageDataValueStates>({
linkMode: undefined,
scheduledAt: '',
scheduledAtFormatError: undefined,
orgUnit: undefined,
linkedEventId: undefined,
});
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Props = {|
export type RelatedStageDataValueStates = {|
linkMode: ?$Keys<typeof LinkModes>,
scheduledAt: string,
scheduledAtFormatError: ?{error: ?string, errorCode: ?string},
orgUnit: ?{
path: string,
id: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ValidationFunctionsByLinkMode } from './ValidationFunctions';
export const relatedStageWidgetIsValid = ({
linkMode,
scheduledAt,
scheduledAtFormatError,
orgUnit,
linkedEventId,
setErrorMessages,
Expand All @@ -25,6 +26,7 @@ export const relatedStageWidgetIsValid = ({

return validationFunction({
scheduledAt,
scheduledAtFormatError,
orgUnit,
linkedEventId,
setErrorMessages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { actions as LinkModes } from '../constants';
export type RelatedStageIsValidProps = {|
linkMode: ?$Keys<typeof LinkModes>,
scheduledAt: ?string,
scheduledAtFormatError: ?{error: ?string, errorCode: ?string},
orgUnit: ?{
id: string,
name: string,
Expand Down

0 comments on commit 4871302

Please sign in to comment.