Skip to content

Commit

Permalink
Refactor groups functionalities out of issues-viewer as a service (#384)
Browse files Browse the repository at this point in the history
Currently, the issues-viewer component contains the fields and functions
tracking the groups to be shown and hidden.

Let's refactor out these functionalities into a new service to ensure 
better separations of concerns.
  • Loading branch information
kokerinks authored Aug 3, 2024
1 parent 4305a0f commit c5999bf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 62 deletions.
51 changes: 51 additions & 0 deletions src/app/core/services/grouping/group.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Injectable } from '@angular/core';
import { Group } from '../../models/github/group.interface';
import { GroupingContextService } from './grouping-context.service';

@Injectable({
providedIn: 'root'
})
export class GroupService {
/** Users to show as columns */
public groups: Group[] = [];

/** The list of users with 0 issues (hidden) */
hiddenGroups: Group[] = [];

constructor(public groupingContextService: GroupingContextService) {}

/**
* Resets both arrays tracking current groups and hidden groups.
*/
resetGroups() {
this.groups = [];
this.hiddenGroups = [];
}

/**
* Update the list of hidden group based on the new info.
* @param issueLength The number of issues under this group.
* @param target The group.
*/
updateHiddenGroups(issueLength: number, target: Group) {
// If the group is in the hidden list, add it if it has no issues.
// Also add it if it is unchecked in the filter.
if (issueLength === 0 && this.groupingContextService.isInHiddenList(target)) {
this.addToHiddenGroups(target);
} else {
this.removeFromHiddenGroups(target);
}
}

private addToHiddenGroups(target: Group) {
const isGroupPresent = this.hiddenGroups.some((group) => group.equals(target));

if (!isGroupPresent) {
this.hiddenGroups.push(target);
}
}

private removeFromHiddenGroups(target: Group) {
this.hiddenGroups = this.hiddenGroups.filter((group) => !group.equals(target));
}
}
8 changes: 4 additions & 4 deletions src/app/issues-viewer/issues-viewer.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<div class="loading-spinner" *ngIf="(this.viewService.isChangingRepo | async) || this.groups.length === 0; else elseBlock">
<div class="loading-spinner" *ngIf="(this.viewService.isChangingRepo | async) || this.groupService.groups.length === 0; else elseBlock">
<mat-progress-spinner color="primary" mode="indeterminate" diameter="50" strokeWidth="5"> </mat-progress-spinner>
</div>

Expand All @@ -8,15 +8,15 @@

<div class="wrapper">
<app-card-view
*ngFor="let group of groups"
*ngFor="let group of this.groupService.groups"
class="issue-table"
#card
[ngStyle]="{ display: card.isLoading || card.issueLength > 0 ? 'initial' : 'none' }"
[group]="group"
[headers]="this.displayedColumns"
(issueLengthChange)="updateHiddenGroups($event, group)"
(issueLengthChange)="this.groupService.updateHiddenGroups($event, group)"
></app-card-view>
<app-hidden-groups [groups]="this.hiddenGroups"></app-hidden-groups>
<app-hidden-groups [groups]="this.groupService.hiddenGroups"></app-hidden-groups>
</div>
</ng-template>
</div>
62 changes: 4 additions & 58 deletions src/app/issues-viewer/issues-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { AfterViewInit, Component, OnDestroy, OnInit, QueryList, ViewChildren }
import { NavigationEnd, NavigationStart, Router } from '@angular/router';
import { BehaviorSubject, of, Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import { Group } from '../core/models/github/group.interface';
import { Repo } from '../core/models/repo.model';
import { ErrorMessageService } from '../core/services/error-message.service';
import { FiltersService } from '../core/services/filters.service';
import { GithubService } from '../core/services/github.service';
import { GroupService } from '../core/services/grouping/group.service';
import { GroupingContextService } from '../core/services/grouping/grouping-context.service';
import { IssueService } from '../core/services/issue.service';
import { LabelService } from '../core/services/label.service';
Expand Down Expand Up @@ -37,12 +35,6 @@ export class IssuesViewerComponent implements OnInit, AfterViewInit, OnDestroy {

popStateNavigationId: number;

/** Users to show as columns */
groups: Group[] = [];

/** The list of users with 0 issues (hidden) */
hiddenGroups: Group[] = [];

@ViewChildren(CardViewComponent) cardViews: QueryList<CardViewComponent>;

views = new BehaviorSubject<QueryList<CardViewComponent>>(undefined);
Expand All @@ -53,6 +45,7 @@ export class IssuesViewerComponent implements OnInit, AfterViewInit, OnDestroy {
public issueService: IssueService,
public labelService: LabelService,
public milestoneService: MilestoneService,
public groupService: GroupService,
public groupingContextService: GroupingContextService,
private router: Router,
private filtersService: FiltersService
Expand Down Expand Up @@ -105,54 +98,7 @@ export class IssuesViewerComponent implements OnInit, AfterViewInit, OnDestroy {
if (this.availableGroupsSubscription) {
this.availableGroupsSubscription.unsubscribe();
}

// Fetch assignees
this.groups = [];
this.hiddenGroups = [];

this.availableGroupsSubscription = this.groupingContextService.getGroups().subscribe((x) => (this.groups = x));
}

/**
* Checks if our current repository available on view service is indeed a valid repository
*/
private checkIfValidRepository() {
const currentRepo = this.viewService.currentRepo;

if (Repo.isInvalidRepoName(currentRepo)) {
return of(false);
}

return (
(this.githubService.isOrganisationPresent(currentRepo.owner) || this.githubService.isUsernamePresent(currentRepo.owner)) &&
this.githubService.isRepositoryPresent(currentRepo.owner, currentRepo.name)
);
}

/**
* Update the list of hidden group based on the new info.
* @param issueLength The number of issues under this group.
* @param target The group.
*/
updateHiddenGroups(issueLength: number, target: Group) {
// If the group is in the hidden list, add it if it has no issues.
// Also add it if it is unchecked in the filter.
if (issueLength === 0 && this.groupingContextService.isInHiddenList(target)) {
this.addToHiddenGroups(target);
} else {
this.removeFromHiddenGroups(target);
}
}

private addToHiddenGroups(target: Group) {
const isGroupPresent = this.hiddenGroups.some((group) => group.equals(target));

if (!isGroupPresent) {
this.hiddenGroups.push(target);
}
}

private removeFromHiddenGroups(target: Group) {
this.hiddenGroups = this.hiddenGroups.filter((group) => !group.equals(target));
this.groupService.resetGroups();
this.availableGroupsSubscription = this.groupingContextService.getGroups().subscribe((x) => (this.groupService.groups = x));
}
}

0 comments on commit c5999bf

Please sign in to comment.