Skip to content

Commit

Permalink
Merge pull request #400 from carbonplan/dynamic-year-filter
Browse files Browse the repository at this point in the history
Dynamic year filter
  • Loading branch information
Shane98c authored Dec 6, 2023
2 parents 7827a44 + 7ed516d commit 30449bc
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions pages/press.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,37 @@ const initFormat = {
video: true,
}

const initYear = {
2020: true,
2021: true,
2022: true,
2023: true,
}
const getCurrentYear = () => new Date().getFullYear()

const Press = () => {
const [format, setFormat] = useState(initFormat)
const [year, setYear] = useState(initYear)
const [years, setYears] = useState(() => {
const currentYear = getCurrentYear()
const initYear = {}
for (let year = 2020; year <= currentYear; year++) {
initYear[year] = true
}
return initYear
})
const [filtered, setFiltered] = useState(press)
const [expanded, setExpanded] = useState(false)

useEffect(() => {
setFiltered(
press.filter((d) => {
const inYear = year[new Date(d.date.replace(/-/g, '/')).getFullYear()]
const inYear = years[new Date(d.date.replace(/-/g, '/')).getFullYear()]
const inFormat = format[d.format]
return inYear && inFormat
})
)
}, [year, format])
}, [years, format])

const FilterContents = () => {
return (
<Group spacing='md'>
<Filter
values={year}
setValues={setYear}
values={years}
setValues={setYears}
label='Filter by year'
showAll
/>
Expand Down

1 comment on commit 30449bc

@vercel
Copy link

@vercel vercel bot commented on 30449bc Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

carbonplan.org – ./

carbonplan.org
carbonplanorg-carbonplan.vercel.app
carbonplanorg-git-main-carbonplan.vercel.app

Please sign in to comment.