diff --git a/src/hooks/useGroups.tsx b/src/hooks/useGroups.tsx index a4d39dc7..9b4db75a 100644 --- a/src/hooks/useGroups.tsx +++ b/src/hooks/useGroups.tsx @@ -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([]); 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(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}; } diff --git a/src/hooks/usePermissions.tsx b/src/hooks/usePermissions.tsx index 67d49394..5fdf962e 100644 --- a/src/hooks/usePermissions.tsx +++ b/src/hooks/usePermissions.tsx @@ -17,7 +17,6 @@ export default function usePermissions(user: string) { const permissionTypes = response.data .filter((x) => !x.users.includes(user)) .reduce((acc, curr) => [...acc, curr.type], [] as PermissionType[]); - console.log(response.data, permissionTypes); setPermissions(permissionTypes); }) .finally(() => setIsLoading(false)); diff --git a/src/pages/(admin)/BatchCreateUser.tsx b/src/pages/(admin)/BatchCreateUser.tsx index 43bd8ec8..d0d50c57 100644 --- a/src/pages/(admin)/BatchCreateUser.tsx +++ b/src/pages/(admin)/BatchCreateUser.tsx @@ -111,7 +111,6 @@ export default function BatchCreateUser({user}: {user: User}) { return clear(); } - console.log(information); setInfos(information); } catch { toast.error( diff --git a/src/pages/(admin)/Lists/GroupList.tsx b/src/pages/(admin)/Lists/GroupList.tsx index 4db19d47..3ac5be76 100644 --- a/src/pages/(admin)/Lists/GroupList.tsx +++ b/src/pages/(admin)/Lists/GroupList.tsx @@ -201,7 +201,11 @@ export default function GroupList({user}: {user: User}) { const {permissions} = usePermissions(user?.id || ""); 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(() => { if (user && ["corporate", "teacher", "mastercorporate"].includes(user.type)) { diff --git a/src/pages/settings.tsx b/src/pages/settings.tsx index a744b2a0..80c08fbb 100644 --- a/src/pages/settings.tsx +++ b/src/pages/settings.tsx @@ -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 { redirect: { destination: "/",