Skip to content

Commit d7893bd

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

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

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

+15-13
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)]="selectedQuantity"
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: selectedQuantity() }"
4850
queryParamsHandling="merge"
4951
class="w-full rounded-full border bg-blue-500 p-2 text-white">
5052
Checkout
@@ -54,18 +56,18 @@ 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+
productId = input('1');
60+
quantity = input<number, string>(1, {
61+
transform: (v) => numberAttribute(v, 1),
5962
});
6063

61-
productId = input('1');
64+
selectedQuantity = linkedSignal(() => this.quantity());
6265
price = computed(
6366
() => products.find((p) => p.id === this.productId())?.price ?? 0,
6467
);
65-
quantity = toSignal(this.form.controls.quantity.valueChanges, {
66-
initialValue: this.form.getRawValue().quantity,
67-
});
68-
totalWithoutVat = computed(() => Number(this.price()) * this.quantity());
68+
totalWithoutVat = computed(
69+
() => Number(this.price()) * this.selectedQuantity(),
70+
);
6971
vat = computed(() => this.totalWithoutVat() * 0.21);
7072
total = computed(() => this.totalWithoutVat() + this.vat());
7173
}

0 commit comments

Comments
 (0)