feat: refactor room-related services and update API URLs for improved structure and clarity

This commit is contained in:
Rémi 2025-01-06 11:21:05 +01:00
parent dc858227fa
commit de8742437f
9 changed files with 22 additions and 15 deletions

View File

@ -11,7 +11,7 @@ import {
Divider, Divider,
TimeInput, TimeInput,
} from "@nextui-org/react"; } from "@nextui-org/react";
import { Room } from "./Room"; import { Room } from "../../types/room";
import moment from "moment"; import moment from "moment";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";

View File

@ -1,7 +1,7 @@
"use client"; "use client";
import { useEffect, useRef } from "react"; import { useEffect, useRef } from "react";
import { RoomCard } from "./Card"; import { RoomCard } from "./Card";
import { Room } from "./Room"; import { Room } from "../../types/room";
import { SkeletonRoomCard } from "./SkeletonRoomCard"; import { SkeletonRoomCard } from "./SkeletonRoomCard";
export const RoomList = ({ rooms }: { rooms: Room[] | null }) => { export const RoomList = ({ rooms }: { rooms: Room[] | null }) => {

View File

@ -2,6 +2,9 @@ const NEXT_PUBLIC_API_URL = process.env.NEXT_PUBLIC_API_URL;
export const API_URLS = { export const API_URLS = {
class: { class: {
all: `${NEXT_PUBLIC_API_URL}/class`, all: `${NEXT_PUBLIC_API_URL}/@me/class`,
}, },
room: {
all: (classId: string) => `${NEXT_PUBLIC_API_URL}/@me/class/${classId}/rooms`,
}
}; };

View File

@ -3,4 +3,4 @@ import { axiosInstance } from "../lib/axios";
const getAll = () => axiosInstance.get<Class[]>(API_URLS.class.all); const getAll = () => axiosInstance.get<Class[]>(API_URLS.class.all);
export const classService = { getAll }; export const classesService = { getAll };

View File

@ -0,0 +1,7 @@
import { API_URLS } from "../constants/apiUrl.constant";
import { axiosInstance } from "../lib/axios";
import { Room } from "../types/room";
const getAll = (classId: string) => axiosInstance.get<Room[]>(API_URLS.room.all(classId));
export const roomsService = { getAll };

View File

@ -1,5 +1,5 @@
import { create } from "zustand"; import { create } from "zustand";
import { classService } from "../services/class.service"; import { classesService } from "../services/classes.service";
type ClassStoreState = { type ClassStoreState = {
classes: Class[]; classes: Class[];
@ -24,7 +24,7 @@ export const useClassStore = create<ClassStore>()((set) => ({
})); }));
}, },
fetchClass: async () => { fetchClass: async () => {
const classResponse = await classService.getAll(); const classResponse = await classesService.getAll();
useClassStore.getState()._setClass(classResponse.data); useClassStore.getState()._setClass(classResponse.data);
return classResponse.data; return classResponse.data;
}, },

View File

@ -1,8 +1,8 @@
import moment from "moment"; import moment from "moment";
import { create } from "zustand"; import { create } from "zustand";
import { Room } from "../components/Room/Room"; import { Room } from "../types/room";
import { axiosInstance } from "../lib/axios";
import { useUserStore } from "./userStore"; import { useUserStore } from "./userStore";
import { roomsService } from "../services/rooms.service";
type RoomStoreState = { type RoomStoreState = {
future: Room[] | null; future: Room[] | null;
@ -37,14 +37,11 @@ export const useRoomStore = create<RoomStore>()((set) => ({
); );
set({ future, actual, past }); set({ future, actual, past });
}, },
fetchRooms: () => { fetchRooms: async () => {
const selectedClassId = useUserStore.getState().currentClassId; const selectedClassId = useUserStore.getState().currentClassId;
if (!selectedClassId) return; if (!selectedClassId) return;
axiosInstance const roomResponse = await roomsService.getAll(selectedClassId);
.get<Room[]>(`/@me/class/${selectedClassId}/rooms`) useRoomStore.getState()._setRooms(roomResponse.data);
.then((classes) => {
useRoomStore.getState()._setRooms(classes.data);
});
}, },
})); }));

View File

@ -19,7 +19,7 @@ export const authOptions: AuthOptions = {
authorization: { authorization: {
url: process.env.OAUTH_AUTHORIZATION_URL, url: process.env.OAUTH_AUTHORIZATION_URL,
params: { params: {
scope: "openid email profile offline_access", scope: "openid email profile",
response_type: "code", response_type: "code",
}, },
}, },