From 2ec652d34bad73ae0a144bea9c71ba33386aee36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Dere=C5=BEi=C4=87?= Date: Fri, 2 Feb 2024 10:33:52 +0100 Subject: [PATCH] BugFix: month list did not expand appropriate month based od search params --- app/ui/MonthLocationList.tsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/ui/MonthLocationList.tsx b/app/ui/MonthLocationList.tsx index 06d945f..eb484a1 100644 --- a/app/ui/MonthLocationList.tsx +++ b/app/ui/MonthLocationList.tsx @@ -7,7 +7,7 @@ import { MonthCard } from "./MonthCard"; import Pagination from "./Pagination"; import { LocationCard } from "./LocationCard"; import { BillingLocation, YearMonth } from "../lib/db-types"; -import { useParams, useRouter } from "next/navigation"; +import { useParams, useRouter, useSearchParams } from "next/navigation"; const getNextYearMonth = (yearMonth:YearMonth) => { const {year, month} = yearMonth; @@ -34,7 +34,13 @@ export const MonthLocationList:React.FC = ({ }) => { const router = useRouter(); - const params = useParams(); + const search = useSearchParams(); + + const initialMonth = search.get('month') + + const [expandedMonth, setExpandedMonth] = React.useState( + initialMonth ? parseInt(initialMonth as string) : -1 // no month is expanded + ); if(!availableYears || !months) { const currentYearMonth:YearMonth = { @@ -50,12 +56,6 @@ export const MonthLocationList:React.FC = ({ ) }; - const { month: initialMonth } = params; - - const [expandedMonth, setExpandedMonth] = React.useState( - initialMonth ? parseInt(initialMonth as string) : new Date().getMonth() + 1 - ); - const monthsArray = Object.entries(months); // when the month is toggled, update the URL