From 75ac9595cf7072d755a99702c56d1c6709c78268 Mon Sep 17 00:00:00 2001 From: ublefo <90136978+ublefo@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:46:53 +1100 Subject: [PATCH] feat: implement numbas test data upload in task definition service renamed api endpoint to reduce confusion between components --- src/app/api/models/task-definition.ts | 4 +-- .../api/services/task-definition.service.ts | 6 +++++ .../task-definition-numbas.component.ts | 26 ++++--------------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/app/api/models/task-definition.ts b/src/app/api/models/task-definition.ts index 01e1aa29e..c470f1aca 100644 --- a/src/app/api/models/task-definition.ts +++ b/src/app/api/models/task-definition.ts @@ -158,7 +158,7 @@ export class TaskDefinition extends Entity { public getNumbasTestUrl(asAttachment: boolean = false) { const constants = AppInjector.get(DoubtfireConstants); - return `${constants.API_URL}/units/${this.unit.id}/task_definitions/${this.id}/numbas_test.json${ + return `${constants.API_URL}/units/${this.unit.id}/task_definitions/${this.id}/numbas_data.json${ asAttachment ? '?as_attachment=true' : '' }`; } @@ -190,7 +190,7 @@ export class TaskDefinition extends Entity { public get numbasTestUploadUrl(): string { return `${AppInjector.get(DoubtfireConstants).API_URL}/units/${this.unit.id}/task_definitions/${ this.id - }/numbas_test`; + }/numbas_data`; } public get taskAssessmentResourcesUploadUrl(): string { diff --git a/src/app/api/services/task-definition.service.ts b/src/app/api/services/task-definition.service.ts index 74a5d36fc..649538bfd 100644 --- a/src/app/api/services/task-definition.service.ts +++ b/src/app/api/services/task-definition.service.ts @@ -139,4 +139,10 @@ export class TaskDefinitionService extends CachedEntityService { formData.append('file', file); return AppInjector.get(HttpClient).post(taskDefinition.taskAssessmentResourcesUploadUrl, formData); } + + public uploadNumbasData(taskDefinition: TaskDefinition, file: File): Observable { + const formData = new FormData(); + formData.append('file', file); + return AppInjector.get(HttpClient).post(taskDefinition.numbasTestUploadUrl, formData); + } } diff --git a/src/app/units/states/edit/directives/unit-tasks-editor/task-definition-editor/task-definition-numbas/task-definition-numbas.component.ts b/src/app/units/states/edit/directives/unit-tasks-editor/task-definition-editor/task-definition-numbas/task-definition-numbas.component.ts index b36218bd1..3c18b3174 100644 --- a/src/app/units/states/edit/directives/unit-tasks-editor/task-definition-editor/task-definition-numbas/task-definition-numbas.component.ts +++ b/src/app/units/states/edit/directives/unit-tasks-editor/task-definition-editor/task-definition-numbas/task-definition-numbas.component.ts @@ -41,29 +41,13 @@ export class TaskDefinitionNumbasComponent { const validFiles = Array.from(files as ArrayLike).filter((f) => f.type === 'application/zip'); if (validFiles.length > 0) { const file = validFiles[0]; - // Temporary until Numbas backend is fixed: save uploaded file to local Downloads folder - this.saveZipFile(file); + this.taskDefinitionService.uploadNumbasData(this.taskDefinition, file).subscribe({ + next: () => this.alerts.add('success', 'Uploaded Numbas test data', 2000), + error: (message) => this.alerts.add('danger', message, 6000), + }); this.taskDefinition.hasUploadedNumbasTest = true; - // this.taskDefinitionService.uploadNumbasTest(this.taskDefinition, file).subscribe({ - // next: () => this.alerts.add('success', 'Uploaded Numbas test', 2000), - // error: (message) => this.alerts.add('danger', message, 6000), - // }); } else { - this.alerts.add('danger', 'Please drop a ZIP to upload for this task', 6000); + this.alerts.add('danger', 'Please drop a zip file to upload Numbas test data for this task', 6000); } } - - private saveZipFile(zipData) { - const blob = new Blob([zipData], {type: 'application/zip'}); - - // Create an anchor element and set its href to the blob URL - const link = document.createElement('a'); - link.href = URL.createObjectURL(blob); - link.download = 'numbas.zip'; - - // Append the link to the document, trigger the download, then remove the link - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); - } }