mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 12:53:03 -05:00
feat: challenge 54 pipe obs signal (#903)
This commit is contained in:
@@ -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'];
|
||||
}
|
||||
@@ -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}`));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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',
|
||||
},
|
||||
];
|
||||
BIN
apps/signal/54-pipe-observable-to-signal/src/favicon.ico
Normal file
BIN
apps/signal/54-pipe-observable-to-signal/src/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
13
apps/signal/54-pipe-observable-to-signal/src/index.html
Normal file
13
apps/signal/54-pipe-observable-to-signal/src/index.html
Normal 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>
|
||||
4
apps/signal/54-pipe-observable-to-signal/src/main.ts
Normal file
4
apps/signal/54-pipe-observable-to-signal/src/main.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { bootstrapApplication } from '@angular/platform-browser';
|
||||
import { AppComponent } from './app/app.component';
|
||||
|
||||
bootstrapApplication(AppComponent).catch((err) => console.error(err));
|
||||
26
apps/signal/54-pipe-observable-to-signal/src/styles.scss
Normal file
26
apps/signal/54-pipe-observable-to-signal/src/styles.scss
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user