From a34086280be38bceb7a64326e704b7392be53d2a Mon Sep 17 00:00:00 2001 From: Shane Loeffler Date: Tue, 5 Dec 2023 14:22:00 -0600 Subject: [PATCH 1/2] dynamic years for filter values --- pages/press.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pages/press.js b/pages/press.js index 8e61b7b4..2fcfb952 100644 --- a/pages/press.js +++ b/pages/press.js @@ -108,11 +108,11 @@ const initFormat = { video: true, } -const initYear = { - 2020: true, - 2021: true, - 2022: true, - 2023: true, +const currentYear = new Date().getFullYear() +const initYear = {} + +for (let year = 2020; year <= currentYear; year++) { + initYear[year] = true } const Press = () => { From 7ed516d02a1aa7806c24011c7fbca5bf5aceacf7 Mon Sep 17 00:00:00 2001 From: Shane Loeffler Date: Wed, 6 Dec 2023 09:12:15 -0600 Subject: [PATCH 2/2] eval currentYear at runtime, plural years --- pages/press.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/pages/press.js b/pages/press.js index 2fcfb952..98bd7038 100644 --- a/pages/press.js +++ b/pages/press.js @@ -108,35 +108,37 @@ const initFormat = { video: true, } -const currentYear = new Date().getFullYear() -const initYear = {} - -for (let year = 2020; year <= currentYear; year++) { - initYear[year] = 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 (