Skip to content

Commit a070d76

Browse files
committed
feat(challenge 54): refactor the application to be a fully signal-based
1 parent 543770b commit a070d76

4 files changed

Lines changed: 17 additions & 29 deletions

File tree

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { inject, Pipe, PipeTransform } from '@angular/core';
2-
import { map, Observable } from 'rxjs';
32
import { CurrencyService } from './currency.service';
43

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

11-
transform(price: number): Observable<string> {
12-
return this.currencyService.symbol$.pipe(map((s) => `${price}${s}`));
10+
transform(price: number, currencyCode: string): string {
11+
const symbol = this.currencyService.getSymbol(currencyCode);
12+
13+
return `${price}${symbol}`;
1314
}
1415
}
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Injectable } from '@angular/core';
2-
import { BehaviorSubject, map } from 'rxjs';
32

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

1817
@Injectable()
1918
export class CurrencyService {
20-
private code = new BehaviorSubject('EUR');
21-
22-
readonly code$ = this.code.asObservable();
23-
readonly symbol$ = this.code$.pipe(
24-
map((code) => currency.find((c) => c.code === code)?.symbol ?? code),
25-
);
26-
27-
public updateCode(code: string) {
28-
this.code.next(code);
19+
getSymbol(code: string): string {
20+
return currency.find((c) => c.code === code)?.symbol ?? code;
2921
}
3022
}
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { AsyncPipe } from '@angular/common';
21
import {
32
ChangeDetectionStrategy,
43
Component,
5-
inject,
6-
Input,
4+
computed,
5+
input,
76
} from '@angular/core';
87
import { CurrencyPipe } from './currency.pipe';
98
import { CurrencyService } from './currency.service';
@@ -13,22 +12,17 @@ import { Product } from './product.model';
1312
// eslint-disable-next-line @angular-eslint/component-selector
1413
selector: 'tr[product-row]',
1514
template: `
16-
<td>{{ productInfo.name }}</td>
17-
<td>{{ productInfo.priceA | currency | async }}</td>
18-
<td>{{ productInfo.priceB | currency | async }}</td>
19-
<td>{{ productInfo.priceC | currency | async }}</td>
15+
<td>{{ product().name }}</td>
16+
<td>{{ product().priceA | currency: currencyCode() }}</td>
17+
<td>{{ product().priceB | currency: currencyCode() }}</td>
18+
<td>{{ product().priceC | currency: currencyCode() }}</td>
2019
`,
21-
imports: [AsyncPipe, CurrencyPipe],
20+
imports: [CurrencyPipe],
2221
providers: [CurrencyService],
2322
changeDetection: ChangeDetectionStrategy.OnPush,
2423
})
2524
export class ProductRowComponent {
26-
protected productInfo!: Product;
25+
product = input.required<Product>();
2726

28-
@Input({ required: true }) set product(product: Product) {
29-
this.currencyService.updateCode(product.currencyCode);
30-
this.productInfo = product;
31-
}
32-
33-
currencyService = inject(CurrencyService);
27+
currencyCode = computed(() => this.product().currencyCode);
3428
}

libs/shared/ui/src/lib/table.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { NgTemplateOutlet } from '@angular/common';
12
import { Component, contentChild, input, TemplateRef } from '@angular/core';
23

34
@Component({
45
// eslint-disable-next-line @angular-eslint/component-selector
56
selector: 'table',
6-
imports: [],
7+
imports: [NgTemplateOutlet],
78
template: `
89
<thead>
910
<ng-container *ngTemplateOutlet="headerTemplate()"></ng-container>

0 commit comments

Comments
 (0)