Skip to content

Commit ddb5036

Browse files
authored
Challenge 56 multi step reactive form and signals (tomalaforge#1030)
feat: challenge 55 form and signal
1 parent 45353aa commit ddb5036

26 files changed

+505
-17
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"extends": ["../../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts"],
7+
"extends": [
8+
"plugin:@nx/angular",
9+
"plugin:@angular-eslint/template/process-inline-templates"
10+
],
11+
"rules": {
12+
"@angular-eslint/directive-selector": [
13+
"error",
14+
{
15+
"type": "attribute",
16+
"prefix": "app",
17+
"style": "camelCase"
18+
}
19+
],
20+
"@angular-eslint/component-selector": [
21+
"error",
22+
{
23+
"type": "element",
24+
"prefix": "app",
25+
"style": "kebab-case"
26+
}
27+
]
28+
}
29+
},
30+
{
31+
"files": ["*.html"],
32+
"extends": ["plugin:@nx/angular-template"],
33+
"rules": {}
34+
}
35+
]
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# forms and signal
2+
3+
> author: thomas-laforge
4+
5+
### Run Application
6+
7+
```bash
8+
npx nx serve signal-forms-and-signal
9+
```
10+
11+
### Documentation and Instruction
12+
13+
Challenge documentation is [here](https://angular-challenges.vercel.app/challenges/signal/56-forms-and-signal/).
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"name": "signal-forms-and-signal",
3+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
4+
"projectType": "application",
5+
"prefix": "app",
6+
"sourceRoot": "apps/signal/56-forms-and-signal/src",
7+
"tags": [],
8+
"targets": {
9+
"build": {
10+
"executor": "@angular-devkit/build-angular:application",
11+
"outputs": ["{options.outputPath}"],
12+
"options": {
13+
"outputPath": "dist/apps/signal/56-forms-and-signal",
14+
"index": "apps/signal/56-forms-and-signal/src/index.html",
15+
"browser": "apps/signal/56-forms-and-signal/src/main.ts",
16+
"polyfills": ["zone.js"],
17+
"tsConfig": "apps/signal/56-forms-and-signal/tsconfig.app.json",
18+
"inlineStyleLanguage": "scss",
19+
"assets": [
20+
{
21+
"glob": "**/*",
22+
"input": "apps/signal/56-forms-and-signal/public"
23+
}
24+
],
25+
"styles": ["apps/signal/56-forms-and-signal/src/styles.scss"],
26+
"scripts": []
27+
},
28+
"configurations": {
29+
"production": {
30+
"budgets": [
31+
{
32+
"type": "initial",
33+
"maximumWarning": "500kb",
34+
"maximumError": "1mb"
35+
},
36+
{
37+
"type": "anyComponentStyle",
38+
"maximumWarning": "2kb",
39+
"maximumError": "4kb"
40+
}
41+
],
42+
"outputHashing": "all"
43+
},
44+
"development": {
45+
"optimization": false,
46+
"extractLicenses": false,
47+
"sourceMap": true
48+
}
49+
},
50+
"defaultConfiguration": "production"
51+
},
52+
"serve": {
53+
"executor": "@angular-devkit/build-angular:dev-server",
54+
"configurations": {
55+
"production": {
56+
"buildTarget": "signal-forms-and-signal:build:production"
57+
},
58+
"development": {
59+
"buildTarget": "signal-forms-and-signal:build:development"
60+
}
61+
},
62+
"defaultConfiguration": "development"
63+
},
64+
"extract-i18n": {
65+
"executor": "@angular-devkit/build-angular:extract-i18n",
66+
"options": {
67+
"buildTarget": "signal-forms-and-signal:build"
68+
}
69+
},
70+
"lint": {
71+
"executor": "@nx/eslint:lint"
72+
}
73+
}
74+
}
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Component } from '@angular/core';
2+
import { RouterOutlet } from '@angular/router';
3+
4+
@Component({
5+
standalone: true,
6+
imports: [RouterOutlet],
7+
selector: 'app-root',
8+
template: `
9+
<h1 class="text-3xl">Shop</h1>
10+
<div class="w-[500px] ">
11+
<router-outlet />
12+
</div>
13+
`,
14+
host: {
15+
class: 'w-full flex justify-center flex-col items-center p-4 gap-10',
16+
},
17+
})
18+
export class AppComponent {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
2+
import { provideRouter, withComponentInputBinding } from '@angular/router';
3+
import { routes } from './app.routes';
4+
5+
export const appConfig: ApplicationConfig = {
6+
providers: [
7+
provideZoneChangeDetection({ eventCoalescing: true }),
8+
provideRouter(routes, withComponentInputBinding()),
9+
],
10+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Routes } from '@angular/router';
2+
3+
export const routes: Routes = [
4+
{
5+
path: '',
6+
redirectTo: 'dashboard',
7+
pathMatch: 'full',
8+
},
9+
{
10+
path: 'dashboard',
11+
loadComponent: () => import('./dashboard.component'),
12+
},
13+
{
14+
path: 'order',
15+
loadComponent: () => import('./order.component'),
16+
},
17+
{
18+
path: 'checkout',
19+
loadComponent: () => import('./checkout.component'),
20+
},
21+
{
22+
path: 'payment',
23+
loadComponent: () => import('./payment.component'),
24+
},
25+
{
26+
path: '**',
27+
redirectTo: 'dashboard',
28+
},
29+
];
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
ChangeDetectionStrategy,
3+
Component,
4+
computed,
5+
input,
6+
} from '@angular/core';
7+
import { RouterLink } from '@angular/router';
8+
import { products } from './products';
9+
10+
@Component({
11+
selector: 'app-dashboard',
12+
standalone: true,
13+
imports: [RouterLink],
14+
template: `
15+
<h2 class="mb-1 w-full bg-gray-400 p-2 text-white">Checkout</h2>
16+
<button
17+
routerLink="/order"
18+
queryParamsHandling="merge"
19+
class="mb-5 text-blue-400">
20+
< back to order
21+
</button>
22+
<section class="mb-5 flex justify-between">
23+
<div class="font-bold">Your order:</div>
24+
<div>
25+
{{ quantity() }} x {{ product()?.name }}: {{ product()?.price }}€
26+
</div>
27+
</section>
28+
29+
<div>Billing Information</div>
30+
<div>...</div>
31+
<div>...</div>
32+
<div>...</div>
33+
<div>...</div>
34+
<div>...</div>
35+
36+
<button
37+
routerLink="/payment"
38+
[queryParams]="{ quantity: quantity() }"
39+
queryParamsHandling="merge"
40+
class="w-full rounded-full border bg-blue-500 p-2 text-white">
41+
Pay
42+
</button>
43+
`,
44+
changeDetection: ChangeDetectionStrategy.OnPush,
45+
})
46+
export default class DashboardComponent {
47+
quantity = input(1);
48+
productId = input('1');
49+
50+
product = computed(() =>
51+
products.find((product) => product.id === this.productId()),
52+
);
53+
totalWithVAT = computed(
54+
() => this.quantity() * (this.product()?.price ?? 0) * 1.21,
55+
);
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
import { RouterLink } from '@angular/router';
3+
import { products } from './products';
4+
5+
@Component({
6+
selector: 'app-dashboard',
7+
standalone: true,
8+
imports: [RouterLink],
9+
template: `
10+
<h2 class="mb-5 w-full bg-gray-400 p-2 text-white">List of Products</h2>
11+
<ul class="w-full *:border-b *:border-l *:border-r *:p-4">
12+
@for (product of products; track product.id) {
13+
<li [class.border-t]="$first">
14+
<div class="flex w-full justify-between">
15+
{{ product.name }} ({{ product.price }}€)
16+
<button
17+
class="w-20 rounded-full border bg-blue-500 p-2 text-white"
18+
routerLink="/order"
19+
[queryParams]="{ productId: product.id }">
20+
Buy
21+
</button>
22+
</div>
23+
</li>
24+
}
25+
</ul>
26+
`,
27+
changeDetection: ChangeDetectionStrategy.OnPush,
28+
})
29+
export default class DashboardComponent {
30+
products = products;
31+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
import { RouterLink } from '@angular/router';
3+
4+
@Component({
5+
selector: 'app-dashboard',
6+
standalone: true,
7+
imports: [RouterLink],
8+
template: `
9+
<h2 class="mb-1 w-full bg-green-700 p-2 text-white">Payment Success</h2>
10+
11+
<button
12+
routerLink="/dashboard"
13+
class="w-full rounded-full border bg-blue-500 p-2 text-white">
14+
Back to Shop
15+
</button>
16+
`,
17+
changeDetection: ChangeDetectionStrategy.OnPush,
18+
})
19+
export default class DashboardComponent {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const products = [
2+
{
3+
id: '1',
4+
name: 'Computer',
5+
price: 2000,
6+
},
7+
{
8+
id: '2',
9+
name: 'Mouse',
10+
price: 40,
11+
},
12+
{
13+
id: '3',
14+
name: 'Keyboard',
15+
price: 80,
16+
},
17+
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>signal-forms-and-signal</title>
6+
<base href="/" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1" />
8+
<link rel="icon" type="image/x-icon" href="favicon.ico" />
9+
</head>
10+
<body>
11+
<app-root></app-root>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)