From 56f374bbfe2b333020319129fc30e0b35aff1839 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Sat, 7 Sep 2024 16:53:58 +0100 Subject: [PATCH] Updated the pagination on the useUsers and migrading the grading --- src/hooks/useUsers.tsx | 31 +++++++--------------------- src/pages/(admin)/Lists/UserList.tsx | 19 ++++++++++------- src/pages/api/grading/index.ts | 19 ++++++++++++----- src/pages/api/users/list.ts | 6 +++--- src/utils/users.be.ts | 11 ++++++++-- 5 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/hooks/useUsers.tsx b/src/hooks/useUsers.tsx index ff700f39..64fd920e 100644 --- a/src/hooks/useUsers.tsx +++ b/src/hooks/useUsers.tsx @@ -5,29 +5,25 @@ import {setupCache} from "axios-cache-interceptor"; const instance = Axios.create(); const axios = setupCache(instance); -export const userHashStudent = { type: "student" } as { type: Type }; -export const userHashTeacher = { type: "teacher" } as { type: Type }; -export const userHashCorporate = { type: "corporate" } as { type: Type }; +export const userHashStudent = {type: "student"} as {type: Type}; +export const userHashTeacher = {type: "teacher"} as {type: Type}; +export const userHashCorporate = {type: "corporate"} as {type: Type}; export default function useUsers(props?: {type?: Type; page?: number; size?: number}) { const [users, setUsers] = useState([]); const [total, setTotal] = useState(0); const [isLoading, setIsLoading] = useState(false); const [isError, setIsError] = useState(false); - const [latestID, setLatestID] = useState(); - const [firstID, setFirstID] = useState(); - const [page, setPage] = useState(0); const getData = () => { const params = new URLSearchParams(); if (!!props) Object.keys(props).forEach((key) => { - if (!!props[key as keyof typeof props]) params.append(key, props[key as keyof typeof props]!.toString()); + if (props[key as keyof typeof props] !== undefined) params.append(key, props[key as keyof typeof props]!.toString()); }); - if (!!latestID) params.append("latestID", latestID); - if (!!firstID) params.append("firstID", firstID); + console.log(params.toString()); setIsLoading(true); axios @@ -39,20 +35,7 @@ export default function useUsers(props?: {type?: Type; page?: number; size?: num .finally(() => setIsLoading(false)); }; - const next = () => { - setLatestID(users[users.length - 1]?.id); - setFirstID(undefined); - setPage((prev) => prev + 1); - }; + useEffect(getData, [props?.page, props?.size, props?.type]); - const previous = () => { - setLatestID(undefined); - setFirstID(page > 1 ? users[0]?.id : undefined); - setPage((prev) => prev - 1); - }; - - // eslint-disable-next-line react-hooks/exhaustive-deps - useEffect(getData, [props, latestID, firstID]); - - return {users, total, page, isLoading, isError, reload: getData, next, previous}; + return {users, total, isLoading, isError, reload: getData}; } diff --git a/src/pages/(admin)/Lists/UserList.tsx b/src/pages/(admin)/Lists/UserList.tsx index 10bc63ad..6a8a9ad7 100644 --- a/src/pages/(admin)/Lists/UserList.tsx +++ b/src/pages/(admin)/Lists/UserList.tsx @@ -58,13 +58,18 @@ export default function UserList({ const [sorter, setSorter] = useState(); const [displayUsers, setDisplayUsers] = useState([]); const [selectedUser, setSelectedUser] = useState(); + const [page, setPage] = useState(0); - const userHash = useMemo(() => ({ - type, - size: 25, - }), [type]) + const userHash = useMemo( + () => ({ + type, + size: 25, + page, + }), + [type, page], + ); - const {users, page, total, reload, next, previous} = useUsers(userHash); + const {users, total, reload} = useUsers(userHash); const {permissions} = usePermissions(user?.id || ""); const {balance} = useUserBalance(); const {groups} = useGroups({ @@ -620,10 +625,10 @@ export default function UserList({
- -
diff --git a/src/pages/api/grading/index.ts b/src/pages/api/grading/index.ts index 8b84f55a..9304a24e 100644 --- a/src/pages/api/grading/index.ts +++ b/src/pages/api/grading/index.ts @@ -14,10 +14,11 @@ import {getUserCorporate} from "@/utils/groups.be"; import {Grading} from "@/interfaces"; import {getGroupsForUser} from "@/utils/groups.be"; import {uniq} from "lodash"; -import {getUser} from "@/utils/users.be"; +import {getSpecificUsers, getUser} from "@/utils/users.be"; import {getGradingSystem} from "@/utils/grading.be"; +import client from "@/lib/mongodb"; -const db = getFirestore(app); +const db = client.db(process.env.MONGODB_DB); export default withIronSessionApiRoute(handler, sessionOptions); @@ -36,6 +37,14 @@ async function get(req: NextApiRequest, res: NextApiResponse) { return res.status(200).json(gradingSystem); } +async function updateGrading(id: string, body: Grading) { + if (await db.collection("grading").findOne({id})) { + await db.collection("grading").updateOne({id}, {$set: body}); + } else { + await db.collection("grading").insertOne({id, ...body}); + } +} + async function post(req: NextApiRequest, res: NextApiResponse) { if (!req.session.user) { res.status(401).json({ok: false}); @@ -49,16 +58,16 @@ async function post(req: NextApiRequest, res: NextApiResponse) { }); const body = req.body as Grading; - await setDoc(doc(db, "grading", req.session.user.id), body); + await updateGrading(req.session.user.id, body); if (req.session.user.type === "mastercorporate") { const groups = await getGroupsForUser(req.session.user.id); const participants = uniq(groups.flatMap((x) => x.participants)); - const participantUsers = await Promise.all(participants.map(getUser)); + const participantUsers = await getSpecificUsers(participants); const corporateUsers = participantUsers.filter((x) => x?.type === "corporate") as CorporateUser[]; - await Promise.all(corporateUsers.map(async (g) => await setDoc(doc(db, "grading", g.id), body))); + await Promise.all(corporateUsers.map(async (g) => await updateGrading(g.id, body))); } res.status(200).json({ok: true}); diff --git a/src/pages/api/users/list.ts b/src/pages/api/users/list.ts index 852ca429..d0a39c3f 100644 --- a/src/pages/api/users/list.ts +++ b/src/pages/api/users/list.ts @@ -13,14 +13,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { return; } - const {size, type, latestID, firstID} = req.query as {size?: string; type?: Type; latestID?: string; firstID?: string}; + const {size, type, page} = req.query as {size?: string; type?: Type; page?: string}; + console.log(size, type, page); const {users, total} = await getLinkedUsers( req.session.user?.id, req.session.user?.type, type, - firstID, - latestID, + page !== undefined ? parseInt(page) : undefined, size !== undefined ? parseInt(size) : undefined, ); diff --git a/src/utils/users.be.ts b/src/utils/users.be.ts index ea701a52..c85144b6 100644 --- a/src/utils/users.be.ts +++ b/src/utils/users.be.ts @@ -44,13 +44,18 @@ export async function getSpecificUsers(ids: string[]) { .toArray(); } -export async function getLinkedUsers(userID?: string, userType?: Type, type?: Type, firstID?: string, lastID?: string, size?: number) { +export async function getLinkedUsers(userID?: string, userType?: Type, type?: Type, page?: number, size?: number) { const filters = { ...(!!type ? {type} : {}), }; if (!userID || userType === "admin" || userType === "developer") { - const users = await db.collection("users").find(filters).toArray(); + const users = await db + .collection("users") + .find(filters) + .skip(page && size ? page * size : 0) + .limit(size || 0) + .toArray(); const total = await db.collection("users").countDocuments(filters); return {users, total}; } @@ -71,6 +76,8 @@ export async function getLinkedUsers(userID?: string, userType?: Type, type?: Ty const users = await db .collection("users") .find({...filters, id: {$in: participants}}) + .skip(page && size ? page * size : 0) + .limit(size || 0) .toArray(); const total = await db.collection("users").countDocuments({...filters, id: {$in: participants}});