Skip to content

Commit 9305b34

Browse files
authored
Add filtering by kind, day and place in ScheduleTable
* Add day parameter handling in ScheduleTable for improved navigation * Add filtering by kind and place in ScheduleTable * Refactor day parameter handling in ScheduleTable to improve navigation and simplify state updates
1 parent b8b45ec commit 9305b34

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/app/(authenticated)/schedule/ScheduleTable.tsx

+39-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import GridList from "@/components/GridList";
55
import List from "@/components/List";
66
import { SessionTile } from "@/components/session";
77
import { getEventFullDate } from "@/utils/utils";
8-
import { useSearchParams } from "next/navigation";
8+
import { usePathname, useRouter, useSearchParams } from "next/navigation";
99
import { useEffect, useMemo, useState } from "react";
1010

1111
interface ScheduleTableProps {
@@ -14,25 +14,31 @@ interface ScheduleTableProps {
1414

1515
export default function ScheduleTable({ sessions }: ScheduleTableProps) {
1616
const searchParams = useSearchParams();
17+
const pathname = usePathname();
18+
const router = useRouter();
1719
const [showingDay, setShowingDay] = useState<string | null>(null);
1820

21+
const dayParam = searchParams.get("day");
22+
const kindParam = searchParams.get("kind");
23+
const placeParam = searchParams.get("place");
24+
1925
const sessionsByDay = useMemo(() => {
2026
const sortedSessions = sessions.sort((a, b) =>
21-
a.date.localeCompare(b.date),
27+
a.date.localeCompare(b.date)
2228
);
2329
return sortedSessions.reduce(
2430
(acc, s) => {
2531
const day = s.date.split("T")[0];
2632
const daySessions = [...(acc[day] || []), s];
2733
return { ...acc, [day]: daySessions };
2834
},
29-
{} as Record<string, SINFOSession[]>,
35+
{} as Record<string, SINFOSession[]>
3036
);
3137
}, [sessions]);
3238

3339
const sortedDays = useMemo(
3440
() => Object.keys(sessionsByDay).sort(),
35-
[sessionsByDay],
41+
[sessionsByDay]
3642
);
3743

3844
useEffect(() => {
@@ -42,16 +48,33 @@ export default function ScheduleTable({ sessions }: ScheduleTableProps) {
4248
setShowingDay(sortedDays.find((d) => day === d) || null);
4349
}, [sortedDays, searchParams]);
4450

51+
const updateSearchParam = (newDay: string) => {
52+
const params = new URLSearchParams(searchParams.toString());
53+
params.delete("kind");
54+
params.delete("place");
55+
56+
if (newDay === dayParam) {
57+
params.delete("day");
58+
} else {
59+
params.set("day", newDay);
60+
}
61+
router.push(`${pathname}?${params.toString()}`, { scroll: false });
62+
};
63+
64+
useEffect(() => {
65+
if (dayParam && sortedDays.includes(dayParam)) {
66+
setShowingDay(dayParam);
67+
}
68+
}, [dayParam, sortedDays]);
69+
4570
return (
4671
<>
4772
<GridList>
4873
{sortedDays.map((d) => (
4974
<EventDayButton
5075
key={`event-day-${d}`}
5176
date={d}
52-
onClick={() =>
53-
setShowingDay((currentDay) => (currentDay === d ? null : d))
54-
}
77+
onClick={() => updateSearchParam(d)}
5578
selected={showingDay === d}
5679
/>
5780
))}
@@ -60,9 +83,15 @@ export default function ScheduleTable({ sessions }: ScheduleTableProps) {
6083
.filter((d) => !showingDay || d === showingDay)
6184
.map((d) => (
6285
<List key={d} title={getEventFullDate(d)}>
63-
{sessionsByDay[d].map((s) => (
64-
<SessionTile key={s.id} session={s} onlyShowHours={true} />
65-
))}
86+
{sessionsByDay[d]
87+
.filter(
88+
(s) =>
89+
(!kindParam || s.kind === kindParam) &&
90+
(!placeParam || s.place === placeParam)
91+
)
92+
.map((s) => (
93+
<SessionTile key={s.id} session={s} onlyShowHours={true} />
94+
))}
6695
</List>
6796
))}
6897
</>

0 commit comments

Comments
 (0)