feat: enhance class and room handling by updating fetchClass return type and improving null checks in components

This commit is contained in:
Rémi 2025-01-05 22:53:51 +01:00
parent 96a0c5c4b0
commit 946bc68946
4 changed files with 22 additions and 17 deletions

View File

@ -1,4 +1,5 @@
"use client"; "use client";
import { useClassStore } from "@/app/stores/classStore";
import { User } from "@/app/types/next-auth"; import { User } from "@/app/types/next-auth";
import { import {
Autocomplete, Autocomplete,
@ -15,9 +16,8 @@ import {
NavbarItem, NavbarItem,
} from "@nextui-org/react"; } from "@nextui-org/react";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useEffect } from "react";
import { ThemeSwitcher } from "../ThemeSwitcher/ThemeSwitcher"; import { ThemeSwitcher } from "../ThemeSwitcher/ThemeSwitcher";
import { useClassStore } from "@/app/stores/classStore";
import { useEffect, useRef } from "react";
const getInitials = (name: string) => { const getInitials = (name: string) => {
if (!name) return ""; if (!name) return "";
@ -37,16 +37,14 @@ export const Header = ({ user }: { user?: User }) => {
const router = useRouter(); const router = useRouter();
const { classes, selectedClass, setSelectedClass, fetchClass } = const { classes, selectedClass, setSelectedClass, fetchClass } =
useClassStore(); useClassStore();
const listClassesRef = useRef<HTMLInputElement>(null);
const initials = user?.name ? getInitials(user.name) : ""; const initials = user?.name ? getInitials(user.name) : "";
useEffect(() => { useEffect(() => {
fetchClass().then(() => { fetchClass().then((classesFetched) => {
if (selectedClass) setSelectedClass(classes[0]); setSelectedClass(classesFetched[0]);
}); });
// eslint-disable-next-line react-hooks/exhaustive-deps }, [fetchClass, setSelectedClass]);
}, []);
return ( return (
<Navbar className="mb-2"> <Navbar className="mb-2">
@ -56,10 +54,10 @@ export const Header = ({ user }: { user?: User }) => {
<NavbarContent as="div" justify="center"> <NavbarContent as="div" justify="center">
<Autocomplete <Autocomplete
ref={listClassesRef}
size="sm" size="sm"
label="Select an class" label="Select an class"
value={selectedClass?.name} value={selectedClass?.name}
selectedKey={selectedClass?.id}
onSelectionChange={(selectedId) => { onSelectionChange={(selectedId) => {
console.log(selectedId); console.log(selectedId);
const inputSelectedClass = classes.find( const inputSelectedClass = classes.find(

View File

@ -4,7 +4,7 @@ import { RoomCard } from "./Card";
import { Room } from "./Room"; import { Room } from "./Room";
import { SkeletonRoomCard } from "./SkeletonRoomCard"; import { SkeletonRoomCard } from "./SkeletonRoomCard";
export const RoomList = ({ rooms }: { rooms: Room[] }) => { export const RoomList = ({ rooms }: { rooms: Room[] | null }) => {
const scrollContainerRef = useRef<HTMLUListElement>(null); const scrollContainerRef = useRef<HTMLUListElement>(null);
const handleWheel = (event: WheelEvent) => { const handleWheel = (event: WheelEvent) => {
@ -47,12 +47,18 @@ export const RoomList = ({ rooms }: { rooms: Room[] }) => {
}; };
}, []); }, []);
if (rooms?.length == 0) return (
<div className="flex gap-2 rounded-xl bg-default-100 p-4">
<p className="text-default-500 text-center w-full">No rooms found</p>
</div>
)
return ( return (
<ul <ul
ref={scrollContainerRef} ref={scrollContainerRef}
className="flex gap-2 overflow-x-auto scrollbar-hide rounded-xl bg-default-100 p-1" className="flex gap-2 overflow-x-auto scrollbar-hide rounded-xl bg-default-100 p-1"
> >
{rooms.length > 0 ? ( {rooms ? (
rooms.map((room) => ( rooms.map((room) => (
<li key={room.id}> <li key={room.id}>
<RoomCard <RoomCard

View File

@ -10,7 +10,7 @@ type ClassStoreState = {
type ClassStoreActions = { type ClassStoreActions = {
_setClass: (classes: Class[]) => void; _setClass: (classes: Class[]) => void;
fetchClass: () => Promise<void>; fetchClass: () => Promise<Class[]>;
setSelectedClass: (selectedClass: Class | undefined | null) => void; setSelectedClass: (selectedClass: Class | undefined | null) => void;
}; };
@ -36,5 +36,6 @@ export const useClassStore = create<ClassStore>()((set) => ({
fetchClass: async () => { fetchClass: async () => {
const classResponse = await axiosInstance.get<Class[]>("/@me/class"); const classResponse = await axiosInstance.get<Class[]>("/@me/class");
useClassStore.getState()._setClass(classResponse.data); useClassStore.getState()._setClass(classResponse.data);
return classResponse.data;
}, },
})); }));

View File

@ -5,9 +5,9 @@ import { axiosInstance } from "../lib/axios";
import { useClassStore } from "./classStore"; import { useClassStore } from "./classStore";
type RoomStoreState = { type RoomStoreState = {
future: Room[]; future: Room[] | null;
actual: Room[]; actual: Room[] | null;
past: Room[]; past: Room[] | null;
}; };
type RoomStoreActions = { type RoomStoreActions = {
@ -18,9 +18,9 @@ type RoomStoreActions = {
type RoomStore = RoomStoreState & RoomStoreActions; type RoomStore = RoomStoreState & RoomStoreActions;
const defaultState: RoomStoreState = { const defaultState: RoomStoreState = {
future: [], future: null,
actual: [], actual: null,
past: [], past: null,
}; };
export const useRoomStore = create<RoomStore>()((set) => ({ export const useRoomStore = create<RoomStore>()((set) => ({