From 39832bd21e0363bf77f4767b2adf408b2e2da551 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Thu, 24 Aug 2023 16:29:12 +0200 Subject: [PATCH] fix(admin-ui): Data table filters react to page navigation --- .../data-table-filter-collection.ts | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/packages/admin-ui/src/lib/core/src/providers/data-table/data-table-filter-collection.ts b/packages/admin-ui/src/lib/core/src/providers/data-table/data-table-filter-collection.ts index f305416fac..d18264745f 100644 --- a/packages/admin-ui/src/lib/core/src/providers/data-table/data-table-filter-collection.ts +++ b/packages/admin-ui/src/lib/core/src/providers/data-table/data-table-filter-collection.ts @@ -4,6 +4,7 @@ import { CustomFieldType } from '@vendure/common/lib/shared-types'; import { assertNever } from '@vendure/common/lib/shared-utils'; import extend from 'just-extend'; import { Subject } from 'rxjs'; +import { debounceTime, distinctUntilChanged, map, startWith } from 'rxjs/operators'; import { CustomFieldConfig, DateOperators, @@ -82,7 +83,7 @@ export class DataTableFilterCollection = #activeFilters: FilterWithValue[] = []; #valueChanges$ = new Subject(); #connectedToRouter = false; - valueChanges = this.#valueChanges$.asObservable(); + valueChanges = this.#valueChanges$.asObservable().pipe(debounceTime(10)); readonly #filtersQueryParamName = 'filters'; constructor(private router: Router) {} @@ -217,25 +218,41 @@ export class DataTableFilterCollection = } connectToRoute(route: ActivatedRoute) { - this.valueChanges.subscribe(value => { + this.valueChanges.subscribe(() => { this.router.navigate(['./'], { queryParams: { [this.#filtersQueryParamName]: this.serialize(), page: 1 }, relativeTo: route, queryParamsHandling: 'merge', }); }); - const filterQueryParams = (route.snapshot.queryParamMap.get(this.#filtersQueryParamName) ?? '') - .split(';') - .map(value => value.split(':')) - .map(([name, value]) => ({ name, value })); - for (const { name, value } of filterQueryParams) { - const filter = this.getFilter(name); - if (filter) { - const val = this.deserializeValue(filter, value); - this.#activeFilters.push(this.createFacetWithValue(filter, val)); - } - } + + route.queryParamMap + .pipe( + map(params => params.get(this.#filtersQueryParamName)), + distinctUntilChanged(), + startWith(route.snapshot.queryParamMap.get(this.#filtersQueryParamName) ?? ''), + ) + .subscribe(value => { + this.#activeFilters = []; + if (value === '') { + this.#valueChanges$.next(this.#activeFilters); + return; + } + const filterQueryParams = (value ?? '') + .split(';') + .map(value => value.split(':')) + .map(([name, value]) => ({ name, value })); + for (const { name, value } of filterQueryParams) { + const filter = this.getFilter(name); + if (filter) { + const val = this.deserializeValue(filter, value); + filter.activate(val); + } + } + }); + this.#connectedToRouter = true; + return this; } @@ -311,11 +328,11 @@ export class DataTableFilterCollection = } private onActivateFilter(filter: DataTableFilter, value: DataTableFilterValue) { - this.#activeFilters.push(this.createFacetWithValue(filter, value)); + this.#activeFilters.push(this.createFilterWithValue(filter, value)); this.#valueChanges$.next(this.#activeFilters); } - private createFacetWithValue( + private createFilterWithValue( filter: DataTableFilter, value: DataTableFilterValue, ) {