From 24c1b0d6ec8d7de473e8fec4411688819f157177 Mon Sep 17 00:00:00 2001 From: Jacob Boyles Date: Wed, 26 Mar 2025 17:56:18 -0400 Subject: [PATCH] stats correctly handle updates --- app/statistics/page.tsx | 51 ++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/app/statistics/page.tsx b/app/statistics/page.tsx index 54cdc8d..c9e363b 100644 --- a/app/statistics/page.tsx +++ b/app/statistics/page.tsx @@ -65,29 +65,42 @@ export default function StaffingRequest() { setLoading(false); }); } - }, [authContext, router, month, year]); + const currentParams = Object.fromEntries(params.entries()); + const newParams = { ...currentParams, month: `${month}`, year: `${year}` }; + const queryString = new URLSearchParams(newParams).toString(); - async function nextMonth() { + router.push(`?${queryString}`, { scroll: false }); + setLoading(false); + }, [authContext, month, params, router, year]); + + const nextMonth = () => { setLoading(true); - if (month === 12) { - setMonth(1); - setYear(year + 1); - } else { - setMonth(month + 1); - } - router.push(`/statistics?month=${month}&year=${year}`); - } - async function previousMonth() { + setMonth((prevMonth) => { + let newMonth = prevMonth + 1; + let newYear = year; + if (newMonth > 12) { + newMonth = 1; + newYear += 1; + } + setYear(newYear); + return newMonth; + }); + }; + + const previousMonth = () => { setLoading(true); - if (month === 1) { - setMonth(12); - setYear(year - 1); - } else { - setMonth(month - 1); - } - router.push(`/statistics?month=${month}&year=${year}`); - } + setMonth((prevMonth) => { + let newMonth = prevMonth - 1; + let newYear = year; + if (newMonth < 1) { + newMonth = 12; + newYear -= 1; + } + setYear(newYear); + return newMonth; + }); + }; return (