refactor: simplify conditional check for class response before fetching rooms
This commit is contained in:
parent
e6d8a9ae9e
commit
c45579221b
@ -48,17 +48,17 @@ const HomePage = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
axiosInstance.get<{ id: string, name: string, createdAt: string }[]>("/@me/class")
|
axiosInstance.get<{ id: string, name: string, createdAt: string }[]>("/@me/class")
|
||||||
.then((classResponse) => {
|
.then((classResponse) => {
|
||||||
classResponse.data.length > 0 &&
|
if (classResponse.data.length)
|
||||||
axiosInstance.get<Room[]>(`/@me/class/${classResponse.data[0].id}/rooms`)
|
axiosInstance.get<Room[]>(`/@me/class/${classResponse.data[0].id}/rooms`)
|
||||||
.then(classes => {
|
.then(classes => {
|
||||||
// Filter rooms by date, get future, actual and past rooms
|
// Filter rooms by date, get future, actual and past rooms
|
||||||
const future = classes.data.filter(room => moment(room.date).isAfter(moment(), "day"));
|
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 actual = classes.data.filter(room => moment(room.date).isSame(moment(), "day"));
|
||||||
const past = classes.data.filter(room => moment(room.date).isBefore(moment()));
|
const past = classes.data.filter(room => moment(room.date).isBefore(moment()));
|
||||||
|
|
||||||
setRooms({ future, actual, past });
|
setRooms({ future, actual, past });
|
||||||
setRoomsLoading(false);
|
setRoomsLoading(false);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
}, []);
|
}, []);
|
||||||
|
Loading…
Reference in New Issue
Block a user