AI Hints
Home›Angular Tutorial›Project: The Book Page, The Basket and Checkout Form
Angular · Lesson 117 of 120

Project: Checkout Form

Place the order.

⏱ 2 min read▶ Video at 2:19:47🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

The last page is checkout. The customer gives their name, and email, and places the order.

  • Name and email
  • Place the order
  • The last page

checkout.ts

Checkout uses a signal form. The model holds the name, and email. Both are required, and the email must look like one. Order works out the amount paid, thanks the customer, and clears the basket.

checkout.tstypescript
import { Component, inject, signal } from '@angular/core';
import { form, FormField, required, email } from '@angular/forms/signals';
import { Basket } from './basket';
@Component({
  selector: 'app-checkout', imports: [FormField], templateUrl: './checkout.html',
})
export class Checkout {
  basket = inject(Basket);
  done = signal('');
  model = signal({ name: '', email: '' });
  checkout = form(this.model, p => {
    required(p.name); required(p.email); email(p.email);
  });
  order() {
    const paid = this.basket.total().toFixed(2);
    this.done.set(`Thanks, ${this.model().name}! You paid £${paid}.`);
    this.basket.clear();
  }
}

Placing an order

The template shows the form, until the order is placed. Each input is connected with form field. The button stays disabled, while the form is invalid. We add Circe, open the basket, go to checkout, and fill in the form. Run it. The order was placed, for ten ninety nine. And the header shows an empty basket.

checkout.htmlhtml
<h2>Checkout</h2>
@if (done()) {
  <p class="card">{{ done() }}</p>
} @else {
  <input [formField]="checkout.name" placeholder="Name">
  <input [formField]="checkout.email" placeholder="Email">
  <p>To pay: £{{ basket.total().toFixed(2) }}</p>
  <button [disabled]="checkout().invalid()" (click)="order()">Place order</button>
}
Also uses book.ts
export interface Book {
  id: number;
  title: string;
  author: string;
  price: number;
}
Also uses basket.ts
import { Service, signal, computed } from '@angular/core';
import { Book } from './book';

@Service()
export class Basket {
  private readonly list = signal<Book[]>([]);
  readonly items = this.list.asReadonly();
  readonly count = computed(() => this.list().length);
  readonly total = computed(() => this.list().reduce((sum, b) => sum + b.price, 0));

  add(book: Book) { this.list.update(l => [...l, book]); }
  remove(index: number) { this.list.update(l => l.filter((_, i) => i !== index)); }
  clear() { this.list.set([]); }
}
Also uses app.routes.ts
import { Routes } from '@angular/router';
import { BookList } from './book-list';

export const routes: Routes = [
  { path: '', redirectTo: 'books', pathMatch: 'full' },
  { path: 'books', component: BookList },
  { path: 'books/:id', loadComponent: () => import('./book-page').then(m => m.BookPage) },
  { path: 'basket', loadComponent: () => import('./basket-page').then(m => m.BasketPage) },
  { path: 'checkout', loadComponent: () => import('./checkout').then(m => m.Checkout) },
  { path: '**', redirectTo: 'books' },
];
Also uses app.ts
import { Component, inject } from '@angular/core';
import { RouterOutlet, RouterLink } from '@angular/router';
import { Basket } from './basket';

@Component({
  selector: 'app-root',
  imports: [RouterOutlet, RouterLink],
  template: `
    <nav class="bar">
      <a id="home" routerLink="/books"><b>Page Turners</b></a>
      <a id="cart" routerLink="/basket">Basket ({{ basket.count() }})</a>
    </nav>
    <router-outlet />
  `,
  styles: `.bar { display: flex; justify-content: space-between; margin-bottom: 8px; }`,
})
export class App {
  basket = inject(Basket);
}
Also uses book-list.ts
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
import { RouterLink } from '@angular/router';
import { Book } from './book';

@Component({
  selector: 'app-book-list',
  imports: [RouterLink],
  template: `
    <input (input)="q.set($any($event.target).value)" placeholder="Search books">
    @for (b of books.value(); track b.id) {
      <p><a [routerLink]="['/books', b.id]">{{ b.title }}</a> · £{{ b.price }}</p>
    } @empty { <p class="muted">No books found.</p> }
  `,
})
export class BookList {
  q = signal('');
  books = httpResource<Book[]>(() => `/api/books?q=${this.q()}`, { defaultValue: [] });
}
Also uses book-page.ts
import { Component, inject, input } from '@angular/core';
import { httpResource } from '@angular/common/http';
import { Basket } from './basket';
import { Book } from './book';
@Component({
  selector: 'app-book-page',
  template: `
    @if (book.error()) { <p class="error">Book not found.</p> }
    @else if (book.value(); as b) {
      <h2>{{ b.title }}</h2>
      <p>by {{ b.author }} · £{{ b.price }} <button (click)="basket.add(b)">Add</button></p>
    }
  `,
})
export class BookPage {
  basket = inject(Basket);
  id = input.required<string>();
  book = httpResource<Book>(() => `/api/books/${this.id()}`);
}
Also uses basket-page.ts
import { Component, inject } from '@angular/core';
import { RouterLink } from '@angular/router';
import { Basket } from './basket';
@Component({
  selector: 'app-basket-page',
  imports: [RouterLink],
  template: `
    <h2>Your basket</h2>
    @for (b of basket.items(); track $index) {
      <p>{{ b.title }} · £{{ b.price }}
        <button (click)="basket.remove($index)">×</button></p>
    } @empty { <p>Your basket is empty.</p> }
    <p><b>Total: £{{ basket.total().toFixed(2) }}</b></p>
    @if (basket.count()) { <a id="checkout" routerLink="/checkout">Go to checkout</a> }
  `,
})
export class BasketPage {
  basket = inject(Basket);
}
Also uses checkout.ts
import { Component, inject, signal } from '@angular/core';
import { form, FormField, required, email } from '@angular/forms/signals';
import { Basket } from './basket';
@Component({
  selector: 'app-checkout', imports: [FormField], templateUrl: './checkout.html',
})
export class Checkout {
  basket = inject(Basket);
  done = signal('');
  model = signal({ name: '', email: '' });
  checkout = form(this.model, p => {
    required(p.name); required(p.email); email(p.email);
  });
  order() {
    const paid = this.basket.total().toFixed(2);
    this.done.set(`Thanks, ${this.model().name}! You paid £${paid}.`);
    this.basket.clear();
  }
}
Also uses actions.json
[{"click": "p:nth-of-type(3) a"}, {"wait": 700}, {"click": "app-book-page button"}, {"click": "#cart"}, {"wait": 700}, {"click": "#checkout"}, {"wait": 700}, {"type": "app-checkout input:nth-of-type(1)", "text": "Sam"}, {"type": "app-checkout input:nth-of-type(2)", "text": "sam@example.com"}, {"click": "app-checkout button"}]
Also uses _url
/books
Also uses _bar
1

Summary

Checkout uses a signal form, with rules, and places the order only when the form is valid. The shop is complete. Next, tests for the project.

Key takeaways
  • form(model, ...)
  • [formField]
  • order()
Next up: Project: Tests — prove it works