Skip to content

Commit

Permalink
veranderingen opgave_bestand
Browse files Browse the repository at this point in the history
  • Loading branch information
Bendemeurichy committed May 23, 2024
1 parent c572ff5 commit 37d9529
Showing 1 changed file with 99 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function AddChangeAssignmentPage() {
const [assignmentFile, setAssignmentFile] = useState<File>()
const [maxScore, SetMaxScore] = useState<number>(20)
const [cleared, setCleared] = useState<boolean>(false)
const [filename, setFilename] = useState<string>('indiening.zip')
const [filename, setFilename] = useState<string>('')
const [groupSize, setGroupSize] = useState<number>(1)

const [user, setUser] = useState<User>()
Expand Down Expand Up @@ -191,7 +191,9 @@ export function AddChangeAssignmentPage() {
setDescription(assignment.beschrijving)

console.log('bestand' + assignment.opgave_bestand)
setFilename(() => assignment.opgave_bestand)
if (assignment.opgave_bestand !== null) {
setFilename(() => assignment.opgave_bestand)
}
SetMaxScore(assignment.max_score)
console.log('max score' + assignment.max_score)

Expand Down Expand Up @@ -245,23 +247,25 @@ export function AddChangeAssignmentPage() {
})

//get the assignment file
await instance
.get(`/projecten/${assignmentId}/opgave_bestand/`, {
responseType: 'blob',
})
.then((response) => {
const blob = new Blob([response.data], {
type: response.headers['content-type'],
})
const file: File = new File([blob], filename, {
type: response.headers['content-type'],
if (filename !== '') {
await instance
.get(`/projecten/${assignmentId}/opgave_bestand/`, {
responseType: 'blob',
})
.then((response) => {
const blob = new Blob([response.data], {
type: response.headers['content-type'],
})
const file: File = new File([blob], filename, {
type: response.headers['content-type'],
})

setAssignmentFile(file)
})
.catch((error) => {
console.error(error)
})
setAssignmentFile(file)
})
.catch((error) => {
console.error(error)
})
}

//end loading -> set loading to false
setLoading(false)
Expand Down Expand Up @@ -307,7 +311,11 @@ export function AddChangeAssignmentPage() {
if (dueDate === null && extraDueDate === null) {
setDeadlineCheck(false)
} else if (dueDate !== null && extraDueDate !== null) {
setDeadlineCheck(extraDueDate.diff(dueDate) < 0)
if (dayjs(extraDueDate).isSame(dayjs(dueDate))) {
setDeadlineCheck(true)
} else {
setDeadlineCheck(dayjs(extraDueDate).isBefore(dayjs(dueDate)))
}
} else if (dueDate !== null && extraDueDate === null) {
setDeadlineCheck(false)
} else {
Expand Down Expand Up @@ -383,89 +391,86 @@ export function AddChangeAssignmentPage() {
})
}

// Upload the assignment to the API. patch if it is an edit, post if it is a new assignment.
// Upload the assignment to the API. put if it is an edit, post if it is a new assignment.
const uploadAssignment = async () => {
let optionalFile: File | null
const formData = new FormData()

if (assignmentFile !== undefined) {
optionalFile = assignmentFile
} else {
optionalFile = null
}

formData.append('titel', title)
formData.append('beschrijving', description)
formData.append('vak', parseInt(courseId as string).toString())
if (optionalFile) {
console.log('optional file' + optionalFile.name)
formData.append('opgave_bestand', optionalFile)
} else {
formData.append('opgave_bestand', '')
}
formData.append('zichtbaar', visible.toString())

// Add optional fields
if (maxScore !== 20) {
formData.append('max_score', maxScore.toString())
}
if (dueDate !== null) {
formData.append('deadline', dueDate.format())
}
if (extraDueDate !== null) {
formData.append('extra_deadline', extraDueDate.format())
}
formData.append('max_groep_grootte', groupSize.toString())
try {
const config = {
headers: {
'Content-Type': 'multipart/form-data',
},
}
// editing an existing assignment: need to put
if (assignmentId) {
const formData = new FormData()
formData.append('project_id', assignmentId)
if (assignmentFile) {
formData.append('opgave_bestand', assignmentFile)
}
formData.append('titel', title)
formData.append('beschrijving', description)
if (courseId) {
formData.append('vak', courseId)
}
if (maxScore !== 20) {
formData.append('max_score', maxScore.toString())
}
if (dueDate) {
formData.append('deadline', dueDate.format())
}
if (extraDueDate) {
formData.append('extra_deadline', extraDueDate.format())
}
formData.append('zichtbaar', visible.toString())
formData.append('max_groep_grootte', groupSize.toString())

const config = {
headers: {
'Content-Type': 'multipart/form-data',
},
}
if (assignmentId !== undefined) {
formData.append('project_id', assignmentId)
console.log('file', formData.get('opgave_bestand'))
await instance
.patch(
'/projecten/' + parseInt(assignmentId) + '/',
// Send the data to the API
await instance.put(
`/projecten/${assignmentId}/`,
formData,
config
)
.catch((error) => {
console.error(error)
})

//upload the restrictions
handleRestrictionUpload(assignmentId.toString())
} else {
//if there is no assignmentId, it is a new assignment
let project_id: number = 0

await instance
.post('/projecten/', formData, config)
.then((response) => (project_id = response.data.project_id))
.catch((error) => {
console.error(error)
})
handleRestrictionUpload(assignmentId.toString())
} else {
// Creating a new assignment: need to post
const formData = new FormData()
if (assignmentFile) {
formData.append('opgave_bestand', assignmentFile)
}
formData.append('titel', title)
formData.append('beschrijving', description)
if (courseId) {
formData.append('vak', courseId)
}
if (maxScore !== 20) {
formData.append('max_score', maxScore.toString())
}
if (dueDate) {
formData.append('deadline', dueDate.format())
}
if (extraDueDate) {
formData.append('extra_deadline', extraDueDate.format())
}
formData.append('zichtbaar', visible.toString())
formData.append('max_groep_grootte', groupSize.toString())

//upload the restrictions
handleRestrictionUpload(project_id.toString())
}
// Send the data to the API
const project = await instance.post(
'/projecten/',
formData,
config
)
handleRestrictionUpload(project.data.id)
}

console.info(
'Form submitted',
title,
description,
dueDate,
restrictions,
visible,
assignmentFile
)
setSaveConfirmation(false)
if (assignmentId !== undefined) {
navigate('/course/' + courseId + '/assignment/' + assignmentId)
} else {
navigate('/course/' + courseId)
if (assignmentId !== undefined) {
navigate('/course/' + courseId + '/assignment/' + assignmentId)
} else {
navigate('/course/' + courseId)
}
} catch (error) {
console.error(error)
} finally {
setSaveConfirmation(false)
}
}

Expand Down

0 comments on commit 37d9529

Please sign in to comment.