Updated the Groups List to allow teachers to view their corporate's students
This commit is contained in:
@@ -2,32 +2,34 @@ import {Group, User} from "@/interfaces/user";
|
||||
import axios from "axios";
|
||||
import {useEffect, useState} from "react";
|
||||
|
||||
export default function useGroups(admin?: string, userType?: string) {
|
||||
export default function useGroups(admin?: string, userType?: string, teacher?: string) {
|
||||
const [groups, setGroups] = useState<Group[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isError, setIsError] = useState(false);
|
||||
|
||||
const isMasterType = userType?.startsWith('master');
|
||||
const isMasterType = userType?.startsWith("master");
|
||||
|
||||
const getData = () => {
|
||||
setIsLoading(true);
|
||||
|
||||
const url = admin ? `/api/groups?admin=${admin}` : "/api/groups";
|
||||
const url = admin && !teacher ? `/api/groups?admin=${admin}` : "/api/groups";
|
||||
axios
|
||||
.get<Group[]>(url)
|
||||
.then((response) => {
|
||||
if(isMasterType) {
|
||||
return setGroups(response.data);
|
||||
}
|
||||
const filter = (g: Group) => g.admin === admin || g.participants.includes(admin || "");
|
||||
if (isMasterType) return setGroups(response.data);
|
||||
|
||||
const filteredGroups = admin ? response.data.filter(filter) : response.data;
|
||||
const filterByAdmins = !!teacher
|
||||
? [teacher, ...response.data.filter((g) => g.participants.includes(teacher)).flatMap((g) => g.admin)]
|
||||
: [admin];
|
||||
const filter = (g: Group) => filterByAdmins.includes(g.admin) || g.participants.includes(admin || "");
|
||||
|
||||
const filteredGroups = !!admin || !!teacher ? response.data.filter(filter) : response.data;
|
||||
return setGroups(admin ? filteredGroups.map((g) => ({...g, disableEditing: g.disableEditing || g.admin !== admin})) : filteredGroups);
|
||||
})
|
||||
.finally(() => setIsLoading(false));
|
||||
};
|
||||
|
||||
useEffect(getData, [admin, isMasterType]);
|
||||
useEffect(getData, [admin, teacher, isMasterType]);
|
||||
|
||||
return {groups, isLoading, isError, reload: getData};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user