Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change breadcrumb array to observable with pipe async #75

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/xng-breadcrumb/src/lib/breadcrumb.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ol class="xng-breadcrumb-list">
<ng-container
*ngFor="
let breadcrumb of breadcrumbs;
let breadcrumb of breadcrumbs$ | async;
last as isLast;
first as isFirst;
index as index;
Expand Down
36 changes: 16 additions & 20 deletions libs/xng-breadcrumb/src/lib/breadcrumb.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ import {
OnInit,
TemplateRef,
ViewEncapsulation,
OnDestroy,
} from '@angular/core';
import { Router } from '@angular/router';
import { Observable, Subscription } from 'rxjs';
import { map } from 'rxjs/operators';
import { BreadcrumbItemDirective } from './breadcrumb-item.directive';
import { BreadcrumbService, BreadcrumbDefinition } from './breadcrumb.service';
import { BreadcrumbDefinition, BreadcrumbService } from './breadcrumb.service';

@Component({
selector: 'xng-breadcrumb',
templateUrl: './breadcrumb.component.html',
styleUrls: ['./breadcrumb.component.scss'],
encapsulation: ViewEncapsulation.None,
})
export class BreadcrumbComponent implements OnInit, OnDestroy {
export class BreadcrumbComponent implements OnInit {
subscription: Subscription;
breadcrumbs: BreadcrumbDefinition[];
breadcrumbs$: Observable<BreadcrumbDefinition[]>;
separatorTemplate: TemplateRef<void>;
private _separator = '/';
Expand Down Expand Up @@ -87,10 +86,18 @@ export class BreadcrumbComponent implements OnInit, OnDestroy {
) {}

ngOnInit() {
this.subscription = this.breadcrumbService.breadcrumbs$.subscribe(
(breadcrumbs) => {
this.breadcrumbs = breadcrumbs
.map((breadcrumb) => {
this.breadcrumbs$ = this.breadcrumbService.breadcrumbs$.pipe(
map((breadcrumbs: BreadcrumbDefinition[]) => {
return breadcrumbs
.filter((breadcrumb: BreadcrumbDefinition) => {
// Usually, breadcrumb list can contain a combination of auto generated and user specified labels
// this filters autogenerated labels in case of "[autoGenerate]: false"
if (this.autoGenerate) {
return true;
}
return !breadcrumb.isAutoGeneratedLabel;
})
.map((breadcrumb: BreadcrumbDefinition) => {
// Do not mutate breadcrumb as its source of truth.
// There can be scenarios where we can have multiple xng-breadcrumb instances in page
return {
Expand All @@ -100,21 +107,10 @@ export class BreadcrumbComponent implements OnInit, OnDestroy {
: undefined,
fragment: this.preserveFragment ? breadcrumb.fragment : undefined,
};
})
.filter((breadcrumb) => {
// Usually, breadcrumb list can contain a combination of auto generated and user specified labels
// this filters autogenerated labels in case of "[autoGenerate]: false"
if (this.autoGenerate) {
return true;
}
return !breadcrumb.isAutoGeneratedLabel;
});
}
})
);
}
ngOnDestroy() {
this.subscription.unsubscribe();
}

handleRoute(breadcrumb: BreadcrumbDefinition) {
const routeLink = breadcrumb.routeInterceptor
Expand Down