From ff824861341abd41d6ccc51df810f8caee490b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= Date: Sun, 5 Jan 2025 03:10:45 +0100 Subject: [PATCH] feat: add class selection functionality to header and room table components --- src/app/components/Header/index.tsx | 37 +++++++++++++++++++++++++- src/app/components/Room/Table.tsx | 4 ++- src/app/stores/classStore.tsx | 40 +++++++++++++++++++++++++++++ src/app/stores/roomStore.tsx | 19 +++++--------- 4 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 src/app/stores/classStore.tsx diff --git a/src/app/components/Header/index.tsx b/src/app/components/Header/index.tsx index bf2df76..d0273af 100644 --- a/src/app/components/Header/index.tsx +++ b/src/app/components/Header/index.tsx @@ -1,6 +1,8 @@ "use client"; import { User } from "@/app/types/next-auth"; import { + Autocomplete, + AutocompleteItem, Avatar, Button, Dropdown, @@ -14,6 +16,8 @@ import { } from "@nextui-org/react"; import { useRouter } from "next/navigation"; import { ThemeSwitcher } from "../ThemeSwitcher/ThemeSwitcher"; +import { useClassStore } from "@/app/stores/classStore"; +import { useEffect, useRef } from "react"; const getInitials = (name: string) => { if (!name) return ""; @@ -31,15 +35,46 @@ const getInitials = (name: string) => { export const Header = ({ user }: { user?: User }) => { const router = useRouter(); + const { classes, selectedClass, setSelectedClass, fetchClass } = + useClassStore(); + const listClassesRef = useRef(null); const initials = user?.name ? getInitials(user.name) : ""; + useEffect(() => { + fetchClass().then(() => { + if (selectedClass) setSelectedClass(classes[0]); + }); + }, []); + return (

Toogether

+ + { + console.log(selectedId); + const inputSelectedClass = classes.find( + (Class) => Class.id === selectedId, + ); + setSelectedClass(inputSelectedClass); + }} + > + {classes.map((Class) => ( + + {Class.name} + + ))} + + + {user?.roles.includes("admin") ? ( @@ -53,7 +88,7 @@ export const Header = ({ user }: { user?: User }) => { ) : null} - + diff --git a/src/app/components/Room/Table.tsx b/src/app/components/Room/Table.tsx index 518384a..48bb44d 100644 --- a/src/app/components/Room/Table.tsx +++ b/src/app/components/Room/Table.tsx @@ -2,13 +2,15 @@ import { useRoomStore } from "@/app/stores/roomStore"; 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(); useEffect(() => { fetchRooms(); - }, []); + }, [selectedClass]); return (
diff --git a/src/app/stores/classStore.tsx b/src/app/stores/classStore.tsx new file mode 100644 index 0000000..6247505 --- /dev/null +++ b/src/app/stores/classStore.tsx @@ -0,0 +1,40 @@ +import { create } from "zustand"; +import { axiosInstance } from "../lib/axios"; + +type Class = { id: string; name: string; createdAt: string }; + +type ClassStoreState = { + classes: Class[]; + selectedClass: Class | undefined | null; +}; + +type ClassStoreActions = { + _setClass: (classes: Class[]) => void; + fetchClass: () => Promise; + setSelectedClass: (selectedClass: Class | undefined | null) => void; +}; + +type ClassStore = ClassStoreState & ClassStoreActions; + +const defaultState: ClassStoreState = { + classes: [], + selectedClass: null, +}; + +export const useClassStore = create()((set) => ({ + ...defaultState, + _setClass: (classes: Class[]) => { + set(() => ({ + classes: classes, + })); + }, + setSelectedClass: (selectedClass: Class | undefined | null) => { + set(() => ({ + selectedClass: selectedClass, + })); + }, + fetchClass: async () => { + const classResponse = await axiosInstance.get("/@me/class"); + useClassStore.getState()._setClass(classResponse.data); + }, +})); diff --git a/src/app/stores/roomStore.tsx b/src/app/stores/roomStore.tsx index 735e370..a6e781a 100644 --- a/src/app/stores/roomStore.tsx +++ b/src/app/stores/roomStore.tsx @@ -2,6 +2,7 @@ import moment from "moment"; import { create } from "zustand"; import { Room } from "../components/Room/Room"; import { axiosInstance } from "../lib/axios"; +import { useClassStore } from "./classStore"; type RoomStoreState = { future: Room[]; @@ -37,19 +38,13 @@ export const useRoomStore = create()((set) => ({ set({ future, actual, past }); }, fetchRooms: () => { + const classSelected = useClassStore.getState().selectedClass; + if (!classSelected) return; + axiosInstance - .get< - { id: string; name: string; createdAt: string }[] - >("/@me/class") - .then((classResponse) => { - if (classResponse.data.length) - axiosInstance - .get< - Room[] - >(`/@me/class/${classResponse.data[0].id}/rooms`) - .then((classes) => { - useRoomStore.getState()._setRooms(classes.data); - }); + .get(`/@me/class/${classSelected.id}/rooms`) + .then((classes) => { + useRoomStore.getState()._setRooms(classes.data); }); }, }));