Skip to content

Commit

Permalink
show create icon only if not existing
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Jan 27, 2025
1 parent 92bd19a commit 3c930c9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
(opened)="setUnits()"
[displayWith]="displayFn"
>
<mat-option (click)="createNew()" *ngIf="(this.filteredUnitOptions | async)?.length === 0">Create new</mat-option>
<mat-option (click)="createNewUnit()" *ngIf="canCreate((this.filteredUnitOptions | async)!)">Create new</mat-option>

<mat-option *ngFor="let unit of this.filteredUnitOptions | async" [value]="unit">
{{unit.unitName}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class KeyResultTypeComponent implements AfterContentInit {
}), map((value) => this._filter(value || '')));
}

createNew() {
createNewUnit() {
const newUnit = { unitName: this.unitSearchTerm };
this.unitOptions.push(newUnit);
this.keyResultForm.get('metric')
Expand All @@ -166,7 +166,6 @@ export class KeyResultTypeComponent implements AfterContentInit {
}

private _filter(value: string): Unit[] {

Check failure on line 168 in frontend/src/app/components/key-result-type/key-result-type.component.ts

View workflow job for this annotation

GitHub Actions / frontend

Class Method name `_filter` must match one of the following formats: camelCase
console.log(value === '');
const filterValue = value.toString()
.toLowerCase();
return this.unitOptions.filter((option) => option.unitName.toLowerCase()
Expand All @@ -182,5 +181,16 @@ export class KeyResultTypeComponent implements AfterContentInit {
?.updateValueAndValidity();
});
}

canCreate(options: Unit[]) {
const rawValue = this.keyResultForm.get('metric')
?.get('unit')
?.getRawValue();
const value = rawValue?.unitName || rawValue;
const doesSearchAlreadyExist = this.unitOptions.some((option) => option.unitName.toLowerCase()
.trim() === value.toLowerCase()
.trim());
return options.length === 0 && !doesSearchAlreadyExist;
}
}

0 comments on commit 3c930c9

Please sign in to comment.