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 axios from "axios";
|
||||||
import {useEffect, useState} from "react";
|
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 [groups, setGroups] = useState<Group[]>([]);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [isError, setIsError] = useState(false);
|
const [isError, setIsError] = useState(false);
|
||||||
|
|
||||||
const isMasterType = userType?.startsWith('master');
|
const isMasterType = userType?.startsWith("master");
|
||||||
|
|
||||||
const getData = () => {
|
const getData = () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
const url = admin ? `/api/groups?admin=${admin}` : "/api/groups";
|
const url = admin && !teacher ? `/api/groups?admin=${admin}` : "/api/groups";
|
||||||
axios
|
axios
|
||||||
.get<Group[]>(url)
|
.get<Group[]>(url)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if(isMasterType) {
|
if (isMasterType) return setGroups(response.data);
|
||||||
return setGroups(response.data);
|
|
||||||
}
|
|
||||||
const filter = (g: Group) => g.admin === admin || g.participants.includes(admin || "");
|
|
||||||
|
|
||||||
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);
|
return setGroups(admin ? filteredGroups.map((g) => ({...g, disableEditing: g.disableEditing || g.admin !== admin})) : filteredGroups);
|
||||||
})
|
})
|
||||||
.finally(() => setIsLoading(false));
|
.finally(() => setIsLoading(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(getData, [admin, isMasterType]);
|
useEffect(getData, [admin, teacher, isMasterType]);
|
||||||
|
|
||||||
return {groups, isLoading, isError, reload: getData};
|
return {groups, isLoading, isError, reload: getData};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ export default function usePermissions(user: string) {
|
|||||||
const permissionTypes = response.data
|
const permissionTypes = response.data
|
||||||
.filter((x) => !x.users.includes(user))
|
.filter((x) => !x.users.includes(user))
|
||||||
.reduce((acc, curr) => [...acc, curr.type], [] as PermissionType[]);
|
.reduce((acc, curr) => [...acc, curr.type], [] as PermissionType[]);
|
||||||
console.log(response.data, permissionTypes);
|
|
||||||
setPermissions(permissionTypes);
|
setPermissions(permissionTypes);
|
||||||
})
|
})
|
||||||
.finally(() => setIsLoading(false));
|
.finally(() => setIsLoading(false));
|
||||||
|
|||||||
@@ -111,7 +111,6 @@ export default function BatchCreateUser({user}: {user: User}) {
|
|||||||
return clear();
|
return clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(information);
|
|
||||||
setInfos(information);
|
setInfos(information);
|
||||||
} catch {
|
} catch {
|
||||||
toast.error(
|
toast.error(
|
||||||
|
|||||||
@@ -201,7 +201,11 @@ export default function GroupList({user}: {user: User}) {
|
|||||||
const {permissions} = usePermissions(user?.id || "");
|
const {permissions} = usePermissions(user?.id || "");
|
||||||
|
|
||||||
const {users} = useUsers();
|
const {users} = useUsers();
|
||||||
const {groups, reload} = useGroups(user && filterTypes.includes(user?.type) ? user.id : undefined, user?.type);
|
const {groups, reload} = useGroups(
|
||||||
|
user && filterTypes.includes(user?.type) ? user.id : undefined,
|
||||||
|
user?.type,
|
||||||
|
user?.type === "teacher" ? user?.id : undefined,
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (user && ["corporate", "teacher", "mastercorporate"].includes(user.type)) {
|
if (user && ["corporate", "teacher", "mastercorporate"].includes(user.type)) {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldRedirectHome(user) || !["developer", "admin", "corporate", "agent", "mastercorporate"].includes(user.type)) {
|
if (shouldRedirectHome(user) || !checkAccess(user, ["admin", "developer", "corporate", "teacher", "mastercorporate"])) {
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
destination: "/",
|
destination: "/",
|
||||||
|
|||||||
Reference in New Issue
Block a user