Skip to content

Commit

Permalink
refactor(ArchiveProjectsButton): Input selected projects only (#1592)
Browse files Browse the repository at this point in the history
This component did not need all the projects- only the selected ones. Also, don't call extractProjects() in ngOnInit(). It's already called in ngOnChanges(). Move function body to ngOnChanges().
  • Loading branch information
hirokiterashima authored Jan 29, 2024
1 parent 948a51e commit 8b0d13b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<archive-projects-button
*ngIf="selectedProjects().length > 0"
[showArchive]="!showArchivedView"
[projects]="filteredProjects"
[projects]="selectedProjects()"
(archiveActionEvent)="refreshProjects()"
></archive-projects-button>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<button
mat-icon-button
*ngIf="showArchive"
(click)="archiveSelectedProjects(true)"
(click)="archiveProjects(true)"
matTooltip="Archive"
matTooltipPosition="above"
i18n-matTooltip
Expand All @@ -11,7 +11,7 @@
<button
mat-icon-button
*ngIf="!showArchive"
(click)="archiveSelectedProjects(false)"
(click)="archiveProjects(false)"
matTooltip="Restore"
matTooltipPosition="above"
i18n-matTooltip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ export class ArchiveProjectsButtonComponent {
private snackBar: MatSnackBar
) {}

protected archiveSelectedProjects(archive: boolean): Subscription {
const projects = this.getSelectedProjects();
protected archiveProjects(archive: boolean): Subscription {
return this.archiveProjectService[archive ? 'archiveProjects' : 'unarchiveProjects'](
projects
this.projects
).subscribe({
next: (archiveProjectsResponse: ArchiveProjectResponse[]) => {
this.updateProjectsArchivedStatus(projects, archiveProjectsResponse);
this.openSuccessSnackBar(projects, archiveProjectsResponse, archive);
this.updateProjectsArchivedStatus(this.projects, archiveProjectsResponse);
this.openSuccessSnackBar(this.projects, archiveProjectsResponse, archive);
},
error: () => {
this.showErrorSnackBar(archive);
Expand Down Expand Up @@ -91,8 +90,4 @@ export class ArchiveProjectsButtonComponent {
}
});
}

private getSelectedProjects(): Project[] {
return this.projects.filter((project: Project) => project.selected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<archive-projects-button
*ngIf="numSelectedRuns > 0"
[showArchive]="showArchive"
[projects]="projects"
[projects]="selectedProjects"
(archiveActionEvent)="archiveActionEvent.emit()"
></archive-projects-button>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SelectRunsOption } from './select-runs-option';
export class SelectRunsControlsComponent {
@Output() archiveActionEvent = new EventEmitter<void>();
protected numSelectedRuns: number = 0;
protected projects: Project[] = [];
protected selectedProjects: Project[] = [];
@Input() runChangedEventEmitter: EventEmitter<void> = new EventEmitter<void>();
@Input() runs: TeacherRun[] = [];
protected selectedAllRuns: boolean = false;
Expand All @@ -22,23 +22,20 @@ export class SelectRunsControlsComponent {
@Input() showArchive: boolean = false;

ngOnInit(): void {
this.extractProjects();
this.runChangedEventEmitter.subscribe(() => {
this.ngOnChanges();
});
}

ngOnChanges(): void {
this.extractProjects();
this.selectedProjects = this.runs
.map((run: TeacherRun) => run.project)
.filter((project: Project) => project.selected);
this.numSelectedRuns = this.runs.filter((run: TeacherRun) => run.project.selected).length;
this.selectedAllRuns = this.numSelectedRuns === this.runs.length;
this.selectedSomeRuns = this.numSelectedRuns !== 0 && !this.selectedAllRuns;
}

private extractProjects(): void {
this.projects = this.runs.map((run: TeacherRun) => run.project);
}

protected selectAllRunsCheckboxClicked(): void {
this.selectRunsOptionChosenEvent.emit(
this.selectedAllRuns || this.selectedSomeRuns ? SelectRunsOption.None : SelectRunsOption.All
Expand Down
14 changes: 7 additions & 7 deletions src/messages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -7431,7 +7431,7 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/teacher/archive-projects-button/archive-projects-button.component.ts</context>
<context context-type="linenumber">68</context>
<context context-type="linenumber">67</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/teacher/run-menu/run-menu.component.ts</context>
Expand All @@ -7450,7 +7450,7 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/teacher/archive-projects-button/archive-projects-button.component.ts</context>
<context context-type="linenumber">87</context>
<context context-type="linenumber">86</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/teacher/run-menu/run-menu.component.ts</context>
Expand All @@ -7469,7 +7469,7 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/teacher/archive-projects-button/archive-projects-button.component.ts</context>
<context context-type="linenumber">90</context>
<context context-type="linenumber">89</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/teacher/run-menu/run-menu.component.ts</context>
Expand All @@ -7484,7 +7484,7 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/teacher/archive-projects-button/archive-projects-button.component.ts</context>
<context context-type="linenumber">66</context>
<context context-type="linenumber">65</context>
</context-group>
</trans-unit>
<trans-unit id="6233410407787231949" datatype="html">
Expand All @@ -7495,7 +7495,7 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/teacher/archive-projects-button/archive-projects-button.component.ts</context>
<context context-type="linenumber">67</context>
<context context-type="linenumber">66</context>
</context-group>
</trans-unit>
<trans-unit id="4649373271512187502" datatype="html">
Expand Down Expand Up @@ -8040,14 +8040,14 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
<source>Error archiving unit(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/teacher/archive-projects-button/archive-projects-button.component.ts</context>
<context context-type="linenumber">78</context>
<context context-type="linenumber">77</context>
</context-group>
</trans-unit>
<trans-unit id="3419148337120710852" datatype="html">
<source>Error restoring unit(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/teacher/archive-projects-button/archive-projects-button.component.ts</context>
<context context-type="linenumber">78</context>
<context context-type="linenumber">77</context>
</context-group>
</trans-unit>
<trans-unit id="a2d9400df68d9f4e89fe24f6a3f40bc5322f19f2" datatype="html">
Expand Down

0 comments on commit 8b0d13b

Please sign in to comment.