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

Dynamic year filter #155

Merged
merged 3 commits into from
Dec 6, 2023
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
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
Loading