diff --git a/frontend/frontend/src/components/SubmissionListItemTeacherPage.tsx b/frontend/frontend/src/components/SubmissionListItemTeacherPage.tsx index 3b5650c1..cd9b9c8a 100644 --- a/frontend/frontend/src/components/SubmissionListItemTeacherPage.tsx +++ b/frontend/frontend/src/components/SubmissionListItemTeacherPage.tsx @@ -3,6 +3,7 @@ import { ListItemButton, ListItemIcon, ListItemText, + Skeleton, } from '@mui/material' import { useNavigate } from 'react-router-dom' import DownloadIcon from '@mui/icons-material/Download' @@ -61,30 +62,40 @@ export function SubmissionListItemTeacherPage({ submissionsResponse.data[ submissionsResponse.data.length - 1 ] - const lastSubmissionResponse = await instance.get(`indieningen/${lastSubmission.indiening_id}/`) - //Get the submission file - const newSubmission: Submission = lastSubmissionResponse.data - newSubmission.filename = lastSubmissionResponse.data.bestand.replace( - /^.*[\\/]/, - '' - ) - newSubmission.bestand = await instance - .get(`/indieningen/${lastSubmission.indiening_id}/indiening_bestand`, { - responseType: 'blob', - }).then((res) => { - let filename = 'indiening.zip' - if (newSubmission.filename) { - filename = newSubmission.filename - } - const blob = new Blob([res.data], { - type: res.headers['content-type'], - }) - const file: File = new File([blob], filename, { - type: res.headers['content-type'], + if (lastSubmission) { + const lastSubmissionResponse = await instance.get( + `indieningen/${lastSubmission.indiening_id}/` + ) + //Get the submission file + const newSubmission: Submission = + lastSubmissionResponse.data + newSubmission.filename = + lastSubmissionResponse.data.bestand.replace( + /^.*[\\/]/, + '' + ) + newSubmission.bestand = await instance + .get( + `/indieningen/${lastSubmission.indiening_id}/indiening_bestand`, + { + responseType: 'blob', + } + ) + .then((res) => { + let filename = 'indiening.zip' + if (newSubmission.filename) { + filename = newSubmission.filename + } + const blob = new Blob([res.data], { + type: res.headers['content-type'], + }) + const file: File = new File([blob], filename, { + type: res.headers['content-type'], + }) + return file }) - return file - }) - setSubmitted(newSubmission) + setSubmitted(newSubmission) + } if (lastSubmission) { const scoreResponse = await instance.get( `/scores/?indiening=${lastSubmission.indiening_id}` @@ -105,9 +116,7 @@ export function SubmissionListItemTeacherPage({ const url = window.URL.createObjectURL(submitted?.bestand) const a = document.createElement('a') a.href = url - a.download = submitted.filename - ? submitted.filename - : 'opgave.zip' + a.download = submitted.filename ? submitted.filename : 'opgave.zip' document.body.appendChild(a) a.click() a.remove() @@ -128,6 +137,7 @@ export function SubmissionListItemTeacherPage({ <> {/* Display group id */} - + {!group_name ? ( + + ) : ( + + )} {/* Display submission timestamp */} { async function fetchUser() { setUserLoading(true) - setLoading(true) const userResponse = await instance.get('/gebruikers/me/') setUser(userResponse.data) setUserLoading(false) @@ -97,6 +97,7 @@ export function AssignmentPage() { async function fetchData() { try { + setLoading(true) const assignmentResponse = await instance.get( `/projecten/${assignmentId}/` ) @@ -133,21 +134,18 @@ export function AssignmentPage() { ) setGroups(groupsResponse.data) if (newAssignment.max_groep_grootte == 1) { - const temp_students = [] - for (const g of groupsResponse.data) { - try { - const userResponse = await instance.get( - `/gebruikers/${g.studenten[0]}/` + const userPromises: Promise>[] = + groupsResponse.data.map((group: Group) => + instance.get( + '/gebruikers/' + group.studenten[0] ) - temp_students.push(userResponse.data) - } catch (error) { - console.error( - 'Error fetching student data:', - error - ) - } - } - setSingleStudents(temp_students) + ) + + const temp_students = await axios.all(userPromises) + + setSingleStudents( + temp_students.map((res) => res.data) + ) } } else { const groupResponse = await instance.get<[Group]>( @@ -185,24 +183,24 @@ export function AssignmentPage() { `groepen/?student=${user.user}&project=${assignmentId}` ) const group: Group = groupResponse.data[0] - const temp_students = [] - for (const s of group.studenten || []) { - try { - const userResponse = await instance.get(`/gebruikers/${s}/`) - temp_students.push(userResponse.data) - } catch (error) { - console.error('Error fetching student data:', error) - } - } - // Update the state with the fetched data - setStudents(temp_students) + + const studentPromises: Promise>[] = + group.studenten.map((id: number) => + instance.get('/gebruikers/' + id) + ) + + const temp_students = await axios.all(studentPromises) + setStudents(temp_students.map((res) => res.data)) + setStudentsLoading(false) } // Fetch students - fetchStudents().catch((error) => - console.error('Error fetching students data:', error) - ) - }, [user, assignment, groups]) + if (!user.is_lesgever && user.user !== 0) { + fetchStudents().catch((error) => + console.error('Error fetching students data:', error) + ) + } + }, [user, assignment, groups, assignmentId]) // Function to download all submissions as a zip file const downloadAllSubmissions = () => {