Updated the pagination on the useUsers and migrading the grading
This commit is contained in:
@@ -58,13 +58,18 @@ export default function UserList({
|
||||
const [sorter, setSorter] = useState<string>();
|
||||
const [displayUsers, setDisplayUsers] = useState<User[]>([]);
|
||||
const [selectedUser, setSelectedUser] = useState<User>();
|
||||
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({
|
||||
</Button>
|
||||
</div>
|
||||
<div className="w-full flex gap-2 justify-between">
|
||||
<Button className="w-full max-w-[200px]" disabled={page === 0} onClick={previous}>
|
||||
<Button className="w-full max-w-[200px]" disabled={page === 0} onClick={() => setPage((prev) => prev - 1)}>
|
||||
Previous Page
|
||||
</Button>
|
||||
<Button className="w-full max-w-[200px]" disabled={page * 25 >= total} onClick={next}>
|
||||
<Button className="w-full max-w-[200px]" disabled={page * 25 >= total} onClick={() => setPage((prev) => prev + 1)}>
|
||||
Next Page
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
@@ -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});
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user