diff --git a/src/app/modules/library/library-project-menu/library-project-menu.component.ts b/src/app/modules/library/library-project-menu/library-project-menu.component.ts index 59236c597a3..7868496e2cb 100644 --- a/src/app/modules/library/library-project-menu/library-project-menu.component.ts +++ b/src/app/modules/library/library-project-menu/library-project-menu.component.ts @@ -7,7 +7,6 @@ import { UserService } from '../../../services/user.service'; import { ConfigService } from '../../../services/config.service'; import { EditRunWarningDialogComponent } from '../../../teacher/edit-run-warning-dialog/edit-run-warning-dialog.component'; import { ArchiveProjectService } from '../../../services/archive-project.service'; -import { ArchiveProjectResponse } from '../../../domain/archiveProjectResponse'; @Component({ selector: 'app-library-project-menu', @@ -88,16 +87,6 @@ export class LibraryProjectMenuComponent { } protected archive(archive: boolean): void { - this.archiveProjectService[archive ? 'archiveProject' : 'unarchiveProject']( - this.project - ).subscribe({ - next: (response: ArchiveProjectResponse) => { - this.archiveProjectService.updateProjectArchivedStatus(this.project, response.archived); - this.archiveProjectService.showArchiveProjectSuccessMessage(this.project, archive); - }, - error: () => { - this.archiveProjectService.showArchiveProjectErrorMessage(archive); - } - }); + this.archiveProjectService.archiveProject(this.project, archive); } } diff --git a/src/app/modules/library/personal-library/personal-library.component.ts b/src/app/modules/library/personal-library/personal-library.component.ts index 1efb7e9a610..1d7ebabcfa0 100644 --- a/src/app/modules/library/personal-library/personal-library.component.ts +++ b/src/app/modules/library/personal-library/personal-library.component.ts @@ -143,11 +143,6 @@ export class PersonalLibraryComponent extends LibraryComponent { } } - protected refreshProjects(): void { - this.unselectAllProjects(); - this.archiveProjectService.refreshProjects(); - } - protected unselectAllProjects(): void { this.projects.forEach((project) => (project.selected = false)); this.selectedProjects.set([]); diff --git a/src/app/services/archive-project.service.ts b/src/app/services/archive-project.service.ts index b39e26faeca..45ab946d2be 100644 --- a/src/app/services/archive-project.service.ts +++ b/src/app/services/archive-project.service.ts @@ -12,83 +12,47 @@ export class ArchiveProjectService { constructor(private http: HttpClient, private snackBar: MatSnackBar) {} - archiveProject(project: Project): Observable { - return this.http.put(`/api/project/${project.id}/archived`, null); - } - - private makeArchiveProjectsRequest(projects: Project[]): Observable { - const projectIds = projects.map((project) => project.id); - return this.http.put(`/api/projects/archived`, projectIds); - } - - unarchiveProject(project: Project): Observable { - return this.http.delete(`/api/project/${project.id}/archived`); - } - - private makeUnarchiveProjectsRequest(projects: Project[]): Observable { - let params = new HttpParams(); - for (const project of projects) { - params = params.append('projectIds', project.id); - } - return this.http.delete(`/api/projects/archived`, { - params: params - }); - } - - refreshProjects(): void { - this.refreshProjectsEventSource.next(); - } - - showArchiveProjectSuccessMessage(project: Project, archive: boolean): void { - this.snackBar - .open($localize`Successfully ${archive ? 'archived' : 'restored'} unit.`, $localize`Undo`) - .onAction() - .subscribe(() => { - this.undoArchiveProjectAction(project, archive ? 'unarchiveProject' : 'archiveProject'); - }); - } - - undoArchiveProjectAction(project: Project, archiveFunctionName: string): void { - this[archiveFunctionName](project).subscribe({ + archiveProject(project: Project, archive: boolean): void { + this[archive ? 'makeArchiveProjectRequest' : 'makeUnarchiveProjectRequest'](project).subscribe({ next: (response: ArchiveProjectResponse) => { this.updateProjectArchivedStatus(project, response.archived); - this.snackBar.open($localize`Action undone.`); + this.showArchiveProjectSuccessMessage(project, archive); }, error: () => { - this.snackBar.open($localize`Error undoing action.`); + this.showArchiveProjectErrorMessage(archive); } }); } - updateProjectArchivedStatus(project: Project, archived: boolean): void { + private makeArchiveProjectRequest(project: Project): Observable { + return this.http.put(`/api/project/${project.id}/archived`, null); + } + + private makeUnarchiveProjectRequest(project: Project): Observable { + return this.http.delete(`/api/project/${project.id}/archived`); + } + + private updateProjectArchivedStatus(project: Project, archived: boolean): void { project.updateArchivedStatus(archived); this.refreshProjects(); } - showArchiveProjectsSuccessMessage(projects: Project[], archived: boolean): void { + private showArchiveProjectSuccessMessage(project: Project, archive: boolean): void { this.snackBar - .open( - archived - ? $localize`Successfully archived ${projects.length} unit(s).` - : $localize`Successfully restored ${projects.length} unit(s).`, - $localize`Undo` - ) + .open($localize`Successfully ${archive ? 'archived' : 'restored'} unit.`, $localize`Undo`) .onAction() .subscribe(() => { - this.undoArchiveProjects( - projects, - archived ? 'makeUnarchiveProjectsRequest' : 'makeArchiveProjectsRequest' + this.undoArchiveProjectAction( + project, + archive ? 'makeUnarchiveProjectRequest' : 'makeArchiveProjectRequest' ); }); } - undoArchiveProjects(projects: Project[], archiveFunctionName: string): void { - this[archiveFunctionName](projects).subscribe({ - next: (response: ArchiveProjectResponse[]) => { - for (const projectResponse of response) { - const project = projects.find((project) => project.id === projectResponse.id); - this.updateProjectArchivedStatus(project, projectResponse.archived); - } + private undoArchiveProjectAction(project: Project, archiveFunctionName: string): void { + this[archiveFunctionName](project).subscribe({ + next: (response: ArchiveProjectResponse) => { + this.updateProjectArchivedStatus(project, response.archived); this.snackBar.open($localize`Action undone.`); }, error: () => { @@ -97,7 +61,7 @@ export class ArchiveProjectService { }); } - showArchiveProjectErrorMessage(archive: boolean): void { + private showArchiveProjectErrorMessage(archive: boolean): void { this.snackBar.open( archive ? $localize`Error archiving unit.` : $localize`Error restoring unit.` ); @@ -117,6 +81,21 @@ export class ArchiveProjectService { }); } + private makeArchiveProjectsRequest(projects: Project[]): Observable { + const projectIds = projects.map((project) => project.id); + return this.http.put(`/api/projects/archived`, projectIds); + } + + private makeUnarchiveProjectsRequest(projects: Project[]): Observable { + let params = new HttpParams(); + for (const project of projects) { + params = params.append('projectIds', project.id); + } + return this.http.delete(`/api/projects/archived`, { + params: params + }); + } + private updateProjectsArchivedStatus( projects: Project[], archiveProjectsResponse: ArchiveProjectResponse[] @@ -152,12 +131,6 @@ export class ArchiveProjectService { }); } - private showErrorSnackBar(archive: boolean): void { - this.snackBar.open( - archive ? $localize`Error archiving unit(s).` : $localize`Error restoring unit(s).` - ); - } - private undoArchiveAction(projects: Project[], archiveFunctionName: string): void { this[archiveFunctionName](projects).subscribe({ next: (archiveProjectsResponse: ArchiveProjectResponse[]) => { @@ -170,4 +143,14 @@ export class ArchiveProjectService { } }); } + + private showErrorSnackBar(archive: boolean): void { + this.snackBar.open( + archive ? $localize`Error archiving unit(s).` : $localize`Error restoring unit(s).` + ); + } + + private refreshProjects(): void { + this.refreshProjectsEventSource.next(); + } } diff --git a/src/app/teacher/run-menu/run-menu.component.spec.ts b/src/app/teacher/run-menu/run-menu.component.spec.ts index b8f67526389..647e2aa2078 100644 --- a/src/app/teacher/run-menu/run-menu.component.spec.ts +++ b/src/app/teacher/run-menu/run-menu.component.spec.ts @@ -128,7 +128,8 @@ function setRun(archived: boolean): void { archived: archived, id: 1, owner: owner, - sharedOwners: [] + sharedOwners: [], + tags: [] } }); } diff --git a/src/app/teacher/run-menu/run-menu.component.ts b/src/app/teacher/run-menu/run-menu.component.ts index 3b0f88283d5..2bb73952461 100644 --- a/src/app/teacher/run-menu/run-menu.component.ts +++ b/src/app/teacher/run-menu/run-menu.component.ts @@ -8,9 +8,7 @@ import { ConfigService } from '../../services/config.service'; import { RunSettingsDialogComponent } from '../run-settings-dialog/run-settings-dialog.component'; import { EditRunWarningDialogComponent } from '../edit-run-warning-dialog/edit-run-warning-dialog.component'; import { Router } from '@angular/router'; -import { MatSnackBar } from '@angular/material/snack-bar'; import { ArchiveProjectService } from '../../services/archive-project.service'; -import { ArchiveProjectResponse } from '../../domain/archiveProjectResponse'; @Component({ selector: 'app-run-menu', @@ -28,8 +26,7 @@ export class RunMenuComponent implements OnInit { private dialog: MatDialog, private userService: UserService, private configService: ConfigService, - private router: Router, - private snackBar: MatSnackBar + private router: Router ) {} ngOnInit() { @@ -85,55 +82,6 @@ export class RunMenuComponent implements OnInit { } protected archive(archive: boolean): void { - this.archiveProjectService[archive ? 'archiveProject' : 'unarchiveProject']( - this.run.project - ).subscribe({ - next: (response: ArchiveProjectResponse) => { - this.updateArchivedStatus(this.run, response.archived); - this.showSuccessMessage(this.run, archive); - }, - error: () => { - this.showErrorMessage(archive); - } - }); - } - - private showSuccessMessage(run: TeacherRun, archive: boolean): void { - this.openSnackBar( - run, - $localize`Successfully ${archive ? 'archived' : 'restored'} unit.`, - archive ? 'unarchiveProject' : 'archiveProject' - ); - } - - private showErrorMessage(archive: boolean): void { - this.snackBar.open($localize`Error ${archive ? 'archiving' : 'unarchiving'} unit.`); - } - - private updateArchivedStatus(run: TeacherRun, archived: boolean): void { - run.project.archived = archived; - this.runArchiveStatusChangedEvent.emit(); - } - - private openSnackBar(run: TeacherRun, message: string, undoFunctionName: string): void { - this.snackBar - .open(message, $localize`Undo`) - .onAction() - .subscribe(() => { - this.undoArchiveAction(run, undoFunctionName); - }); - } - - private undoArchiveAction(run: TeacherRun, archiveFunctionName: string): void { - this.archiveProjectService[archiveFunctionName](run.project).subscribe({ - next: (response: ArchiveProjectResponse) => { - run.project.archived = response.archived; - this.archiveProjectService.refreshProjects(); - this.snackBar.open($localize`Action undone.`); - }, - error: () => { - this.snackBar.open($localize`Error undoing action.`); - } - }); + this.archiveProjectService.archiveProject(this.run.project, archive); } } diff --git a/src/app/teacher/teacher-run-list-item/teacher-run-list-item.component.spec.ts b/src/app/teacher/teacher-run-list-item/teacher-run-list-item.component.spec.ts index ae71f88d368..c2660158eaa 100644 --- a/src/app/teacher/teacher-run-list-item/teacher-run-list-item.component.spec.ts +++ b/src/app/teacher/teacher-run-list-item/teacher-run-list-item.component.spec.ts @@ -118,14 +118,10 @@ function render() { function runArchiveStatusChanged() { describe('run is not archived and archive menu button is clicked', () => { it('should archive run and emit events', async () => { - const runSelectedSpy = spyOn(component.runSelectedStatusChangedEvent, 'emit'); - const runArchiveSpy = spyOn(component.runArchiveStatusChangedEvent, 'emit'); expect(await runListItemHarness.isArchived()).toBeFalse(); spyOn(http, 'put').and.returnValue(of(new ArchiveProjectResponse(1, true))); await runListItemHarness.clickArchiveMenuButton(); expect(await runListItemHarness.isArchived()).toBeTrue(); - expect(runSelectedSpy).toHaveBeenCalled(); - expect(runArchiveSpy).toHaveBeenCalled(); }); }); } diff --git a/src/messages.xlf b/src/messages.xlf index e3c83c2cadb..3741672061c 100644 --- a/src/messages.xlf +++ b/src/messages.xlf @@ -5873,7 +5873,7 @@ Click "Cancel" to keep the invalid JSON open so you can fix it.units src/app/modules/library/personal-library/personal-library.component.ts - 32 + 28 @@ -7408,118 +7408,82 @@ Click "Cancel" to keep the invalid JSON open so you can fix it.Successfully unit. src/app/services/archive-project.service.ts - 44 - - - src/app/teacher/run-menu/run-menu.component.ts - 104 + 42 Undo src/app/services/archive-project.service.ts - 44 - - - src/app/services/archive-project.service.ts - 74 + 42 src/app/services/archive-project.service.ts - 144 - - - src/app/teacher/run-menu/run-menu.component.ts - 120 + 123 Action undone. src/app/services/archive-project.service.ts - 55 - - - src/app/services/archive-project.service.ts - 92 + 56 src/app/services/archive-project.service.ts - 166 - - - src/app/teacher/run-menu/run-menu.component.ts - 132 + 139 Error undoing action. src/app/services/archive-project.service.ts - 58 - - - src/app/services/archive-project.service.ts - 95 - - - src/app/services/archive-project.service.ts - 169 - - - src/app/teacher/run-menu/run-menu.component.ts - 135 - - - - Successfully archived unit(s). - - src/app/services/archive-project.service.ts - 72 + 59 src/app/services/archive-project.service.ts 142 - - Successfully restored unit(s). + + Error archiving unit. src/app/services/archive-project.service.ts - 73 + 66 + + + Error restoring unit. src/app/services/archive-project.service.ts - 143 + 66 - - Error archiving unit. + + Successfully archived unit(s). src/app/services/archive-project.service.ts - 102 + 121 - - Error restoring unit. + + Successfully restored unit(s). src/app/services/archive-project.service.ts - 102 + 122 Error archiving unit(s). src/app/services/archive-project.service.ts - 157 + 149 Error restoring unit(s). src/app/services/archive-project.service.ts - 157 + 149 @@ -8443,21 +8407,14 @@ Click "Cancel" to keep the invalid JSON open so you can fix it.Run Settings src/app/teacher/run-menu/run-menu.component.ts - 68 + 65 Edit Classroom Unit Warning src/app/teacher/run-menu/run-menu.component.ts - 78 - - - - Error unit. - - src/app/teacher/run-menu/run-menu.component.ts - 110 + 75