Skip to content

Commit 608eac2

Browse files
committed
feat: 50 bug in effect qbeeck
1 parent 95899e2 commit 608eac2

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

apps/signal/56-forms-and-signal/src/app/order.component.ts

+24-16
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@ import {
33
Component,
44
computed,
55
input,
6+
linkedSignal,
7+
numberAttribute,
68
} 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';
910
import { RouterLink } from '@angular/router';
1011
import { products } from './products';
1112

1213
@Component({
1314
selector: 'app-order',
14-
imports: [RouterLink, ReactiveFormsModule],
15+
imports: [RouterLink, FormsModule],
1516
template: `
1617
<h2 class="mb-5 w-full bg-gray-400 p-2 text-white">Order</h2>
1718
<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">
1920
<label for="countries" class="mb-2 block text-nowrap text-gray-900">
2021
Select a quantity
2122
</label>
2223
<select
23-
formControlName="quantity"
24+
[(ngModel)]="quantityValue"
25+
[ngModelOptions]="{ standalone: true }"
2426
id="countries"
2527
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">
2628
<option value="1">1</option>
@@ -44,7 +46,7 @@ import { products } from './products';
4446
</div>
4547
<button
4648
routerLink="/checkout"
47-
[queryParams]="{ quantity: quantity() }"
49+
[queryParams]="{ quantity: quantityValue() }"
4850
queryParamsHandling="merge"
4951
class="w-full rounded-full border bg-blue-500 p-2 text-white">
5052
Checkout
@@ -54,18 +56,24 @@ import { products } from './products';
5456
changeDetection: ChangeDetectionStrategy.OnPush,
5557
})
5658
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),
5962
});
6063

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(
6370
() => products.find((p) => p.id === this.productId())?.price ?? 0,
6471
);
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+
);
7179
}

0 commit comments

Comments
 (0)