Skip to content

Commit

Permalink
Merge pull request #170 from makeopensource/62-edit-course-add/drop-s…
Browse files Browse the repository at this point in the history
…tudents

updated to use bulk add/drop students endpoints
  • Loading branch information
jessehartloff authored Nov 5, 2024
2 parents f5400cd + 2f67d94 commit 94b82f5
Showing 1 changed file with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand All @@ -213,18 +217,45 @@ 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) {
// if no file inputted then addSingleStudent with email
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)
}
}

Expand All @@ -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)
}
}

Expand Down

0 comments on commit 94b82f5

Please sign in to comment.