diff --git a/src/components/Dialogs/PeriodDimension/PeriodDimension.js b/src/components/Dialogs/PeriodDimension/PeriodDimension.js index f9cfe4626..e741a6ac9 100644 --- a/src/components/Dialogs/PeriodDimension/PeriodDimension.js +++ b/src/components/Dialogs/PeriodDimension/PeriodDimension.js @@ -41,15 +41,18 @@ import { StartEndDate } from './StartEndDate.js' export const OPTION_PRESETS = 'PRESETS' export const OPTION_START_END_DATES = 'START_END_DATES' -const isStartEndDate = (id) => { - const parts = id.split('_') - return ( - parts.length === 2 && +const getStartEndDate = (id) => { + const { dimensionId: periodId } = extractDimensionIdParts(id) + const parts = periodId.split('_') + return parts.length === 2 && !isNaN(Date.parse(parts[0])) && !isNaN(Date.parse(parts[1])) - ) + ? parts + : [] } +const isStartEndDate = (id) => getStartEndDate(id).length === 2 + const useIsInLayout = (dimensionId) => { const allDimensionIds = useSelector(sGetDimensionIdsFromLayout) return useMemo( @@ -68,8 +71,7 @@ const useLocalizedStartEndDateFormatter = () => { ) return (startEndDate) => { if (isStartEndDate(startEndDate)) { - return startEndDate - .split('_') + return getStartEndDate(startEndDate) .map((dateStr) => formatter.format(new Date(dateStr))) .join(' - ') } else { @@ -117,6 +119,7 @@ export const PeriodDimension = ({ dimension, onClose }) => { const selectedIds = useSelector((state) => sGetUiItemsByDimension(state, dimension?.id) ) + const [entryMethod, setEntryMethod] = useState( selectedIds.filter((id) => isStartEndDate(id)).length ? OPTION_START_END_DATES @@ -200,7 +203,7 @@ export const PeriodDimension = ({ dimension, onClose }) => { )} {entryMethod === OPTION_START_END_DATES && ( { if (!value && selectedIds.length) { updatePeriodDimensionItems([]) diff --git a/src/components/Dialogs/PeriodDimension/StartEndDate.js b/src/components/Dialogs/PeriodDimension/StartEndDate.js index d786d99cf..320d4c97f 100644 --- a/src/components/Dialogs/PeriodDimension/StartEndDate.js +++ b/src/components/Dialogs/PeriodDimension/StartEndDate.js @@ -4,8 +4,10 @@ import PropTypes from 'prop-types' import React, { useEffect, useState } from 'react' import styles from './StartEndDate.module.css' -export const StartEndDate = ({ value, setValue }) => { - const [startDateStr, endDateStr] = value ? value.split('_') : [] +export const StartEndDate = ({ + value: [startDateStr, endDateStr], + setValue, +}) => { const [startDate, setStartDate] = useState(startDateStr) const [endDate, setEndDate] = useState(endDateStr) @@ -54,5 +56,5 @@ export const StartEndDate = ({ value, setValue }) => { } StartEndDate.propTypes = { setValue: PropTypes.func.isRequired, - value: PropTypes.string.isRequired, + value: PropTypes.array.isRequired, }