Skip to content

Commit

Permalink
Merge pull request #283 from SELab-2/assignment_performance
Browse files Browse the repository at this point in the history
performance inladen studenten verbeterd
  • Loading branch information
Bendemeurichy authored May 19, 2024
2 parents 1a8eda5 + c209b7d commit f17ccc5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 66 deletions.
90 changes: 54 additions & 36 deletions frontend/frontend/src/components/SubmissionListItemTeacherPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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}`
Expand All @@ -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()
Expand All @@ -128,6 +137,7 @@ export function SubmissionListItemTeacherPage({
<>
<ListItem id={group_id} sx={{ margin: 0 }} disablePadding={true}>
<ListItemButton
disabled={!submitted}
sx={{
width: '100%',
height: 40,
Expand All @@ -140,16 +150,24 @@ export function SubmissionListItemTeacherPage({
onClick={handleSubmissionClick}
>
{/* Display group id */}
<ListItemText
sx={{
maxWidth: '24%',
color: 'primary.main',
'&:hover': {
color: 'primary.light',
},
}}
primary={group_name}
/>
{!group_name ? (
<Skeleton
variant="text"
sx={{ minWidth: '24%' }}
height={30}
/>
) : (
<ListItemText
sx={{
maxWidth: '24%',
color: 'primary.main',
'&:hover': {
color: 'primary.light',
},
}}
primary={group_name}
/>
)}
{/* Display submission timestamp */}
<ListItemText
sx={{ minWidth: '24%' }}
Expand Down
58 changes: 28 additions & 30 deletions frontend/frontend/src/pages/assignmentPage/AssignmentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import DownloadIcon from '@mui/icons-material/Download'
import WarningPopup from '../../components/WarningPopup.tsx'
import { User } from '../subjectsPage/AddChangeSubjectPage.tsx'
import StudentPopUp from '../subjectsPage/StudentPopUp.tsx'
import axios, { AxiosResponse } from 'axios'

// group interface
export interface Group {
Expand Down Expand Up @@ -89,14 +90,14 @@ export function AssignmentPage() {
useEffect(() => {
async function fetchUser() {
setUserLoading(true)
setLoading(true)
const userResponse = await instance.get('/gebruikers/me/')
setUser(userResponse.data)
setUserLoading(false)
}

async function fetchData() {
try {
setLoading(true)
const assignmentResponse = await instance.get(
`/projecten/${assignmentId}/`
)
Expand Down Expand Up @@ -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<AxiosResponse<User>>[] =
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]>(
Expand Down Expand Up @@ -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<AxiosResponse<User>>[] =
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 = () => {
Expand Down

0 comments on commit f17ccc5

Please sign in to comment.