refactor: simplify conditional check for class response before fetching rooms

This commit is contained in:
Rémi 2025-01-02 19:30:50 +01:00
parent e6d8a9ae9e
commit c45579221b

View File

@ -48,17 +48,17 @@ const HomePage = () => {
useEffect(() => {
axiosInstance.get<{ id: string, name: string, createdAt: string }[]>("/@me/class")
.then((classResponse) => {
classResponse.data.length > 0 &&
axiosInstance.get<Room[]>(`/@me/class/${classResponse.data[0].id}/rooms`)
.then(classes => {
// Filter rooms by date, get future, actual and past rooms
const future = classes.data.filter(room => moment(room.date).isAfter(moment(), "day"));
const actual = classes.data.filter(room => moment(room.date).isSame(moment(), "day"));
const past = classes.data.filter(room => moment(room.date).isBefore(moment()));
if (classResponse.data.length)
axiosInstance.get<Room[]>(`/@me/class/${classResponse.data[0].id}/rooms`)
.then(classes => {
// Filter rooms by date, get future, actual and past rooms
const future = classes.data.filter(room => moment(room.date).isAfter(moment(), "day"));
const actual = classes.data.filter(room => moment(room.date).isSame(moment(), "day"));
const past = classes.data.filter(room => moment(room.date).isBefore(moment()));
setRooms({ future, actual, past });
setRoomsLoading(false);
});
setRooms({ future, actual, past });
setRoomsLoading(false);
});
})
}, []);