All files / src/app/shared/components/filter/filter-dropdown filter-dropdown.component.ts

94.11% Statements 16/17
70% Branches 7/10
83.33% Functions 5/6
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 501x 1x                                     1x   6x   6x 6x     3x   5x 3x           2x       1x 1x 1x 1x          
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output } from '@angular/core';
import { FormControl } from '@angular/forms';
 
import { Facet } from 'ish-core/models/facet/facet.model';
import { Filter } from 'ish-core/models/filter/filter.model';
import { URLFormParams } from 'ish-core/utils/url-form-params';
 
/**
 * The Filter Dropdown Component displays a filter group. The items of the filter group are presented as a dropdown menu.
 *
 * @example
 * <ish-filter-dropdown
 *   [filterElement]="element"
 *   (applyFilter)="applyFilter($event)" />
 */
@Component({
  selector: 'ish-filter-dropdown',
  templateUrl: './filter-dropdown.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FilterDropdownComponent implements OnChanges {
  @Input({ required: true }) filterElement: Filter;
  @Output() readonly applyFilter = new EventEmitter<{ searchParameter: URLFormParams }>();
 
  selectedFacetsControl = new FormControl<Facet | Facet[]>([], { nonNullable: true });
  isMultiSelect = true;
 
  ngOnChanges() {
    this.isMultiSelect = this.filterElement.selectionType !== 'single';
 
    const selectedFacets = this.filterElement.facets.filter(x => x.selected);
    this.selectedFacetsControl.setValue(this.isMultiSelect ? selectedFacets : (selectedFacets[0] ?? undefined), {
      emitEvent: false,
    });
  }
 
  apply(facet: Facet) {
    this.applyFilter.emit({ searchParameter: facet.searchParameter });
  }
 
  onSingleChange(facet: Facet) {
    Eif (!this.isMultiSelect) {
      const facetToApply = facet ?? this.filterElement.facets.find(f => f.selected);
      Eif (facetToApply) {
        this.apply(facetToApply);
      }
    }
  }
}