From 896f62b2f21b9ba43ce81ea9e7dd0904b8c3dcf7 Mon Sep 17 00:00:00 2001 From: Hiroki Terashima Date: Wed, 11 Oct 2023 10:12:30 -0700 Subject: [PATCH] refactor(TeacherDataService): Clean up pause period code (#1460) --- .../shared/top-bar/top-bar.component.ts | 6 +- .../wise5/services/teacherDataService.ts | 93 +++++++------------ src/messages.xlf | 2 +- 3 files changed, 33 insertions(+), 68 deletions(-) diff --git a/src/assets/wise5/classroomMonitor/classroomMonitorComponents/shared/top-bar/top-bar.component.ts b/src/assets/wise5/classroomMonitor/classroomMonitorComponents/shared/top-bar/top-bar.component.ts index 81bdae7bb2d..aac2bdda51d 100644 --- a/src/assets/wise5/classroomMonitor/classroomMonitorComponents/shared/top-bar/top-bar.component.ts +++ b/src/assets/wise5/classroomMonitor/classroomMonitorComponents/shared/top-bar/top-bar.component.ts @@ -91,12 +91,8 @@ export class TopBarComponent implements OnInit { ); } - /** - * Check whether any period in the run is paused - * @return Boolean whether any of the periods are paused - */ protected isAnyPeriodPaused(): boolean { - return this.dataService.isAnyPeriodPaused(); + return this.dataService.getPeriods().some((period) => period.paused); } protected switchToAuthoringView(): void { diff --git a/src/assets/wise5/services/teacherDataService.ts b/src/assets/wise5/services/teacherDataService.ts index c7a5329b985..7cfde5ded4c 100644 --- a/src/assets/wise5/services/teacherDataService.ts +++ b/src/assets/wise5/services/teacherDataService.ts @@ -738,35 +738,8 @@ export class TeacherDataService extends DataService { ); } - isAnyPeriodPaused() { - return this.getPeriods().some((period) => { - return period.paused; - }); - } - - isPeriodPaused(periodId: number): boolean { - if (periodId === -1) { - return this.isAllPeriodsPaused(); - } else { - return this.getPeriodById(periodId).paused; - } - } - - isAllPeriodsPaused() { - let numPausedPeriods = 0; - const periods = this.getPeriods(); - for (const period of periods) { - if (period.paused) { - numPausedPeriods++; - } - } - return numPausedPeriods === periods.length; - } - - getPeriodById(periodId: number): any { - return this.getPeriods().find((period) => { - return period.periodId === periodId; - }); + private getPeriodById(periodId: number): any { + return this.getPeriods().find((period) => period.periodId === periodId); } /** @@ -774,9 +747,9 @@ export class TeacherDataService extends DataService { * @param periodId the id of the period to toggle * @param isPaused Boolean whether the period should be paused or not */ - pauseScreensChanged(periodId, isPaused) { + pauseScreensChanged(periodId: number, isPaused: boolean): void { this.updatePausedRunStatusValue(periodId, isPaused); - this.sendRunStatusThenHandlePauseScreen(periodId, isPaused); + this.saveRunStatusThenHandlePauseScreen(periodId, isPaused); const context = 'ClassroomMonitor', nodeId = null, componentId = null, @@ -787,19 +760,17 @@ export class TeacherDataService extends DataService { this.saveEvent(context, nodeId, componentId, componentType, category, event, data); } - sendRunStatusThenHandlePauseScreen(periodId, isPaused) { - this.sendRunStatus() - .toPromise() - .then(() => { - if (isPaused) { - this.TeacherWebSocketService.pauseScreens(periodId); - } else { - this.TeacherWebSocketService.unPauseScreens(periodId); - } - }); + private saveRunStatusThenHandlePauseScreen(periodId: number, isPaused: boolean): void { + this.saveRunStatus().subscribe(() => { + if (isPaused) { + this.TeacherWebSocketService.pauseScreens(periodId); + } else { + this.TeacherWebSocketService.unPauseScreens(periodId); + } + }); } - sendRunStatus() { + private saveRunStatus(): Observable { const url = this.ConfigService.getConfigParam('runStatusURL'); const body = new HttpParams() .set('runId', this.ConfigService.getConfigParam('runId')) @@ -807,46 +778,44 @@ export class TeacherDataService extends DataService { const options = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }; - return this.http.post(url, body, options); - } - - createRunStatus() { - const periods = this.ConfigService.getPeriods(); - for (const period of periods) { - period.paused = false; - } - return { - runId: this.ConfigService.getConfigParam('runId'), - periods: periods - }; + return this.http.post(url, body, options); } /** * Update the paused value for a period in our run status * @param periodId the period id or -1 for all periods - * @param value whether the period is paused or not + * @param isPaused whether the period is paused or not */ - updatePausedRunStatusValue(periodId, value) { + private updatePausedRunStatusValue(periodId: number, isPaused: boolean): void { if (this.runStatus == null) { this.runStatus = this.createRunStatus(); } if (periodId === -1) { - this.updateAllPeriodsPausedValue(value); + this.updateAllPeriodsPausedValue(isPaused); } else { - this.updatePeriodPausedValue(periodId, value); + this.updatePeriodPausedValue(periodId, isPaused); } } - updateAllPeriodsPausedValue(value) { + private createRunStatus(): any { + const periods = this.ConfigService.getPeriods(); + periods.forEach((period) => (period.paused = false)); + return { + runId: this.ConfigService.getConfigParam('runId'), + periods: periods + }; + } + + private updateAllPeriodsPausedValue(isPaused: boolean): void { for (const period of this.runStatus.periods) { - period.paused = value; + period.paused = isPaused; } } - updatePeriodPausedValue(periodId, value) { + private updatePeriodPausedValue(periodId: number, isPaused: boolean): void { for (const period of this.runStatus.periods) { if (period.periodId === periodId) { - period.paused = value; + period.paused = isPaused; } } } diff --git a/src/messages.xlf b/src/messages.xlf index e9b6f54c3a0..a34858b3954 100644 --- a/src/messages.xlf +++ b/src/messages.xlf @@ -13909,7 +13909,7 @@ Click "Cancel" to keep the invalid JSON open so you can fix it. src/assets/wise5/classroomMonitor/classroomMonitorComponents/shared/top-bar/top-bar.component.ts - 105 + 101