From d67810bedf6a73c62ebe17735231f5be4c60366e Mon Sep 17 00:00:00 2001 From: alaa-yahia Date: Mon, 13 Jan 2025 12:54:15 +0200 Subject: [PATCH] chore: pass format and calendar as props --- .../dateField/getDateFieldConfigForCustomForm.js | 3 +++ .../dateRangeField/getDateRangeFieldConfig.js | 3 +++ .../getDateTimeFieldConfigForCustomForm.js | 3 +++ .../getDateTimeRangeFieldConfig.js | 3 +++ .../EnrollmentWithFirstStageDataEntry.component.js | 3 +++ .../DataEntry/DataEntry.component.js | 3 +++ .../DataEntry/DataEntry.component.js | 3 +++ .../EditEventDataEntry.component.js | 2 ++ .../ScheduleDate/ScheduleDate.component.js | 3 +++ .../FormComponents/DateFieldForRelatedStages.js | 6 +++++- .../validators/form/getDateTimeRangeValidator.js | 12 ++++++++++-- 11 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/core_modules/capture-core/components/D2Form/field/configs/dateField/getDateFieldConfigForCustomForm.js b/src/core_modules/capture-core/components/D2Form/field/configs/dateField/getDateFieldConfigForCustomForm.js index efeea1412c..9f5b057d0d 100644 --- a/src/core_modules/capture-core/components/D2Form/field/configs/dateField/getDateFieldConfigForCustomForm.js +++ b/src/core_modules/capture-core/components/D2Form/field/configs/dateField/getDateFieldConfigForCustomForm.js @@ -3,6 +3,7 @@ import moment from 'moment'; import { createFieldConfig, createProps } from '../base/configBaseCustomForm'; import { DateFieldForCustomForm } from '../../Components'; import { convertDateObjectToDateFormatString } from '../../../../../../capture-core/utils/converters/date'; +import { systemSettingsStore } from '../../../../../metaDataMemoryStores'; import type { DateDataElement } from '../../../../../metaData'; import type { QuerySingleResource } from '../../../../../utils/api/api.types'; @@ -12,6 +13,8 @@ export const getDateFieldConfigForCustomForm = (metaData: DateDataElement, optio maxWidth: 350, calendarWidth: 350, calendarMax: !metaData.allowFutureDate ? convertDateObjectToDateFormatString(moment()) : undefined, + calendarType: systemSettingsStore.get().calendar, + dateFormat: systemSettingsStore.get().dateFormat, }, metaData); return createFieldConfig({ diff --git a/src/core_modules/capture-core/components/D2Form/field/configs/dateRangeField/getDateRangeFieldConfig.js b/src/core_modules/capture-core/components/D2Form/field/configs/dateRangeField/getDateRangeFieldConfig.js index 283da64708..90fec23265 100644 --- a/src/core_modules/capture-core/components/D2Form/field/configs/dateRangeField/getDateRangeFieldConfig.js +++ b/src/core_modules/capture-core/components/D2Form/field/configs/dateRangeField/getDateRangeFieldConfig.js @@ -1,6 +1,7 @@ // @flow import { createFieldConfig, createProps } from '../base/configBaseDefaultForm'; import { DateRangeFieldForForm } from '../../Components'; +import { systemSettingsStore } from '../../../../../metaDataMemoryStores'; import type { DataElement as MetaDataElement } from '../../../../../metaData'; import type { QuerySingleResource } from '../../../../../utils/api/api.types'; @@ -14,6 +15,8 @@ export const getDateRangeFieldConfig = (metaData: MetaDataElement, options: Obje maxWidth: options.formHorizontal ? 150 : 350, calendarWidth: options.formHorizontal ? 250 : 350, popupAnchorPosition: getCalendarAnchorPosition(options.formHorizontal), + calendarType: systemSettingsStore.get().calendar, + dateFormat: systemSettingsStore.get().dateFormat, }, options, metaData); return createFieldConfig({ diff --git a/src/core_modules/capture-core/components/D2Form/field/configs/dateTimeField/getDateTimeFieldConfigForCustomForm.js b/src/core_modules/capture-core/components/D2Form/field/configs/dateTimeField/getDateTimeFieldConfigForCustomForm.js index 86024056fc..c061ea81e7 100644 --- a/src/core_modules/capture-core/components/D2Form/field/configs/dateTimeField/getDateTimeFieldConfigForCustomForm.js +++ b/src/core_modules/capture-core/components/D2Form/field/configs/dateTimeField/getDateTimeFieldConfigForCustomForm.js @@ -2,6 +2,7 @@ import { orientations } from '../../../../FormFields/New'; import { createFieldConfig, createProps } from '../base/configBaseCustomForm'; import { DateTimeFieldForCustomForm } from '../../Components'; +import { systemSettingsStore } from '../../../../../metaDataMemoryStores'; import type { DataElement as MetaDataElement } from '../../../../../metaData'; import type { QuerySingleResource } from '../../../../../utils/api/api.types'; @@ -12,6 +13,8 @@ export const getDateTimeFieldConfigForCustomForm = (metaData: MetaDataElement, o calendarWidth: '350px', orientation: orientations.HORIZONTAL, shrinkDisabled: false, + calendarType: systemSettingsStore.get().calendar, + dateFormat: systemSettingsStore.get().dateFormat, }, metaData); return createFieldConfig({ diff --git a/src/core_modules/capture-core/components/D2Form/field/configs/dateTimeRangeField/getDateTimeRangeFieldConfig.js b/src/core_modules/capture-core/components/D2Form/field/configs/dateTimeRangeField/getDateTimeRangeFieldConfig.js index cc6e6c1a1a..b4919fd991 100644 --- a/src/core_modules/capture-core/components/D2Form/field/configs/dateTimeRangeField/getDateTimeRangeFieldConfig.js +++ b/src/core_modules/capture-core/components/D2Form/field/configs/dateTimeRangeField/getDateTimeRangeFieldConfig.js @@ -2,6 +2,7 @@ import { orientations } from '../../../../FormFields/New'; import { createFieldConfig, createProps } from '../base/configBaseDefaultForm'; import { DateTimeRangeFieldForForm } from '../../Components'; +import { systemSettingsStore } from '../../../../../metaDataMemoryStores'; import type { DataElement as MetaDataElement } from '../../../../../metaData'; import type { QuerySingleResource } from '../../../../../utils/api/api.types'; @@ -17,6 +18,8 @@ export const getDateTimeRangeFieldConfig = (metaData: MetaDataElement, options: shrinkDisabled: options.formHorizontal, calendarWidth: options.formHorizontal ? 250 : 350, popupAnchorPosition: getCalendarAnchorPosition(options.formHorizontal), + calendarType: systemSettingsStore.get().calendar, + dateFormat: systemSettingsStore.get().dateFormat, }, options, metaData); return createFieldConfig({ diff --git a/src/core_modules/capture-core/components/DataEntries/Enrollment/EnrollmentWithFirstStageDataEntry/EnrollmentWithFirstStageDataEntry.component.js b/src/core_modules/capture-core/components/DataEntries/Enrollment/EnrollmentWithFirstStageDataEntry/EnrollmentWithFirstStageDataEntry.component.js index cff88329d0..e547c41eaf 100644 --- a/src/core_modules/capture-core/components/DataEntries/Enrollment/EnrollmentWithFirstStageDataEntry/EnrollmentWithFirstStageDataEntry.component.js +++ b/src/core_modules/capture-core/components/DataEntries/Enrollment/EnrollmentWithFirstStageDataEntry/EnrollmentWithFirstStageDataEntry.component.js @@ -25,6 +25,7 @@ import { withCleanUp } from './withCleanUp'; import { getEventDateValidatorContainers } from './fieldValidators/eventDate.validatorContainersGetter'; import { stageMainDataIds } from './getDataEntryPropsToInclude'; import { withTransformPropName } from '../../../../HOC'; +import { systemSettingsStore } from '../../../../metaDataMemoryStores'; const overrideMessagePropNames = { errorMessage: 'validationError', @@ -219,6 +220,8 @@ const getReportDateSettingsFn = () => { required: true, calendarWidth: props.formHorizontal ? 250 : 350, popupAnchorPosition: getCalendarAnchorPosition(props.formHorizontal), + calendarType: systemSettingsStore.get().calendar, + dateFormat: systemSettingsStore.get().dateFormat, }), getPropName: () => stageMainDataIds.OCCURRED_AT, getValidatorContainers: () => getEventDateValidatorContainers(), diff --git a/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/DataEntry.component.js b/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/DataEntry.component.js index 3743fa5dc4..ac5fb4c65d 100644 --- a/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/DataEntry.component.js +++ b/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/DataEntry.component.js @@ -52,6 +52,7 @@ import { attributeOptionsKey, getCategoryOptionsValidatorContainers, withAOCFieldBuilder, withDataEntryFields, } from '../../../../DataEntryDhis2Helpers'; +import { systemSettingsStore } from '../../../../../metaDataMemoryStores'; const getStyles = theme => ({ savingContextContainer: { @@ -162,6 +163,8 @@ const buildReportDateSettingsFn = () => { required: true, calendarWidth: props.formHorizontal ? 250 : 350, popupAnchorPosition: getCalendarAnchorPosition(props.formHorizontal), + calendarType: systemSettingsStore.get().calendar, + dateFormat: systemSettingsStore.get().dateFormat, }), getPropName: () => 'occurredAt', getValidatorContainers: () => getEventDateValidatorContainers(), diff --git a/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/DataEntry/DataEntry.component.js b/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/DataEntry/DataEntry.component.js index aa4520ca1c..3954899320 100644 --- a/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/DataEntry/DataEntry.component.js +++ b/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/DataEntry/DataEntry.component.js @@ -42,6 +42,7 @@ import { attributeOptionsKey, getCategoryOptionsValidatorContainers, } from '../../DataEntryDhis2Helpers'; +import { systemSettingsStore } from '../../../metaDataMemoryStores'; const getStyles = theme => ({ savingContextContainer: { @@ -149,6 +150,8 @@ const buildReportDateSettingsFn = () => { required: true, calendarWidth: props.formHorizontal ? 250 : 350, popupAnchorPosition: getCalendarAnchorPosition(props.formHorizontal), + calendarType: systemSettingsStore.get().calendar, + dateFormat: systemSettingsStore.get().dateFormat, }), getPropName: () => 'occurredAt', getValidatorContainers: () => getEventDateValidatorContainers(), diff --git a/src/core_modules/capture-core/components/WidgetEventEdit/EditEventDataEntry/EditEventDataEntry.component.js b/src/core_modules/capture-core/components/WidgetEventEdit/EditEventDataEntry/EditEventDataEntry.component.js index 96b1e4ff0a..5a112438b1 100644 --- a/src/core_modules/capture-core/components/WidgetEventEdit/EditEventDataEntry/EditEventDataEntry.component.js +++ b/src/core_modules/capture-core/components/WidgetEventEdit/EditEventDataEntry/EditEventDataEntry.component.js @@ -187,6 +187,8 @@ const buildScheduleDateSettingsFn = () => { calendarWidth: 350, label: props.formFoundation.getLabel('scheduledAt'), disabled: true, + calendarType: systemSettingsStore.get().calendar, + dateFormat: systemSettingsStore.get().dateFormat, }), getIsHidden: (props: Object) => props.id !== dataEntryIds.ENROLLMENT_EVENT || props.hideDueDate, getPropName: () => 'scheduledAt', diff --git a/src/core_modules/capture-core/components/WidgetEventSchedule/ScheduleDate/ScheduleDate.component.js b/src/core_modules/capture-core/components/WidgetEventSchedule/ScheduleDate/ScheduleDate.component.js index ca3f806f2d..cd5d2ab5f1 100644 --- a/src/core_modules/capture-core/components/WidgetEventSchedule/ScheduleDate/ScheduleDate.component.js +++ b/src/core_modules/capture-core/components/WidgetEventSchedule/ScheduleDate/ScheduleDate.component.js @@ -4,6 +4,7 @@ import { spacersNum } from '@dhis2/ui'; import withStyles from '@material-ui/core/styles/withStyles'; import { DateField } from 'capture-core/components/FormFields/New'; import { InfoBox } from '../InfoBox'; +import { systemSettingsStore } from '../../../metaDataMemoryStores'; import type { Props } from './scheduleDate.types'; const styles = { @@ -42,6 +43,8 @@ const ScheduleDatePlain = ({ } setScheduleDate(e); }} + calendarType={systemSettingsStore.get().calendar} + dateFormat={systemSettingsStore.get().dateFormat} /> } ); }; diff --git a/src/core_modules/capture-core/utils/validation/validators/form/getDateTimeRangeValidator.js b/src/core_modules/capture-core/utils/validation/validators/form/getDateTimeRangeValidator.js index f238469b0f..45b497a108 100644 --- a/src/core_modules/capture-core/utils/validation/validators/form/getDateTimeRangeValidator.js +++ b/src/core_modules/capture-core/utils/validation/validators/form/getDateTimeRangeValidator.js @@ -1,5 +1,6 @@ // @flow import { Temporal } from '@js-temporal/polyfill'; +import { convertStringToTemporal } from 'capture-core/utils/converters/date'; import { isValidDateTime } from './dateTimeValidator'; function isValidDateTimeWithEmptyCheck(value: ?Object) { @@ -8,7 +9,12 @@ function isValidDateTimeWithEmptyCheck(value: ?Object) { const convertDateTimeToTemporal = (value: Object) => { const { date, time } = value; - const [year, month, day] = date.split('-').map(Number); + const dateInTemporal = convertStringToTemporal(date); + + if (!dateInTemporal) { + return null; + } + const { year, month, day } = dateInTemporal; let hour; let minutes; @@ -46,6 +52,8 @@ export const getDateTimeRangeValidator = (invalidDateTimeMessage: string) => const fromDateTime = convertDateTimeToTemporal(value.from); const toDateTime = convertDateTimeToTemporal(value.to); - + if (!fromDateTime || !toDateTime) { + return false; + } return Temporal.PlainDateTime.compare(fromDateTime, toDateTime) <= 0; };