Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Label model #254

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/app/core/models/label.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@
* Represents a label and its attributes.
*/
export class Label implements SimpleLabel {
readonly category: string;
readonly name: string;
readonly formattedName: string; // 'category'.'name' (e.g. severity.Low) if a category exists or 'name' if the category does not exist.
color: string;
definition?: string;

constructor(label: { name: string; color: string; definition?: string }) {
const containsDotRegex = /\.\b/g; // contains dot in middle of name
[this.category, this.name] = containsDotRegex.test(label.name) ? label.name.split('.') : [undefined, label.name];
this.formattedName = this.category === undefined || this.category === '' ? this.name : this.category.concat('.', this.name);
this.name = label.name;
this.color = label.color;
this.definition = label.definition;
}

public equals(label: Label) {
return this.name === label.name && this.category === label.category;
}
}

/**
* Represents a simplified label with name and color
*/
export type SimpleLabel = {
formattedName: string;
name: string;
color: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,24 @@
<mat-list-option
#option
*ngFor="let label of this.labels$ | async"
[value]="label.formattedName"
[selected]="selectedLabelNames.includes(label.formattedName)"
[value]="label.name"
[selected]="selectedLabelNames.includes(label.name)"
class="list-option"
[class.hidden]="filter(input.value, label.name)"
>
<div class="flexbox-container">
<button
mat-icon-button
*ngIf="!hiddenLabelNames.has(label.formattedName)"
(click)="hide(label.formattedName); $event.stopPropagation()"
>
<button mat-icon-button *ngIf="!hiddenLabelNames.has(label.name)" (click)="hide(label.name); $event.stopPropagation()">
<mat-icon>visibility</mat-icon>
</button>
<button
mat-icon-button
*ngIf="hiddenLabelNames.has(label.formattedName)"
(click)="show(label.formattedName); $event.stopPropagation()"
>
<button mat-icon-button *ngIf="hiddenLabelNames.has(label.name)" (click)="show(label.name); $event.stopPropagation()">
<mat-icon>visibility_off</mat-icon>
</button>
<mat-chip
[ngStyle]="labelService.setLabelStyle(label.color)"
[disabled]="hiddenLabelNames.has(label.formattedName)"
[disabled]="hiddenLabelNames.has(label.name)"
(click)="simulateClick(option); $event.stopPropagation()"
>
{{ label.formattedName }}
{{ label.name }}
</mat-chip>
</div>
</mat-list-option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class LabelFilterBarComponent implements OnInit, AfterViewInit, OnDestroy
if (this.allLabels === undefined || this.allLabels.length === 0) {
return false;
}
return this.allLabels.some((label) => !this.filter(filter, label.formattedName));
return this.allLabels.some((label) => !this.filter(filter, label.name));
}

updateSelection(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, Input } from '@angular/core';
import { Label } from '../../../core/models/label.model';
import { GithubLabel } from '../../../core/models/github/github-label.model';
import { LabelService } from '../../../core/services/label.service';

@Component({
Expand All @@ -8,7 +8,7 @@ import { LabelService } from '../../../core/services/label.service';
styleUrls: ['./issue-pr-card-labels.component.css']
})
export class IssuePrCardLabelsComponent {
@Input() labels: Label[];
@Input() labelSet: Set<Label>;
@Input() labels: GithubLabel[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this is needed now that the name property of a Label is present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I clarify, do you mean that the type annotation for the field labels can be left as Label[]? The reason I changed the type annotation is because in the only place that this component is used, in issues-pr-card.component.html:

<mat-card-content>
  <app-issue-pr-card-milestone [milestone]="issue.milestone"></app-issue-pr-card-milestone>
  <app-issue-pr-card-labels [labels]="issue.githubLabels" [labelSet]="dropdownFilter?.hiddenLabels"></app-issue-pr-card-labels>
</mat-card-content>

The labels field is given issue.githubLabels, which is of type GithubLabel[].

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool 🎉

@Input() labelSet: Set<string>;
constructor(public labelService: LabelService) {}
}
Loading