|  | 
|  | 1 | +import { | 
|  | 2 | + ChangeDetectionStrategy, | 
|  | 3 | + Component, | 
|  | 4 | + computed, | 
|  | 5 | + input, | 
|  | 6 | +} from '@angular/core'; | 
|  | 7 | +import { toSignal } from '@angular/core/rxjs-interop'; | 
|  | 8 | +import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; | 
|  | 9 | +import { RouterLink } from '@angular/router'; | 
|  | 10 | +import { products } from './products'; | 
|  | 11 | + | 
|  | 12 | +@Component({ | 
|  | 13 | + selector: 'app-order', | 
|  | 14 | + standalone: true, | 
|  | 15 | + imports: [RouterLink, ReactiveFormsModule], | 
|  | 16 | + template: ` | 
|  | 17 | + <h2 class="mb-5 w-full bg-gray-400 p-2 text-white">Order</h2> | 
|  | 18 | + <section class="flex flex-col gap-5"> | 
|  | 19 | + <form class="flex items-center justify-between gap-5" [formGroup]="form"> | 
|  | 20 | + <label for="countries" class="mb-2 block text-nowrap text-gray-900"> | 
|  | 21 | + Select a quantity | 
|  | 22 | + </label> | 
|  | 23 | + <select | 
|  | 24 | + formControlName="quantity" | 
|  | 25 | + id="countries" | 
|  | 26 | + 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"> | 
|  | 27 | + <option value="1">1</option> | 
|  | 28 | + <option value="2">2</option> | 
|  | 29 | + <option value="3">3</option> | 
|  | 30 | + <option value="4">4</option> | 
|  | 31 | + <option value="5">5</option> | 
|  | 32 | + </select> | 
|  | 33 | + </form> | 
|  | 34 | + <div class="flex justify-between"> | 
|  | 35 | + <div>SubTotal</div> | 
|  | 36 | + <div>{{ totalWihoutVat() }} €</div> | 
|  | 37 | + </div> | 
|  | 38 | + <div class="flex justify-between"> | 
|  | 39 | + <div>VAT (21%)</div> | 
|  | 40 | + <div>{{ vat() }} €</div> | 
|  | 41 | + </div> | 
|  | 42 | + <div class="flex justify-between"> | 
|  | 43 | + <div>Total</div> | 
|  | 44 | + <div>{{ total() }} €</div> | 
|  | 45 | + </div> | 
|  | 46 | + <button | 
|  | 47 | + routerLink="/checkout" | 
|  | 48 | + [queryParams]="{ quantity: quantity() }" | 
|  | 49 | + queryParamsHandling="merge" | 
|  | 50 | + class="w-full rounded-full border bg-blue-500 p-2 text-white"> | 
|  | 51 | + Checkout | 
|  | 52 | + </button> | 
|  | 53 | + </section> | 
|  | 54 | + `, | 
|  | 55 | + changeDetection: ChangeDetectionStrategy.OnPush, | 
|  | 56 | +}) | 
|  | 57 | +export default class OrderComponent { | 
|  | 58 | + form = new FormGroup({ | 
|  | 59 | + quantity: new FormControl(1, { nonNullable: true }), | 
|  | 60 | + }); | 
|  | 61 | + | 
|  | 62 | + productId = input('1'); | 
|  | 63 | + price = computed( | 
|  | 64 | + () => products.find((p) => p.id === this.productId())?.price ?? 0, | 
|  | 65 | + ); | 
|  | 66 | + quantity = toSignal(this.form.controls.quantity.valueChanges, { | 
|  | 67 | + initialValue: this.form.getRawValue().quantity, | 
|  | 68 | + }); | 
|  | 69 | + totalWihoutVat = computed(() => Number(this.price()) * this.quantity()); | 
|  | 70 | + vat = computed(() => this.totalWihoutVat() * 0.21); | 
|  | 71 | + total = computed(() => this.totalWihoutVat() + this.vat()); | 
|  | 72 | +} | 
0 commit comments