Skip to content

Commit

Permalink
chore: pass format and calendar as props
Browse files Browse the repository at this point in the history
  • Loading branch information
alaa-yahia committed Jan 13, 2025
1 parent 9805435 commit d67810b
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
attributeOptionsKey,
getCategoryOptionsValidatorContainers, withAOCFieldBuilder, withDataEntryFields,
} from '../../../../DataEntryDhis2Helpers';
import { systemSettingsStore } from '../../../../../metaDataMemoryStores';

const getStyles = theme => ({
savingContextContainer: {
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
attributeOptionsKey,
getCategoryOptionsValidatorContainers,
} from '../../DataEntryDhis2Helpers';
import { systemSettingsStore } from '../../../metaDataMemoryStores';

const getStyles = theme => ({
savingContextContainer: {
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -42,6 +43,8 @@ const ScheduleDatePlain = ({
}
setScheduleDate(e);
}}
calendarType={systemSettingsStore.get().calendar}
dateFormat={systemSettingsStore.get().dateFormat}
/>
</div>}
<InfoBox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../FormFields/New';
import labelTypeClasses from './dataEntryFieldLabels.module.css';
import { baseInputStyles } from './commonProps';
import { systemSettingsStore } from '../../../metaDataMemoryStores';
import type { ErrorMessagesForRelatedStages } from '../RelatedStagesActions';
import type { RelatedStageDataValueStates } from '../WidgetRelatedStages.types';

Expand Down Expand Up @@ -46,7 +47,8 @@ export const DateFieldForRelatedStages = ({
setTouched(true);
onBlurDateField(event, internalComponentError);
};

const calendarType = systemSettingsStore.get().calendar;
const dateFormat = systemSettingsStore.get().dateFormat;
const shouldShowError = (touched || saveAttempted);
return (
<DateFieldForForm
Expand All @@ -60,6 +62,8 @@ export const DateFieldForRelatedStages = ({
calendarWidth={350}
onBlur={onBlur}
errorMessage={shouldShowError && errorMessages?.scheduledAt}
calendarType={calendarType}
dateFormat={dateFormat}
/>
);
};
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
};

0 comments on commit d67810b

Please sign in to comment.