RED-3800: update filter service
This commit is contained in:
parent
37ab9a6ed7
commit
e20ed84ca2
@ -1,10 +1,10 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, Observable, Subject } from 'rxjs';
|
||||
import { map, startWith, switchMapTo } from 'rxjs/operators';
|
||||
import { map, startWith, switchMap } from 'rxjs/operators';
|
||||
import { processFilters, toFlatFilters } from './filter-utils';
|
||||
import { IFilterGroup } from './models/filter-group.model';
|
||||
import { INestedFilter } from './models/nested-filter.model';
|
||||
import { get, shareDistinctLast, shareLast } from '../utils';
|
||||
import { any, get, shareDistinctLast, shareLast } from '../utils';
|
||||
import { NestedFilter } from './models/nested-filter';
|
||||
import { Filter } from './models/filter';
|
||||
import { IFilter } from './models/filter.model';
|
||||
@ -13,18 +13,22 @@ import { IFilter } from './models/filter.model';
|
||||
export class FilterService {
|
||||
readonly showResetFilters$: Observable<boolean>;
|
||||
readonly filterGroups$: Observable<IFilterGroup[]>;
|
||||
|
||||
readonly #singleFilters = new Map<string, BehaviorSubject<IFilter | undefined>>();
|
||||
private readonly _filterGroups$ = new BehaviorSubject<IFilterGroup[]>([]);
|
||||
private readonly _refresh$ = new Subject();
|
||||
readonly #filterGroups$ = new BehaviorSubject<IFilterGroup[]>([]);
|
||||
readonly #refresh$ = new Subject();
|
||||
|
||||
constructor() {
|
||||
this.filterGroups$ = this._refresh$.pipe(startWith(''), switchMapTo(this._filterGroups$.asObservable()), shareLast());
|
||||
|
||||
this.filterGroups$ = this.#refresh$.pipe(
|
||||
startWith(''),
|
||||
switchMap(() => this.#filterGroups$.asObservable()),
|
||||
shareLast(),
|
||||
);
|
||||
this.showResetFilters$ = this._showResetFilters$;
|
||||
}
|
||||
|
||||
get filterGroups(): IFilterGroup[] {
|
||||
return Object.values(this._filterGroups$.getValue());
|
||||
return Object.values(this.#filterGroups$.getValue());
|
||||
}
|
||||
|
||||
get enabledFlatFilters() {
|
||||
@ -38,13 +42,13 @@ export class FilterService {
|
||||
private get _showResetFilters$(): Observable<boolean> {
|
||||
return this.filterGroups$.pipe(
|
||||
map(value => toFlatFilters(value)),
|
||||
map(f => !!f.find(el => el.checked)),
|
||||
any(filter => !!filter.checked),
|
||||
shareDistinctLast(),
|
||||
);
|
||||
}
|
||||
|
||||
refresh(): void {
|
||||
this._refresh$.next(true);
|
||||
this.#refresh$.next(true);
|
||||
}
|
||||
|
||||
toggleFilter(filterGroupSlug: string, key: string, checkChildren = false): void {
|
||||
@ -75,11 +79,11 @@ export class FilterService {
|
||||
addFilterGroup(value: IFilterGroup): void {
|
||||
const oldFilters = this.getGroup(value.slug)?.filters;
|
||||
if (!oldFilters) {
|
||||
return this._filterGroups$.next([...this.filterGroups, value]);
|
||||
return this.#filterGroups$.next([...this.filterGroups, value]);
|
||||
}
|
||||
|
||||
const newGroup = { ...value, filters: processFilters(oldFilters, value.filters) };
|
||||
this._filterGroups$.next([...this.filterGroups.filter(f => f.slug !== newGroup.slug), newGroup]);
|
||||
this.#filterGroups$.next([...this.filterGroups.filter(f => f.slug !== newGroup.slug), newGroup]);
|
||||
}
|
||||
|
||||
addFilterGroups(values: IFilterGroup[], removeOldFilters = false): void {
|
||||
@ -93,7 +97,7 @@ export class FilterService {
|
||||
});
|
||||
|
||||
const filterSlugs = newFilters.map(f => f.slug);
|
||||
this._filterGroups$.next(
|
||||
this.#filterGroups$.next(
|
||||
removeOldFilters ? newFilters : [...this.filterGroups.filter(f => !filterSlugs.includes(f.slug)), ...newFilters],
|
||||
);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user