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
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 11 additions & 8 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 @@ -200,7 +203,7 @@ export const PeriodDimension = ({ dimension, onClose }) => {
)}
{entryMethod === OPTION_START_END_DATES && (
<StartEndDate
value={selectedIds[0] || ''}
value={getStartEndDate(selectedIds[0] || '')}
setValue={(value) => {
if (!value && selectedIds.length) {
updatePeriodDimensionItems([])
Expand Down
8 changes: 5 additions & 3 deletions src/components/Dialogs/PeriodDimension/StartEndDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
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)

useEffect(() => {
setValue(startDate && endDate ? `${startDate}_${endDate}` : '')
}, [startDate, endDate])

Check warning on line 16 in src/components/Dialogs/PeriodDimension/StartEndDate.js

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'setValue'. Either include it or remove the dependency array. If 'setValue' changes too often, find the parent component that defines it and wrap that definition in useCallback

const onStartDateChange = ({ value }) => {
setStartDate(value)
Expand Down Expand Up @@ -54,5 +56,5 @@
}
StartEndDate.propTypes = {
setValue: PropTypes.func.isRequired,
value: PropTypes.string.isRequired,
value: PropTypes.array.isRequired,
}
Loading