Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer:50 #1255

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 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,24 +3,26 @@ import {
Component,
computed,
input,
linkedSignal,
numberAttribute,
} from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { FormsModule } from '@angular/forms';
import { RouterLink } from '@angular/router';
import { products } from './products';

@Component({
selector: 'app-order',
imports: [RouterLink, ReactiveFormsModule],
imports: [RouterLink, FormsModule],
template: `
<h2 class="mb-5 w-full bg-gray-400 p-2 text-white">Order</h2>
<section class="flex flex-col gap-5">
<form class="flex items-center justify-between gap-5" [formGroup]="form">
<form class="flex items-center justify-between gap-5">
<label for="countries" class="mb-2 block text-nowrap text-gray-900">
Select a quantity
</label>
<select
formControlName="quantity"
[(ngModel)]="selectedQuantity"
[ngModelOptions]="{ standalone: true }"
id="countries"
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">
<option value="1">1</option>
Expand All @@ -44,7 +46,7 @@ import { products } from './products';
</div>
<button
routerLink="/checkout"
[queryParams]="{ quantity: quantity() }"
[queryParams]="{ quantity: selectedQuantity() }"
queryParamsHandling="merge"
class="w-full rounded-full border bg-blue-500 p-2 text-white">
Checkout
Expand All @@ -54,18 +56,18 @@ import { products } from './products';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export default class OrderComponent {
form = new FormGroup({
quantity: new FormControl(1, { nonNullable: true }),
productId = input('1');
quantity = input<number, string>(1, {
transform: (v) => numberAttribute(v, 1),
});

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