From 54c5b2063bf2d0a25c702c16bd62823709ed4b43 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Tue, 5 Nov 2024 09:43:26 +0000 Subject: [PATCH] ENCOA-216: Added a button to remove inactive assignees --- src/pages/api/assignments/[id]/index.ts | 2 +- src/pages/assignments/[id].tsx | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/pages/api/assignments/[id]/index.ts b/src/pages/api/assignments/[id]/index.ts index 11af9c71..b0fe5b8b 100644 --- a/src/pages/api/assignments/[id]/index.ts +++ b/src/pages/api/assignments/[id]/index.ts @@ -42,7 +42,7 @@ async function DELETE(req: NextApiRequest, res: NextApiResponse) { async function PATCH(req: NextApiRequest, res: NextApiResponse) { const {id} = req.query; - await db.collection("assignments").updateOne({id: id as string}, {$set: {assigner: req.session.user?.id, ...req.body}}); + await db.collection("assignments").updateOne({id: id as string}, {$set: {...req.body}}); res.status(200).json({ok: true}); } diff --git a/src/pages/assignments/[id].tsx b/src/pages/assignments/[id].tsx index 4af9a55d..6ce4e703 100644 --- a/src/pages/assignments/[id].tsx +++ b/src/pages/assignments/[id].tsx @@ -298,6 +298,25 @@ export default function AssignmentView({user, users, entity, assignment}: Props) return false; }; + const removeInactiveAssignees = () => { + const mappedResults = mapBy(assignment.results, 'user') + const inactiveAssignees = assignment.assignees.filter((a) => !mappedResults.includes(a)) + const activeAssignees = assignment.assignees.filter((a) => mappedResults.includes(a)) + + if (!confirm(`Are you sure you want to remove ${inactiveAssignees.length} assignees?`)) return + + axios + .patch(`/api/assignments/${assignment.id}`, {assignees: activeAssignees}) + .then(() => { + toast.success(`The assignment "${assignment.name}" has been updated successfully!`); + router.replace(router.asPath); + }) + .catch((e) => { + console.log(e); + toast.error("Something went wrong, please try again later!"); + }); + } + const copyLink = async () => { const origin = window.location.origin await navigator.clipboard.writeText(`${origin}/exam?assignment=${assignment.id}`) @@ -360,6 +379,11 @@ export default function AssignmentView({user, users, entity, assignment}: Props) Assigner: {getUserName(users.find((x) => x.id === assignment?.assigner))} + + {assignment.assignees.length !== 0 && assignment.results.length !== assignment.assignees.length && ( + + )} +
Average Scores