Skip to content

Commit

Permalink
chore: bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
turban committed Sep 12, 2023
1 parent 0c9957c commit 3f3445a
Showing 1 changed file with 26 additions and 41 deletions.
67 changes: 26 additions & 41 deletions src/components/periods/PeriodSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@dhis2/ui'
import cx from 'classnames'
import PropTypes from 'prop-types'
import React, { useState, useCallback, useEffect } from 'react'
import React, { useState, useMemo, useCallback, useEffect } from 'react'
import usePrevious from '../../hooks/usePrevious.js'
import {
getFixedPeriodsByType,
Expand All @@ -26,9 +26,22 @@ const PeriodSelect = ({
period,
periodType,
}) => {
const [year, setYear] = useState()
const [periods, setPeriods] = useState()
const prevPeriods = usePrevious(periods)
const [year, setYear] = useState(getYear(period?.startDate || lastDate))
const prevYear = usePrevious(year)

// Set periods when periodType or year changes
const periods = useMemo(
() =>
periodType
? getFixedPeriodsByType({
periodType,
year,
firstDate,
lastDate,
})
: [period], // saved map period
[periodType, year, firstDate, lastDate]

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

View workflow job for this annotation

GitHub Actions / lint

React Hook useMemo has a missing dependency: 'period'. Either include it or remove the dependency array

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

View workflow job for this annotation

GitHub Actions / lint

React Hook useMemo has a missing dependency: 'period'. Either include it or remove the dependency array
)

// Increment/decrement year
const changeYear = useCallback(
Expand All @@ -45,49 +58,21 @@ const PeriodSelect = ({
[year, firstDate, lastDate]
)

// Set initial year
useEffect(() => {
if (!year) {
setYear(getYear(period?.startDate || lastDate))
}
}, [year, period, lastDate])

// Set periods when year changes
// Autoselect most recent period
useEffect(() => {
if (year && periodType) {
setPeriods(
getFixedPeriodsByType({ periodType, year, firstDate, lastDate })
)
if (!period) {
onChange(filterFuturePeriods(periods)[0] || periods[0])
}
}, [year, periodType, firstDate, lastDate])
}, [period, periods, year, onChange])

// If period is loaded in favorite
// Keep the same period position when year changes
useEffect(() => {
if (!periodType && !periods && period) {
setPeriods([period])
}
}, [periodType, period, periods])
if (period && !periods.some((p) => p.id === period.id)) {
const periodId = period.id.replace(prevYear, year)

useEffect(() => {
if (periods) {
if (!period) {
// Autoselect most recent period
onChange(filterFuturePeriods(periods)[0] || periods[0])
} else if (
!periods.find((p) => p.id === period.id) &&
prevPeriods
) {
// Change period if year is changed (but keep period index)
const periodIndex = prevPeriods.findIndex(
(p) => p.id === period.id
)

if (periodIndex !== -1) {
onChange(periods[periodIndex])
}
}
onChange(periods.find((p) => p.id === periodId))
}
}, [period, periods, prevPeriods, onChange])
}, [period, periods, year, prevYear, onChange])

if (!periods) {
return null
Expand Down

0 comments on commit 3f3445a

Please sign in to comment.