Skip to content

Commit

Permalink
Fix navigable list allowing pagination after navigation and preventin…
Browse files Browse the repository at this point in the history
…g filter to apply to children #158
  • Loading branch information
sambaptista committed Nov 23, 2021
1 parent b086bf6 commit 3b5c409
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
15 changes: 11 additions & 4 deletions projects/natural/src/lib/classes/abstract-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,18 @@ export class NaturalAbstractList<
/**
* Persist search and then launch whatever is required to refresh the list
*/
public search(naturalSearchSelections: NaturalSearchSelections, navigationExtras?: NavigationExtras): void {
public search(
naturalSearchSelections: NaturalSearchSelections,
navigationExtras?: NavigationExtras,
resetPagination = true,
): void {
// Reset page index to restart the pagination (preserve pageSize)
this.variablesManager.merge('pagination', {
pagination: pick(this.defaultPagination, ['offset', 'pageIndex']),
} as ExtractVall<TService>);

if (resetPagination) {
this.variablesManager.merge('pagination', {
pagination: pick(this.defaultPagination, ['offset', 'pageIndex']),
} as ExtractVall<TService>);
}

// Persist if activated
// Two parallel navigations conflict. We first persist the search, then the pagination
Expand Down
69 changes: 43 additions & 26 deletions projects/natural/src/lib/classes/abstract-navigable-list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// tslint:disable:directive-class-suffix
import {Directive, Injector, Input, OnDestroy, OnInit} from '@angular/core';
import {RouterLink} from '@angular/router';
import {NavigationExtras, RouterLink} from '@angular/router';
import {map, takeUntil} from 'rxjs/operators';
import {NaturalSearchSelections} from '../modules/search/types/values';
import {NaturalAbstractModelService} from '../services/abstract-model.service';
Expand Down Expand Up @@ -49,6 +49,8 @@ export class NaturalAbstractNavigableList<
*/
@Input() public ancestorRelationName = 'parent';

private oldAncertorId: string | null = null;

public breadcrumbs: BreadcrumbItem[] = [];

constructor(service: TService, injector: Injector) {
Expand All @@ -61,30 +63,35 @@ export class NaturalAbstractNavigableList<
// "na" is a trailing param, and should be considered only when there is no search
this.route.params.subscribe(params => {
// "ns" stands for natural-search to be shorter in url
if (!params['ns']) {
let navigationConditionValue: any | null = null;

// "na" stands for "navigation" (relation) in url
if (params['na']) {
navigationConditionValue = {have: {values: [params['na']]}};
this.service.getOne(params['na']).subscribe(
// TODO casting should disappear and instead this class should enforce
// the service to support Tone with a new generic
(ancestor: BreadcrumbItem) => (this.breadcrumbs = this.getBreadcrumb(ancestor)),
);
this.clearSearch();
} else {
navigationConditionValue = {empty: {}};
this.breadcrumbs = [];
}

const condition: Literal = {};
condition[this.ancestorRelationName] = navigationConditionValue;
const variables: QueryVariables = {filter: {groups: [{conditions: [condition]}]}};

// todo : check why without "as Vall" it errors. Vall is supposed to be QueryVariables, and filter too.
this.variablesManager.set('navigation', variables as ExtractVall<TService>);
if (params['ns']) {
return;
}

let navigationConditionValue: any | null = null;

// "na" stands for "navigation" (relation) in url
if (params['na']) {
navigationConditionValue = {have: {values: [params['na']]}};
this.service.getOne(params['na']).subscribe(
// TODO casting should disappear and instead this class should enforce
// the service to support Tone with a new generic
(ancestor: BreadcrumbItem) => (this.breadcrumbs = this.getBreadcrumb(ancestor)),
);

const hasAncestorChanged = params['na'] !== this.oldAncertorId;
this.oldAncertorId = params['na'];
this.clearSearch(hasAncestorChanged);
} else {
navigationConditionValue = {empty: {}};
this.breadcrumbs = [];
}

const condition: Literal = {};
condition[this.ancestorRelationName] = navigationConditionValue;
const variables: QueryVariables = {filter: {groups: [{conditions: [condition]}]}};

// todo : check why without "as Vall" it errors. Vall is supposed to be QueryVariables, and filter too.
this.variablesManager.set('navigation', variables as ExtractVall<TService>);
});

super.ngOnInit();
Expand Down Expand Up @@ -132,12 +139,22 @@ export class NaturalAbstractNavigableList<
super.translateSearchAndRefreshList(naturalSearchSelections);
}

public clearSearch(): void {
public clearSearch(resetPagination = true): void {
this.naturalSearchSelections = [[]];
this.search([[]]);
super.search([[]], undefined, resetPagination);
this.persistenceService.persistInStorage('ns', null, this.getStorageKey());
}

public search(
naturalSearchSelections: NaturalSearchSelections,
navigationExtras?: NavigationExtras,
resetPagination = true,
): void {
this.persistenceService.persistInUrl('na', null, this.route).then(() => {
super.search(naturalSearchSelections, navigationExtras, resetPagination);
});
}

/**
* Return an array for router link usage
*/
Expand Down

0 comments on commit 3b5c409

Please sign in to comment.