-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: product context access directive (#605)
- Loading branch information
Showing
17 changed files
with
81 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/app/core/directives/product-context-access.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Directive, EmbeddedViewRef, OnDestroy, TemplateRef, ViewContainerRef } from '@angular/core'; | ||
import { Subject } from 'rxjs'; | ||
import { takeUntil } from 'rxjs/operators'; | ||
|
||
import { ProductContext, ProductContextFacade } from 'ish-core/facades/product-context.facade'; | ||
|
||
type ProductContextAccessContext = ProductContext & { context: ProductContextFacade }; | ||
|
||
@Directive({ | ||
selector: '[ishProductContextAccess]', | ||
}) | ||
export class ProductContextAccessDirective implements OnDestroy { | ||
private view: EmbeddedViewRef<ProductContextAccessContext>; | ||
private destroy$ = new Subject(); | ||
|
||
constructor( | ||
context: ProductContextFacade, | ||
viewContainer: ViewContainerRef, | ||
template: TemplateRef<ProductContextAccessContext> | ||
) { | ||
context | ||
.select() | ||
.pipe(takeUntil(this.destroy$)) | ||
.subscribe(ctx => { | ||
if (!this.view && ctx?.product) { | ||
this.view = viewContainer.createEmbeddedView(template, { ...ctx, context }); | ||
} else if (this.view && ctx?.product) { | ||
Object.keys(ctx).forEach(key => { | ||
this.view.context[key] = ctx[key]; | ||
}); | ||
} | ||
|
||
if (this.view) { | ||
this.view.markForCheck(); | ||
} | ||
}); | ||
} | ||
|
||
static ngTemplateContextGuard(_: ProductContextAccessDirective, ctx: unknown): ctx is ProductContextAccessContext { | ||
return !!ctx || true; | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroy$.next(); | ||
this.destroy$.complete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...detail/account-wishlist-detail-line-item/account-wishlist-detail-line-item.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
src/app/pages/product/product-brand/product-brand.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<div *ngIf="manufacturer$ | async as manufacturer" class="product-brand"> | ||
<a [routerLink]="['/search', manufacturer]"> | ||
<span itemprop="brand">{{ manufacturer }}</span> | ||
</a> | ||
</div> | ||
<ng-container *ishProductContextAccess="let product = product"> | ||
<div *ngIf="product.manufacturer as manufacturer" class="product-brand"> | ||
<a [routerLink]="['/search', manufacturer]"> | ||
<span itemprop="brand">{{ manufacturer }}</span> | ||
</a> | ||
</div> | ||
</ng-container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 2 additions & 13 deletions
15
src/app/pages/product/product-brand/product-brand.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,8 @@ | ||
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { ProductContextFacade } from 'ish-core/facades/product-context.facade'; | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'ish-product-brand', | ||
templateUrl: './product-brand.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class ProductBrandComponent implements OnInit { | ||
manufacturer$: Observable<string>; | ||
|
||
constructor(private context: ProductContextFacade) {} | ||
|
||
ngOnInit() { | ||
this.manufacturer$ = this.context.select('product', 'manufacturer'); | ||
} | ||
} | ||
export class ProductBrandComponent {} |