Skip to content

Commit

Permalink
fix(admin-ui): Fix back/forward nav issues with data table filters
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Oct 11, 2023
1 parent 342fafa commit 58cb5a5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
16 changes: 13 additions & 3 deletions packages/admin-ui/src/lib/core/src/common/base-list.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, inject, OnDestroy, OnInit } from '@angular/core';
import { DestroyRef, Directive, inject, OnDestroy, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { ActivatedRoute, QueryParamsHandling, Router } from '@angular/router';
import { ResultOf, TypedDocumentNode, VariablesOf } from '@graphql-typed-document-node/core';
Expand Down Expand Up @@ -212,8 +212,14 @@ export class TypedBaseListComponent<
protected router = inject(Router);
protected serverConfigService = inject(ServerConfigService);
private refreshStreams: Array<Observable<any>> = [];
private collections: Array<DataTableFilterCollection | DataTableSortCollection<any>> = [];
constructor() {
super(inject(Router), inject(ActivatedRoute));

const destroyRef = inject(DestroyRef);
destroyRef.onDestroy(() => {
this.collections.forEach(c => c.destroy());
});
}

protected configure(config: {
Expand Down Expand Up @@ -241,11 +247,15 @@ export class TypedBaseListComponent<
}

createFilterCollection(): DataTableFilterCollection<NonNullable<NonNullable<Vars['options']>['filter']>> {
return new DataTableFilterCollection<NonNullable<Vars['options']['filter']>>(this.router);
const collection = new DataTableFilterCollection<NonNullable<Vars['options']['filter']>>(this.router);
this.collections.push(collection);
return collection;
}

createSortCollection(): DataTableSortCollection<NonNullable<NonNullable<Vars['options']>['sort']>> {
return new DataTableSortCollection<NonNullable<Vars['options']['sort']>>(this.router);
const collection = new DataTableSortCollection<NonNullable<Vars['options']['sort']>>(this.router);
this.collections.push(collection);
return collection;
}

setLanguage(code: LanguageCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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 { debounceTime, distinctUntilChanged, map, startWith, takeUntil } from 'rxjs/operators';
import {
CustomFieldConfig,
DateOperators,
Expand All @@ -28,6 +28,7 @@ import {

export class FilterWithValue<Type extends DataTableFilterType = DataTableFilterType> {
private onUpdateFns = new Set<(value: DataTableFilterValue<Type>) => void>();

constructor(
public readonly filter: DataTableFilter<any, Type>,
public value: DataTableFilterValue<Type>,
Expand Down Expand Up @@ -85,6 +86,7 @@ export class DataTableFilterCollection<FilterInput extends Record<string, any> =
#connectedToRouter = false;
valueChanges = this.#valueChanges$.asObservable().pipe(debounceTime(10));
readonly #filtersQueryParamName = 'filters';
private readonly destroy$ = new Subject<void>();

constructor(private router: Router) {}

Expand All @@ -96,6 +98,11 @@ export class DataTableFilterCollection<FilterInput extends Record<string, any> =
return this.#activeFilters;
}

destroy() {
this.destroy$.next();
this.destroy$.complete();
}

addFilter<FilterType extends DataTableFilterType>(
config: DataTableFilterOptions<FilterInput, FilterType>,
): DataTableFilterCollection<FilterInput> {
Expand Down Expand Up @@ -218,7 +225,11 @@ export class DataTableFilterCollection<FilterInput extends Record<string, any> =
}

connectToRoute(route: ActivatedRoute) {
this.valueChanges.subscribe(() => {
this.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(val => {
const currentFilters = route.snapshot.queryParamMap.get(this.#filtersQueryParamName);
if (val.length === 0 && !currentFilters) {
return;
}
this.router.navigate(['./'], {
queryParams: { [this.#filtersQueryParamName]: this.serialize(), page: 1 },
relativeTo: route,
Expand All @@ -230,6 +241,7 @@ export class DataTableFilterCollection<FilterInput extends Record<string, any> =
map(params => params.get(this.#filtersQueryParamName)),
distinctUntilChanged(),
startWith(route.snapshot.queryParamMap.get(this.#filtersQueryParamName) ?? ''),
takeUntil(this.destroy$),
)
.subscribe(value => {
this.#activeFilters = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ActivatedRoute, Router } from '@angular/router';
import { CustomFieldType } from '@vendure/common/lib/shared-types';
import { assertNever } from '@vendure/common/lib/shared-utils';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { CustomFieldConfig } from '../../common/generated-types';
import { DataTableSort, DataTableSortOptions, DataTableSortOrder } from './data-table-sort';

Expand All @@ -15,13 +16,19 @@ export class DataTableSortCollection<
valueChanges = this.#valueChanges$.asObservable();
readonly #sortQueryParamName = 'sort';
#defaultSort: { name: keyof SortInput; sortOrder: DataTableSortOrder } | undefined;
private readonly destroy$ = new Subject<void>();

constructor(private router: Router) {}

get length(): number {
return this.#sorts.length;
}

destroy() {
this.destroy$.next();
this.destroy$.complete();
}

addSort<Name extends keyof SortInput>(
config: DataTableSortOptions<SortInput, Name>,
): DataTableSortCollection<SortInput, [...Names, Name]> {
Expand Down Expand Up @@ -80,7 +87,7 @@ export class DataTableSortCollection<
}

connectToRoute(route: ActivatedRoute) {
this.valueChanges.subscribe(value => {
this.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.router.navigate(['./'], {
queryParams: { [this.#sortQueryParamName]: this.serialize() },
relativeTo: route,
Expand Down

0 comments on commit 58cb5a5

Please sign in to comment.