From 1684ac2743e0826d04ce59cdacd11071474fd16c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= Date: Mon, 6 Jan 2025 00:31:25 +0100 Subject: [PATCH] feat: refactor class selection handling by introducing user store and updating related components --- src/app/components/Header/contents.tsx | 32 ++++++++++++++++---------- src/app/components/Room/Table.tsx | 8 +++---- src/app/components/Sidebar/index.tsx | 2 +- src/app/services/class.service.ts | 1 - src/app/stores/classStore.tsx | 10 -------- src/app/stores/roomStore.tsx | 8 +++---- src/app/stores/userStore.ts | 31 +++++++++++++++++++++++++ src/app/types/class.d.ts | 5 ++++ 8 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 src/app/stores/userStore.ts create mode 100644 src/app/types/class.d.ts diff --git a/src/app/components/Header/contents.tsx b/src/app/components/Header/contents.tsx index 5894e81..e2ab9cf 100644 --- a/src/app/components/Header/contents.tsx +++ b/src/app/components/Header/contents.tsx @@ -1,5 +1,4 @@ "use client"; -import { useClassStore } from "@/app/stores/classStore"; import { User } from "@/app/types/next-auth"; import { getInitials } from "@/app/utils/initial"; import { @@ -19,19 +18,28 @@ import { import { useRouter } from "next/navigation"; import { useEffect } from "react"; import { ThemeSwitcher } from "../ThemeSwitcher/ThemeSwitcher"; +import { useUserStore } from "@/app/stores/userStore"; +import { useClassStore } from "@/app/stores/classStore"; export const HeaderContent = ({ user }: { user?: User }) => { const router = useRouter(); - const { classes, selectedClass, setSelectedClass, fetchClass } = - useClassStore(); - const initials = user?.name ? getInitials(user.name) : ""; + const { classes, fetchClass } = useClassStore(); + const { + currentClassId: selectedClassId, + setCurrentClassId: setSelectedClassId, + } = useUserStore(); useEffect(() => { - fetchClass().then((classesFetched) => { - setSelectedClass(classesFetched[0]); - }); - }, [fetchClass, setSelectedClass]); + fetchClass(); + }, [fetchClass]); + + const selectedClass = classes.find((Class) => Class.id === selectedClassId); + + useEffect(() => { + if (!selectedClass && classes.length > 0) + setSelectedClassId(classes[0].id); + }, [selectedClass, classes, setSelectedClassId]); return ( @@ -46,15 +54,15 @@ export const HeaderContent = ({ user }: { user?: User }) => { value={selectedClass?.name} selectedKey={selectedClass?.id} onSelectionChange={(selectedId) => { - console.log(selectedId); const inputSelectedClass = classes.find( (Class) => Class.id === selectedId, ); - setSelectedClass(inputSelectedClass); + if (inputSelectedClass) + setSelectedClassId(inputSelectedClass.id); }} > {classes.map((Class) => ( - + {Class.name} ))} @@ -86,7 +94,7 @@ export const HeaderContent = ({ user }: { user?: User }) => { as="button" className="transition-transform" color="secondary" - name={initials} + name={user?.name ? getInitials(user.name) : ""} size="sm" /> diff --git a/src/app/components/Room/Table.tsx b/src/app/components/Room/Table.tsx index bcf0f56..496252f 100644 --- a/src/app/components/Room/Table.tsx +++ b/src/app/components/Room/Table.tsx @@ -1,16 +1,16 @@ "use client"; import { useRoomStore } from "@/app/stores/roomStore"; +import { useUserStore } from "@/app/stores/userStore"; import { useEffect } from "react"; import { RoomList } from "./List"; -import { useClassStore } from "@/app/stores/classStore"; export const RoomTable = () => { const { fetchRooms, actual, future, past } = useRoomStore(); - const { selectedClass } = useClassStore(); + const { currentClassId: selectedClassId } = useUserStore(); useEffect(() => { - fetchRooms(); - }, [fetchRooms, selectedClass]); + if (selectedClassId) fetchRooms(); + }, [fetchRooms, selectedClassId]); return (
diff --git a/src/app/components/Sidebar/index.tsx b/src/app/components/Sidebar/index.tsx index 90e2a4d..77fc4a5 100644 --- a/src/app/components/Sidebar/index.tsx +++ b/src/app/components/Sidebar/index.tsx @@ -10,7 +10,7 @@ export const Sidebar = () => { const router = useRouter(); return ( -