Skip to content

Commit

Permalink
chore: year change period fix
Browse files Browse the repository at this point in the history
  • Loading branch information
turban committed Sep 28, 2023
1 parent 7babfb5 commit b79ce3e
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/components/periods/PeriodSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const PeriodSelect = ({
periodType,
}) => {
const [year, setYear] = useState(getYear(period?.startDate || lastDate))
// const [periods, setPeriods] = useState()
const prevYear = usePrevious(year)
// const prevPeriods = usePrevious(periods)

// Set periods when periodType or year changes
/* eslint-disable react-hooks/exhaustive-deps */
Expand All @@ -45,6 +47,13 @@ const PeriodSelect = ({
)
/* eslint-enable react-hooks/exhaustive-deps */

const periodIndex = useMemo(
() => period && periods.findIndex((p) => p.id === period.id),
[period, periods]
)

const prevPeriodIndex = usePrevious(periodIndex)

// Increment/decrement year
const changeYear = useCallback(
(change) => {
Expand All @@ -60,21 +69,46 @@ const PeriodSelect = ({
[year, firstDate, lastDate]
)

// Set periods when periodType or year changes
/*
useEffect(() => {
if (periodType) {
setPeriods(
getFixedPeriodsByType({
periodType,
year,
firstDate,
lastDate,
})
)
}
}, [periodType, year, firstDate, lastDate])
// Set saved map period
useEffect(() => {
if (!periodType && period) {
setPeriods([period])
}
}, [periodType, period])
*/

// Autoselect most recent period
useEffect(() => {
if (!period) {
if (!period && periods) {
onChange(filterFuturePeriods(periods)[0] || periods[0])
}
}, [period, periods, year, onChange])

// Keep the same period position when year changes
useEffect(() => {
if (period && !periods.some((p) => p.id === period.id)) {
const periodId = period.id.replace(prevYear, year)
if (year !== prevYear && prevPeriodIndex >= 0) {
const newPeriod = periods[prevPeriodIndex]

onChange(periods.find((p) => p.id === periodId))
if (newPeriod) {
onChange(newPeriod)
}
}
}, [period, periods, year, prevYear, onChange])
}, [year, prevYear, periods, prevPeriodIndex])

Check warning on line 111 in src/components/periods/PeriodSelect.js

View workflow job for this annotation

GitHub Actions / lint

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

if (!periods) {
return null
Expand Down

0 comments on commit b79ce3e

Please sign in to comment.