From de8742437f6963d9ddb84d33f8aabece51c5eca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= Date: Mon, 6 Jan 2025 11:21:05 +0100 Subject: [PATCH] feat: refactor room-related services and update API URLs for improved structure and clarity --- src/app/components/Room/Card.tsx | 2 +- src/app/components/Room/List.tsx | 2 +- src/app/constants/apiUrl.constant.ts | 5 ++++- .../{class.service.ts => classes.service.ts} | 2 +- src/app/services/rooms.service.ts | 7 +++++++ src/app/stores/classStore.ts | 4 ++-- src/app/stores/roomStore.ts | 13 +++++-------- .../{components/Room/Room.d.ts => types/room.d.ts} | 0 src/authOptions.ts | 2 +- 9 files changed, 22 insertions(+), 15 deletions(-) rename src/app/services/{class.service.ts => classes.service.ts} (80%) create mode 100644 src/app/services/rooms.service.ts rename src/app/{components/Room/Room.d.ts => types/room.d.ts} (100%) diff --git a/src/app/components/Room/Card.tsx b/src/app/components/Room/Card.tsx index 3ce2cd5..2f67fab 100644 --- a/src/app/components/Room/Card.tsx +++ b/src/app/components/Room/Card.tsx @@ -11,7 +11,7 @@ import { Divider, TimeInput, } from "@nextui-org/react"; -import { Room } from "./Room"; +import { Room } from "../../types/room"; import moment from "moment"; import { useRouter } from "next/navigation"; diff --git a/src/app/components/Room/List.tsx b/src/app/components/Room/List.tsx index 065a579..ca819c4 100644 --- a/src/app/components/Room/List.tsx +++ b/src/app/components/Room/List.tsx @@ -1,7 +1,7 @@ "use client"; import { useEffect, useRef } from "react"; import { RoomCard } from "./Card"; -import { Room } from "./Room"; +import { Room } from "../../types/room"; import { SkeletonRoomCard } from "./SkeletonRoomCard"; export const RoomList = ({ rooms }: { rooms: Room[] | null }) => { diff --git a/src/app/constants/apiUrl.constant.ts b/src/app/constants/apiUrl.constant.ts index 276433a..1c711c7 100644 --- a/src/app/constants/apiUrl.constant.ts +++ b/src/app/constants/apiUrl.constant.ts @@ -2,6 +2,9 @@ const NEXT_PUBLIC_API_URL = process.env.NEXT_PUBLIC_API_URL; export const API_URLS = { 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`, + } }; diff --git a/src/app/services/class.service.ts b/src/app/services/classes.service.ts similarity index 80% rename from src/app/services/class.service.ts rename to src/app/services/classes.service.ts index 312740a..cb99414 100644 --- a/src/app/services/class.service.ts +++ b/src/app/services/classes.service.ts @@ -3,4 +3,4 @@ import { axiosInstance } from "../lib/axios"; const getAll = () => axiosInstance.get(API_URLS.class.all); -export const classService = { getAll }; +export const classesService = { getAll }; diff --git a/src/app/services/rooms.service.ts b/src/app/services/rooms.service.ts new file mode 100644 index 0000000..b3822fb --- /dev/null +++ b/src/app/services/rooms.service.ts @@ -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(API_URLS.room.all(classId)); + +export const roomsService = { getAll }; diff --git a/src/app/stores/classStore.ts b/src/app/stores/classStore.ts index 99cc70c..8b5c228 100644 --- a/src/app/stores/classStore.ts +++ b/src/app/stores/classStore.ts @@ -1,5 +1,5 @@ import { create } from "zustand"; -import { classService } from "../services/class.service"; +import { classesService } from "../services/classes.service"; type ClassStoreState = { classes: Class[]; @@ -24,7 +24,7 @@ export const useClassStore = create()((set) => ({ })); }, fetchClass: async () => { - const classResponse = await classService.getAll(); + const classResponse = await classesService.getAll(); useClassStore.getState()._setClass(classResponse.data); return classResponse.data; }, diff --git a/src/app/stores/roomStore.ts b/src/app/stores/roomStore.ts index 249703f..5b7108a 100644 --- a/src/app/stores/roomStore.ts +++ b/src/app/stores/roomStore.ts @@ -1,8 +1,8 @@ import moment from "moment"; import { create } from "zustand"; -import { Room } from "../components/Room/Room"; -import { axiosInstance } from "../lib/axios"; +import { Room } from "../types/room"; import { useUserStore } from "./userStore"; +import { roomsService } from "../services/rooms.service"; type RoomStoreState = { future: Room[] | null; @@ -37,14 +37,11 @@ export const useRoomStore = create()((set) => ({ ); set({ future, actual, past }); }, - fetchRooms: () => { + fetchRooms: async () => { const selectedClassId = useUserStore.getState().currentClassId; if (!selectedClassId) return; - axiosInstance - .get(`/@me/class/${selectedClassId}/rooms`) - .then((classes) => { - useRoomStore.getState()._setRooms(classes.data); - }); + const roomResponse = await roomsService.getAll(selectedClassId); + useRoomStore.getState()._setRooms(roomResponse.data); }, })); diff --git a/src/app/components/Room/Room.d.ts b/src/app/types/room.d.ts similarity index 100% rename from src/app/components/Room/Room.d.ts rename to src/app/types/room.d.ts diff --git a/src/authOptions.ts b/src/authOptions.ts index 965f06f..f72143c 100644 --- a/src/authOptions.ts +++ b/src/authOptions.ts @@ -19,7 +19,7 @@ export const authOptions: AuthOptions = { authorization: { url: process.env.OAUTH_AUTHORIZATION_URL, params: { - scope: "openid email profile offline_access", + scope: "openid email profile", response_type: "code", }, },