challenge 53 big signal performance (#904)

* feat: challenge 52 big signal performance

* fix: update to 53
This commit is contained in:
Laforge Thomas
2024-06-11 17:28:14 +02:00
committed by GitHub
parent ecc7c8d36c
commit c02c41a606
28 changed files with 453 additions and 16 deletions

View File

@@ -0,0 +1,21 @@
import { CDFlashingDirective } from '@angular-challenges/shared/directives';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { UserStore } from './user.service';
@Component({
selector: 'address-user',
standalone: true,
template: `
<div cd-flash class="m-4 block border border-gray-500 p-4">
Address:
<div>Street: {{ userService.user().address.street }}</div>
<div>ZipCode: {{ userService.user().address.zipCode }}</div>
<div>City: {{ userService.user().address.city }}</div>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [CDFlashingDirective],
})
export class AddressComponent {
userService = inject(UserStore);
}

View File

@@ -0,0 +1,27 @@
import { Component } from '@angular/core';
import { AddressComponent } from './address.component';
import { JobComponent } from './job.component';
import { NameComponent } from './name.component';
import { NoteComponent } from './note.component';
import { UserFormComponent } from './user-form.component';
@Component({
standalone: true,
selector: 'app-root',
template: `
<name />
<address-user />
<job />
<note />
<user-form />
`,
styles: [''],
imports: [
JobComponent,
NameComponent,
AddressComponent,
NoteComponent,
UserFormComponent,
],
})
export class AppComponent {}

View File

@@ -0,0 +1,5 @@
import { ApplicationConfig } from '@angular/core';
export const appConfig: ApplicationConfig = {
providers: [],
};

View File

@@ -0,0 +1,20 @@
import { CDFlashingDirective } from '@angular-challenges/shared/directives';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { UserStore } from './user.service';
@Component({
selector: 'job',
standalone: true,
template: `
<div cd-flash class="m-4 block border border-gray-500 p-4">
Job:
<div>title: {{ userService.user().title }}</div>
<div>salary: {{ userService.user().salary }}</div>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [CDFlashingDirective],
})
export class JobComponent {
userService = inject(UserStore);
}

View File

@@ -0,0 +1,18 @@
import { CDFlashingDirective } from '@angular-challenges/shared/directives';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { UserStore } from './user.service';
@Component({
selector: 'name',
standalone: true,
template: `
<div cd-flash class="m-4 block border border-gray-500 p-4">
Name: {{ userService.user().name }}
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [CDFlashingDirective],
})
export class NameComponent {
userService = inject(UserStore);
}

View File

@@ -0,0 +1,18 @@
import { CDFlashingDirective } from '@angular-challenges/shared/directives';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { UserStore } from './user.service';
@Component({
selector: 'note',
standalone: true,
template: `
<div cd-flash class="m-4 block border border-gray-500 p-4">
Note: {{ userService.user().note }}
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [CDFlashingDirective],
})
export class NoteComponent {
userService = inject(UserStore);
}

View File

@@ -0,0 +1,100 @@
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { UserStore } from './user.service';
@Component({
selector: 'user-form',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="submit()" class="flex flex-col gap-4">
<div>
Name:
<input
class="rounded-md border border-gray-400"
formControlName="name" />
</div>
<div>
Address:
<div>
street:
<input
class="rounded-md border border-gray-400"
formControlName="street" />
</div>
<div>
zipCode:
<input
class="rounded-md border border-gray-400"
formControlName="zipCode" />
</div>
<div>
city:
<input
class="rounded-md border border-gray-400"
formControlName="city" />
</div>
</div>
<div>
note:
<input
class="rounded-md border border-gray-400"
formControlName="note" />
</div>
<div>
title:
<input
class="rounded-md border border-gray-400"
formControlName="title" />
</div>
<div>
salary:
<input
class="rounded-md border border-gray-400"
formControlName="salary" />
</div>
<button class="w-fit border p-2">Submit</button>
</form>
`,
host: {
class: 'block border border-gray-500 p-4 pt-10 m-4',
},
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserFormComponent {
userStore = inject(UserStore);
form = new FormGroup({
name: new FormControl(this.userStore.user().name, { nonNullable: true }),
street: new FormControl(this.userStore.user().address.street, {
nonNullable: true,
}),
zipCode: new FormControl(this.userStore.user().address.zipCode, {
nonNullable: true,
}),
city: new FormControl(this.userStore.user().address.city, {
nonNullable: true,
}),
note: new FormControl(this.userStore.user().note, { nonNullable: true }),
title: new FormControl(this.userStore.user().title, { nonNullable: true }),
salary: new FormControl(this.userStore.user().salary, {
nonNullable: true,
}),
});
submit() {
this.userStore.user.update((u) => ({
...u,
name: this.form.getRawValue().name,
address: {
...u.address,
street: this.form.getRawValue().street,
zipCode: this.form.getRawValue().zipCode,
city: this.form.getRawValue().city,
},
note: this.form.getRawValue().note,
title: this.form.getRawValue().title,
salary: this.form.getRawValue().salary,
}));
}
}

View File

@@ -0,0 +1,16 @@
import { Injectable, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class UserStore {
user = signal({
name: 'Bob',
address: {
street: '',
zipCode: '',
city: '',
},
note: '',
title: '',
salary: 0,
});
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>signal-big-signal-performance</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<app-root></app-root>
</body>
</html>

View File

@@ -0,0 +1,7 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { appConfig } from './app/app.config';
bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err),
);

View File

@@ -0,0 +1,5 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
/* You can add global styles to this file, and also import other style files */