Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { inject, Pipe, PipeTransform } from '@angular/core';
import { map, Observable } from 'rxjs';
import { CurrencyService } from './currency.service';

@Pipe({
Expand All @@ -8,7 +7,9 @@ import { CurrencyService } from './currency.service';
export class CurrencyPipe implements PipeTransform {
currencyService = inject(CurrencyService);

transform(price: number): Observable<string> {
return this.currencyService.symbol$.pipe(map((s) => `${price}${s}`));
transform(price: number, currencyCode: string): string {
const symbol = this.currencyService.getSymbol(currencyCode);

return `${price}${symbol}`;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, map } from 'rxjs';

export interface Currency {
name: string;
Expand All @@ -17,14 +16,7 @@ export const currency: Currency[] = [

@Injectable()
export class CurrencyService {
private code = new BehaviorSubject('EUR');

readonly code$ = this.code.asObservable();
readonly symbol$ = this.code$.pipe(
map((code) => currency.find((c) => c.code === code)?.symbol ?? code),
);

public updateCode(code: string) {
this.code.next(code);
getSymbol(code: string): string {
return currency.find((c) => c.code === code)?.symbol ?? code;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { AsyncPipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
Input,
computed,
input,
} from '@angular/core';
import { CurrencyPipe } from './currency.pipe';
import { CurrencyService } from './currency.service';
Expand All @@ -13,22 +12,17 @@ import { Product } from './product.model';
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'tr[product-row]',
template: `
<td>{{ productInfo.name }}</td>
<td>{{ productInfo.priceA | currency | async }}</td>
<td>{{ productInfo.priceB | currency | async }}</td>
<td>{{ productInfo.priceC | currency | async }}</td>
<td>{{ product().name }}</td>
<td>{{ product().priceA | currency: currencyCode() }}</td>
<td>{{ product().priceB | currency: currencyCode() }}</td>
<td>{{ product().priceC | currency: currencyCode() }}</td>
`,
imports: [AsyncPipe, CurrencyPipe],
imports: [CurrencyPipe],
providers: [CurrencyService],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductRowComponent {
protected productInfo!: Product;
product = input.required<Product>();

@Input({ required: true }) set product(product: Product) {
this.currencyService.updateCode(product.currencyCode);
this.productInfo = product;
}

currencyService = inject(CurrencyService);
currencyCode = computed(() => this.product().currencyCode);
}
3 changes: 2 additions & 1 deletion libs/shared/ui/src/lib/table.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { NgTemplateOutlet } from '@angular/common';
import { Component, contentChild, input, TemplateRef } from '@angular/core';

@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'table',
imports: [],
imports: [NgTemplateOutlet],
template: `
<thead>
<ng-container *ngTemplateOutlet="headerTemplate()"></ng-container>
Expand Down
Loading