@@ -3,24 +3,26 @@ import {
3
3
Component ,
4
4
computed ,
5
5
input ,
6
+ linkedSignal ,
7
+ numberAttribute ,
6
8
} from '@angular/core' ;
7
- import { toSignal } from '@angular/core/rxjs-interop' ;
8
- import { FormControl , FormGroup , ReactiveFormsModule } from '@angular/forms' ;
9
+ import { FormsModule } from '@angular/forms' ;
9
10
import { RouterLink } from '@angular/router' ;
10
11
import { products } from './products' ;
11
12
12
13
@Component ( {
13
14
selector : 'app-order' ,
14
- imports : [ RouterLink , ReactiveFormsModule ] ,
15
+ imports : [ RouterLink , FormsModule ] ,
15
16
template : `
16
17
<h2 class="mb-5 w-full bg-gray-400 p-2 text-white">Order</h2>
17
18
<section class="flex flex-col gap-5">
18
- <form class="flex items-center justify-between gap-5" [formGroup]="form" >
19
+ <form class="flex items-center justify-between gap-5">
19
20
<label for="countries" class="mb-2 block text-nowrap text-gray-900">
20
21
Select a quantity
21
22
</label>
22
23
<select
23
- formControlName="quantity"
24
+ [(ngModel)]="quantityValue"
25
+ [ngModelOptions]="{ standalone: true }"
24
26
id="countries"
25
27
class="block w-32 rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500">
26
28
<option value="1">1</option>
@@ -44,7 +46,7 @@ import { products } from './products';
44
46
</div>
45
47
<button
46
48
routerLink="/checkout"
47
- [queryParams]="{ quantity: quantity () }"
49
+ [queryParams]="{ quantity: quantityValue () }"
48
50
queryParamsHandling="merge"
49
51
class="w-full rounded-full border bg-blue-500 p-2 text-white">
50
52
Checkout
@@ -54,18 +56,24 @@ import { products } from './products';
54
56
changeDetection : ChangeDetectionStrategy . OnPush ,
55
57
} )
56
58
export default class OrderComponent {
57
- form = new FormGroup ( {
58
- quantity : new FormControl ( 1 , { nonNullable : true } ) ,
59
+ readonly productId = input ( '1' ) ;
60
+ readonly quantity = input < number , string > ( 1 , {
61
+ transform : ( v ) => numberAttribute ( v , 1 ) ,
59
62
} ) ;
60
63
61
- productId = input ( '1' ) ;
62
- price = computed (
64
+ readonly quantityValue = linkedSignal ( {
65
+ source : this . quantity ,
66
+ computation : ( source ) => ( source !== 1 ? source : 1 ) ,
67
+ } ) ;
68
+
69
+ protected readonly price = computed (
63
70
( ) => products . find ( ( p ) => p . id === this . productId ( ) ) ?. price ?? 0 ,
64
71
) ;
65
- quantity = toSignal ( this . form . controls . quantity . valueChanges , {
66
- initialValue : this . form . getRawValue ( ) . quantity ,
67
- } ) ;
68
- totalWithoutVat = computed ( ( ) => Number ( this . price ( ) ) * this . quantity ( ) ) ;
69
- vat = computed ( ( ) => this . totalWithoutVat ( ) * 0.21 ) ;
70
- total = computed ( ( ) => this . totalWithoutVat ( ) + this . vat ( ) ) ;
72
+ protected readonly totalWithoutVat = computed (
73
+ ( ) => Number ( this . price ( ) ) * this . quantityValue ( ) ,
74
+ ) ;
75
+ protected readonly vat = computed ( ( ) => this . totalWithoutVat ( ) * 0.21 ) ;
76
+ protected readonly total = computed (
77
+ ( ) => this . totalWithoutVat ( ) + this . vat ( ) ,
78
+ ) ;
71
79
}
0 commit comments