From 03ccec311c00b6d07cf87044fe89fb6731e0afd8 Mon Sep 17 00:00:00 2001 From: Hiroki Terashima Date: Tue, 26 Nov 2024 09:16:27 -0800 Subject: [PATCH] refactor(AnnotationService): Simplify getLatestAnnotation logic (#2005) --- .../wise5/services/annotationService.ts | 88 ++++++------------- src/assets/wise5/vle/vle.component.ts | 5 +- 2 files changed, 29 insertions(+), 64 deletions(-) diff --git a/src/assets/wise5/services/annotationService.ts b/src/assets/wise5/services/annotationService.ts index 4a510ae0d05..9ab866799fe 100644 --- a/src/assets/wise5/services/annotationService.ts +++ b/src/assets/wise5/services/annotationService.ts @@ -1,5 +1,3 @@ -'use strict'; - import { Injectable } from '@angular/core'; import { ProjectService } from './projectService'; import { ConfigService } from './configService'; @@ -19,9 +17,9 @@ export class AnnotationService { public annotationReceived$: Observable = this.annotationReceivedSource.asObservable(); constructor( + private configService: ConfigService, private http: HttpClient, - private ConfigService: ConfigService, - private ProjectService: ProjectService + private projectService: ProjectService ) {} getAnnotations(): Annotation[] { @@ -43,60 +41,28 @@ export class AnnotationService { * @param params an object containing the params to match * @returns the latest annotation that matches the params */ - getLatestAnnotation(params) { - let annotation = null; - - if (params != null) { - let nodeId = params.nodeId; - let componentId = params.componentId; - let fromWorkgroupId = params.fromWorkgroupId; - let toWorkgroupId = params.toWorkgroupId; - let type = params.type; - - let annotations = this.annotations; - - if (annotations != null) { - for (let a = annotations.length - 1; a >= 0; a--) { - let tempAnnotation = annotations[a]; - - if (tempAnnotation != null) { - let match = true; - - if (nodeId && tempAnnotation.nodeId !== nodeId) { - match = false; - } - if (match && componentId && tempAnnotation.componentId !== componentId) { - match = false; - } - if (match && fromWorkgroupId && tempAnnotation.fromWorkgroupId !== fromWorkgroupId) { - match = false; - } - if (match && toWorkgroupId && tempAnnotation.toWorkgroupId !== toWorkgroupId) { - match = false; - } - if (match && type) { - if (type.constructor === Array) { - for (let thisType of type) { - if (tempAnnotation.type !== thisType) { - match = false; - } - } - } else { - if (tempAnnotation.type !== type) { - match = false; - } - } - } - - if (match) { - annotation = tempAnnotation; - break; - } - } + getLatestAnnotation(params): any { + for (let a = this.annotations.length - 1; a >= 0; a--) { + const annotation = this.annotations[a]; + let match = true; + if (annotation.nodeId !== params.nodeId) { + match = false; + } + if (match && annotation.componentId !== params.componentId) { + match = false; + } + if (match) { + if (params.type.constructor === Array) { + match = params.type.every((thisType) => annotation.type === thisType); + } else { + match = annotation.type === params.type; + } + if (match) { + return annotation; } } } - return annotation; + return null; } /** @@ -155,7 +121,7 @@ export class AnnotationService { annotation.requestToken = generateRandomKey(); // use this to keep track of unsaved annotations. this.addOrUpdateAnnotation(annotation); const annotations = [annotation]; - if (this.ConfigService.isPreview()) { + if (this.configService.isPreview()) { // if we're in preview, don't make any request to the server but pretend we did let savedAnnotationDataResponse = { annotations: annotations @@ -164,13 +130,13 @@ export class AnnotationService { return Promise.resolve(annotation); } else { const params = { - runId: this.ConfigService.getRunId(), - workgroupId: this.ConfigService.getWorkgroupId(), + runId: this.configService.getRunId(), + workgroupId: this.configService.getWorkgroupId(), annotations: JSON.stringify(annotations) }; const headers = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }); return this.http - .post(this.ConfigService.getConfigParam('teacherDataURL'), $.param(params), { + .post(this.configService.getConfigParam('teacherDataURL'), $.param(params), { headers: headers }) .toPromise() @@ -206,7 +172,7 @@ export class AnnotationService { localAnnotation.serverSaveTime = savedAnnotation.serverSaveTime; localAnnotation.requestToken = null; // requestToken is no longer needed. - if (this.ConfigService.isPreview() && localAnnotation.id == null) { + if (this.configService.isPreview() && localAnnotation.id == null) { /* * we are in preview mode so we will set a dummy * annotation id into the annotation @@ -286,7 +252,7 @@ export class AnnotationService { return annotations.filter((annotation) => { return ( this.isScoreOrAutoScore(annotation) && - this.ProjectService.shouldIncludeInTotalScore(annotation.nodeId, annotation.componentId) + this.projectService.shouldIncludeInTotalScore(annotation.nodeId, annotation.componentId) ); }); } diff --git a/src/assets/wise5/vle/vle.component.ts b/src/assets/wise5/vle/vle.component.ts index 74903214cf6..5fe8b3375da 100644 --- a/src/assets/wise5/vle/vle.component.ts +++ b/src/assets/wise5/vle/vle.component.ts @@ -378,12 +378,11 @@ export class VLEComponent implements AfterViewInit { * @returns {the|Object} */ getLatestAnnotationForComponent: (nodeId, componentId, annotationType) => { - let params = { + return this.annotationService.getLatestAnnotation({ nodeId: nodeId, componentId: componentId, type: annotationType - }; - return this.annotationService.getLatestAnnotation(params); + }); }, /** * Updates the annotation locally and on the server