BugFix: month list did not expand appropriate month based od search params

This commit is contained in:
2024-02-02 10:33:52 +01:00
parent 6d33c5c117
commit 2ec652d34b

View File

@@ -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<MonthLocationListProps > = ({
}) => {
const router = useRouter();
const params = useParams();
const search = useSearchParams();
const initialMonth = search.get('month')
const [expandedMonth, setExpandedMonth] = React.useState<number>(
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<MonthLocationListProps > = ({
</>)
};
const { month: initialMonth } = params;
const [expandedMonth, setExpandedMonth] = React.useState<number>(
initialMonth ? parseInt(initialMonth as string) : new Date().getMonth() + 1
);
const monthsArray = Object.entries(months);
// when the month is toggled, update the URL