From 2f67d947e5b65284b8c9c146fa14b765d670c0ab Mon Sep 17 00:00:00 2001 From: NeemZ16 <110858265+NeemZ16@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:08:46 -0400 Subject: [PATCH] updated to use bulk add/drop students endpoints --- .../pages/forms/courses/courseUpdatePage.tsx | 48 +++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/devU-client/src/components/pages/forms/courses/courseUpdatePage.tsx b/devU-client/src/components/pages/forms/courses/courseUpdatePage.tsx index 065df76..40fbd82 100644 --- a/devU-client/src/components/pages/forms/courses/courseUpdatePage.tsx +++ b/devU-client/src/components/pages/forms/courses/courseUpdatePage.tsx @@ -202,7 +202,11 @@ const CourseUpdatePage = ({ }) => { const dropSingleStudent = async (email: string) => { const userID = await getUserId(email) - if (!userID) { return } + + if (userID == 0) { + setAlert({ autoDelete: false, type: 'error', message: "userID not found" }) + return + } try { await RequestService.delete(`/api/course/${courseId}/user-courses/${userID}`) @@ -213,6 +217,32 @@ const CourseUpdatePage = ({ }) => { } } + const addBulkStudent = async (emails: string[]) => { + try { + const reqBody = { + users: emails + } + const res = await RequestService.post(`/api/course/${courseId}/user-courses/students/add`, reqBody) + setAlert({ autoDelete: true, type: 'success', message: res.success }) + } catch (error: any) { // Use any if the error type isn't strictly defined + const message = error.message || "An unknown error occurred" + setAlert({ autoDelete: false, type: 'error', message }) + } + } + + const dropBulkStudent = async (emails: string[]) => { + try { + const reqBody = { + users: emails + } + const res = await RequestService.post(`/api/course/${courseId}/user-courses/students/drop`, reqBody) + setAlert({ autoDelete: true, type: 'success', message: res.success }) + } catch (error: any) { // Use any if the error type isn't strictly defined + const message = error.message || "An unknown error occurred" + setAlert({ autoDelete: false, type: 'error', message }) + } + } + const handleAddStudent = () => { console.log("emails: ", emails); if (emails.length<1) { @@ -220,11 +250,12 @@ const CourseUpdatePage = ({ }) => { console.log("adding single user") addSingleStudent(studentEmail) } else { - // if file inputted then for each email parsed from csv addSingleStudent + // if file inputted then call console.log("adding multiple users") - emails.forEach(email => { - addSingleStudent(email) - }) + // emails.forEach(email => { + // addSingleStudent(email) + // }) + addBulkStudent(emails) } } @@ -236,9 +267,10 @@ const CourseUpdatePage = ({ }) => { } else { // if file inputted then for each email parsed from csv dropSingleStudent console.log("dropping multiple users") - emails.forEach(email => { - dropSingleStudent(email) - }) + // emails.forEach(email => { + // dropSingleStudent(email) + // }) + dropBulkStudent(emails) } }