Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extract the start-end-date part of the item id #486

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/components/Dialogs/PeriodDimension/PeriodDimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -164,6 +167,15 @@ export const PeriodDimension = ({ dimension, onClose }) => {
updatePeriodDimensionItems([])
}

const selectedPeriods = selectedIds.map((id) => ({
id,
name: getNameFromMetadata(id),
}))

const { dimensionId: selectedStartEndDate } = extractDimensionIdParts(
selectedIds[0] || ''
)

return dimension ? (
<DimensionModal
dataTest={'period-dimension-modal'}
Expand All @@ -188,10 +200,7 @@ export const PeriodDimension = ({ dimension, onClose }) => {
<div className={styles.entry}>
{entryMethod === OPTION_PRESETS && (
<BasePeriodDimension
selectedPeriods={selectedIds.map((id) => ({
id,
name: getNameFromMetadata(id),
}))}
selectedPeriods={selectedPeriods}
onSelect={({ items }) =>
updatePeriodDimensionItems(items)
}
Expand All @@ -200,7 +209,7 @@ export const PeriodDimension = ({ dimension, onClose }) => {
)}
{entryMethod === OPTION_START_END_DATES && (
<StartEndDate
value={selectedIds[0] || ''}
value={selectedStartEndDate}
setValue={(value) => {
if (!value && selectedIds.length) {
updatePeriodDimensionItems([])
Expand Down
Loading