Skip to content

Commit

Permalink
Merge pull request #155 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 878e97f + 78ed672 commit 82d7d68
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
6 changes: 3 additions & 3 deletions components/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { useMemo } from 'react'
import { Box } from 'theme-ui'
import Entry from './entry'

const List = ({ posts, year }) => {
const List = ({ posts, years }) => {
const filteredContents = useMemo(() => {
return posts.filter((d) => year[d.date.split('-')[2]])
}, [posts, year])
return posts.filter((d) => years[d.date.split('-')[2]])
}, [posts, years])

return (
<Box>
Expand Down
25 changes: 14 additions & 11 deletions components/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ import {

import List from './list'

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

const Settings = ({ setYear, year }) => {
const Settings = ({ setYears, years }) => {
return (
<Group spacing='md'>
<Filter
values={year}
setValues={setYear}
values={years}
setValues={setYears}
label='Filter by year'
showAll
/>
Expand All @@ -30,9 +26,16 @@ const Settings = ({ setYear, year }) => {
}

const Main = ({ showMobileSettings, posts }) => {
const [year, setYear] = useState(initYear)
const [years, setYears] = useState(() => {
const currentYear = getCurrentYear()
const initYears = {}
for (let year = 2021; year <= currentYear; year++) {
initYears[year] = true
}
return initYears
})

const settings = <Settings setYear={setYear} year={year} />
const settings = <Settings setYears={setYears} years={years} />

return (
<>
Expand Down Expand Up @@ -61,7 +64,7 @@ const Main = ({ showMobileSettings, posts }) => {
{settings}
</Column>
<Column start={[1, 2, 5, 5]} width={[6, 6, 7, 7]}>
<List year={year} posts={posts} />
<List years={years} posts={posts} />
</Column>
</Row>
</>
Expand Down

1 comment on commit 82d7d68

@vercel
Copy link

@vercel vercel bot commented on 82d7d68 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:

blog – ./

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

Please sign in to comment.