Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions apps/signal/56-forms-and-signal/src/app/order.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Component,
computed,
input,
Input,
} from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
Expand Down Expand Up @@ -44,7 +45,7 @@ import { products } from './products';
</div>
<button
routerLink="/checkout"
[queryParams]="{ quantity: quantity() }"
[queryParams]="{ quantity: quantityInternal() }"
queryParamsHandling="merge"
class="w-full rounded-full border bg-blue-500 p-2 text-white">
Checkout
Expand All @@ -58,14 +59,24 @@ export default class OrderComponent {
quantity: new FormControl(1, { nonNullable: true }),
});

@Input() set quantity(value: number) {
if (!value) {
return;
}

this.form.controls.quantity.patchValue(value);
}

productId = input('1');
price = computed(
() => products.find((p) => p.id === this.productId())?.price ?? 0,
);
quantity = toSignal(this.form.controls.quantity.valueChanges, {
quantityInternal = toSignal(this.form.controls.quantity.valueChanges, {
initialValue: this.form.getRawValue().quantity,
});
totalWithoutVat = computed(() => Number(this.price()) * this.quantity());
totalWithoutVat = computed(
() => Number(this.price()) * this.quantityInternal(),
);
vat = computed(() => this.totalWithoutVat() * 0.21);
total = computed(() => this.totalWithoutVat() + this.vat());
}