feat: challenge 54 pipe obs signal (#903)

This commit is contained in:
Laforge Thomas
2024-06-17 20:51:27 +02:00
committed by GitHub
parent e6fd5468e2
commit d49cbc0de8
26 changed files with 417 additions and 25 deletions

View File

@@ -0,0 +1,31 @@
import { TableComponent } from '@angular-challenges/shared/ui';
import { AsyncPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { CurrencyPipe } from './currency.pipe';
import { ProductRowComponent } from './product-row.component';
import { products } from './product.model';
@Component({
standalone: true,
imports: [AsyncPipe, CurrencyPipe, TableComponent, ProductRowComponent],
selector: 'app-root',
template: `
<table [items]="products">
<ng-template #header>
<tr>
@for (col of displayedColumns; track col) {
<th>{{ col }}</th>
}
</tr>
</ng-template>
<ng-template #body let-product>
<tr product-row [product]="product"></tr>
</ng-template>
</table>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
products = products;
displayedColumns = ['name', 'priceA', 'priceB', 'priceC'];
}

View File

@@ -0,0 +1,15 @@
import { inject, Pipe, PipeTransform } from '@angular/core';
import { map, Observable } from 'rxjs';
import { CurrencyService } from './currency.service';
@Pipe({
name: 'currency',
standalone: true,
})
export class CurrencyPipe implements PipeTransform {
currencyService = inject(CurrencyService);
transform(price: number): Observable<string> {
return this.currencyService.symbol$.pipe(map((s) => `${price}${s}`));
}
}

View File

@@ -0,0 +1,30 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, map } from 'rxjs';
export interface Currency {
name: string;
code: string;
symbol: string;
}
export const currency: Currency[] = [
{ name: 'Euro', code: 'EUR', symbol: '€' },
{ name: 'Dollar US', code: 'USD', symbol: 'US$' },
{ name: 'Dollar Autralien', code: 'AUD', symbol: 'AU$' },
{ name: 'Livre Sterling', code: 'GBP', symbol: '£' },
{ name: 'Dollar Canadien', code: 'CAD', symbol: 'CAD' },
];
@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);
}
}

View File

@@ -0,0 +1,34 @@
import { AsyncPipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
Input,
} from '@angular/core';
import { CurrencyPipe } from './currency.pipe';
import { CurrencyService } from './currency.service';
import { Product } from './product.model';
@Component({
standalone: true,
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>
`,
imports: [AsyncPipe, CurrencyPipe],
providers: [CurrencyService],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductRowComponent {
protected productInfo!: Product;
@Input({ required: true }) set product(product: Product) {
this.currencyService.updateCode(product.currencyCode);
this.productInfo = product;
}
currencyService = inject(CurrencyService);
}

View File

@@ -0,0 +1,55 @@
export interface Product {
name: string;
priceA: number;
priceB: number;
priceC: number;
currencyCode: string;
}
export const products: Product[] = [
{
name: 'bike',
priceA: 1000,
priceB: 2000,
priceC: 2200,
currencyCode: 'USD',
},
{ name: 'tent', priceA: 112, priceB: 120, priceC: 41, currencyCode: 'EUR' },
{
name: 'sofa',
priceA: 500,
priceB: 422,
priceC: 5000,
currencyCode: 'EUR',
},
{
name: 'watch',
priceA: 50,
priceB: 130,
priceC: 150,
currencyCode: 'AUD',
},
{
name: 'computer',
priceA: 1000,
priceB: 2200,
priceC: 3500,
currencyCode: 'GBP',
},
{ name: 'mug', priceA: 10, priceB: 15, priceC: 20, currencyCode: 'EUR' },
{
name: 'headset',
priceA: 100,
priceB: 150,
priceC: 220,
currencyCode: 'CAD',
},
{ name: 'cable', priceA: 5, priceB: 10, priceC: 15, currencyCode: 'EUR' },
{
name: 'table',
priceA: 100,
priceB: 20,
priceC: 500,
currencyCode: 'EUR',
},
];

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>signal-pipe-observable-to-signal</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<app-root></app-root>
</body>
</html>

View File

@@ -0,0 +1,4 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

View File

@@ -0,0 +1,26 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
/* You can add global styles to this file, and also import other style files */
table {
width: 100%;
}
table thead > tr > th {
text-align: left;
padding: 1rem 1rem;
border: 1px solid #dee2e6;
border-width: 0 0 1px 0;
font-weight: 700;
color: #343a40;
background: #f8f9fa;
transition: box-shadow 0.2s;
}
table tbody > tr > td {
text-align: left;
border: 1px solid #dee2e6;
border-width: 0 0 1px 0;
padding: 1rem 1rem;
}