mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-12 05:43:03 -05:00
feat(doc): move di
This commit is contained in:
54
apps/angular/di/src/app/app.component.ts
Normal file
54
apps/angular/di/src/app/app.component.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/* eslint-disable @angular-eslint/directive-selector */
|
||||
import { AsyncPipe, NgFor } from '@angular/common';
|
||||
import { Component, Directive } from '@angular/core';
|
||||
import { TableModule } from 'primeng/table';
|
||||
import { CurrencyPipe } from './currency.pipe';
|
||||
import { CurrencyService } from './currency.service';
|
||||
import { Product, products } from './product.model';
|
||||
|
||||
interface ProductContext {
|
||||
$implicit: Product;
|
||||
}
|
||||
|
||||
@Directive({
|
||||
selector: 'ng-template[pTemplate="body"]',
|
||||
standalone: true,
|
||||
})
|
||||
export class ProductDirective {
|
||||
static ngTemplateContextGuard(
|
||||
dir: ProductDirective,
|
||||
ctx: unknown
|
||||
): ctx is ProductContext {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
imports: [TableModule, CurrencyPipe, AsyncPipe, NgFor, ProductDirective],
|
||||
providers: [CurrencyService],
|
||||
selector: 'app-root',
|
||||
template: `
|
||||
<p-table [value]="products">
|
||||
<ng-template pTemplate="header">
|
||||
<tr>
|
||||
<th *ngFor="let col of displayedColumns">
|
||||
{{ col }}
|
||||
</th>
|
||||
</tr>
|
||||
</ng-template>
|
||||
<ng-template pTemplate="body" let-product>
|
||||
<tr>
|
||||
<td>{{ product.name }}</td>
|
||||
<td>{{ product.priceA | currency | async }}</td>
|
||||
<td>{{ product.priceB | currency | async }}</td>
|
||||
<td>{{ product.priceC | currency | async }}</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
</p-table>
|
||||
`,
|
||||
})
|
||||
export class AppComponent {
|
||||
products = products;
|
||||
displayedColumns = ['name', 'priceA', 'priceB', 'priceC'];
|
||||
}
|
||||
17
apps/angular/di/src/app/currency.pipe.ts
Normal file
17
apps/angular/di/src/app/currency.pipe.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { inject, Pipe, PipeTransform } from '@angular/core';
|
||||
import { map } from 'rxjs';
|
||||
import { CurrencyService } from './currency.service';
|
||||
|
||||
@Pipe({
|
||||
name: 'currency',
|
||||
standalone: true,
|
||||
})
|
||||
export class CurrencyPipe implements PipeTransform {
|
||||
currencyService = inject(CurrencyService);
|
||||
|
||||
transform(price: number) {
|
||||
return this.currencyService.symbol$.pipe(
|
||||
map((s) => `${String(price)}${s}`)
|
||||
);
|
||||
}
|
||||
}
|
||||
29
apps/angular/di/src/app/currency.service.ts
Normal file
29
apps/angular/di/src/app/currency.service.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ComponentStore } from '@ngrx/component-store';
|
||||
import { 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 extends ComponentStore<{ code: string }> {
|
||||
readonly code$ = this.select((state) => state.code);
|
||||
readonly symbol$ = this.code$.pipe(
|
||||
map((code) => currency.find((c) => c.code === code)?.symbol ?? code)
|
||||
);
|
||||
|
||||
constructor() {
|
||||
super({ code: 'EUR' });
|
||||
}
|
||||
}
|
||||
55
apps/angular/di/src/app/product.model.ts
Normal file
55
apps/angular/di/src/app/product.model.ts
Normal 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',
|
||||
},
|
||||
];
|
||||
Reference in New Issue
Block a user