-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Answer:65 #1464
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
Open
phhien203
wants to merge
2
commits into
tomalaforge:main
Choose a base branch
from
phhien203:phhien203-signal-forms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Answer:65 #1464
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,20 @@ | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| effect, | ||
| inject, | ||
| input, | ||
| linkedSignal, | ||
| } from '@angular/core'; | ||
| import { rxResource } from '@angular/core/rxjs-interop'; | ||
| import { | ||
| FormControl, | ||
| FormGroup, | ||
| ReactiveFormsModule, | ||
| Validators, | ||
| } from '@angular/forms'; | ||
| import { form, FormField, min, required, submit } from '@angular/forms/signals'; | ||
| import { Router } from '@angular/router'; | ||
| import { of } from 'rxjs'; | ||
| import { lastValueFrom, of } from 'rxjs'; | ||
| import { FakeBackendService } from './fake-backend.service'; | ||
| import { User } from './user.model'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-user-form', | ||
| imports: [ReactiveFormsModule], | ||
| imports: [FormField], | ||
| template: ` | ||
| <div class="rounded-lg border border-gray-200 bg-white p-6 shadow-sm"> | ||
| <h2 class="mb-4 text-xl font-semibold text-gray-800"> | ||
|
|
@@ -30,7 +26,7 @@ import { FakeBackendService } from './fake-backend.service'; | |
| class="h-8 w-8 animate-spin rounded-full border-4 border-indigo-500 border-t-transparent"></div> | ||
| </div> | ||
| } @else { | ||
| <form [formGroup]="userForm" (ngSubmit)="onSubmit()" class="space-y-4"> | ||
| <form (submit)="onSubmit($event)" class="space-y-4"> | ||
| <div> | ||
| <label | ||
| for="firstname" | ||
|
|
@@ -40,15 +36,18 @@ import { FakeBackendService } from './fake-backend.service'; | |
| <input | ||
| id="firstname" | ||
| type="text" | ||
| formControlName="firstname" | ||
| class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm" /> | ||
| class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 focus:outline-none sm:text-sm" | ||
| [formField]="userSignalForm.firstname" /> | ||
| @if ( | ||
| userForm.get('firstname')?.invalid && | ||
| userForm.get('firstname')?.touched | ||
| userSignalForm.firstname().invalid() && | ||
| userSignalForm.firstname().touched() | ||
| ) { | ||
| <p class="mt-1 text-xs text-red-500">Firstname is required</p> | ||
| @for (error of userSignalForm.firstname().errors(); track error) { | ||
| <p class="mt-1 text-xs text-red-500">{{ error.message }}</p> | ||
| } | ||
| } | ||
| </div> | ||
|
|
||
| <div> | ||
| <label | ||
| for="lastname" | ||
|
|
@@ -58,49 +57,59 @@ import { FakeBackendService } from './fake-backend.service'; | |
| <input | ||
| id="lastname" | ||
| type="text" | ||
| formControlName="lastname" | ||
| class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm" /> | ||
| class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 focus:outline-none sm:text-sm" | ||
| [formField]="userSignalForm.lastname" /> | ||
| @if ( | ||
| userForm.get('lastname')?.invalid && | ||
| userForm.get('lastname')?.touched | ||
| userSignalForm.lastname().invalid() && | ||
| userSignalForm.lastname().touched() | ||
| ) { | ||
| <p class="mt-1 text-xs text-red-500">Lastname is required</p> | ||
| @for (error of userSignalForm.lastname().errors(); track error) { | ||
| <p class="mt-1 text-xs text-red-500">{{ error.message }}</p> | ||
| } | ||
| } | ||
| </div> | ||
|
|
||
| <div> | ||
| <label for="age" class="block text-sm font-medium text-gray-700"> | ||
| Age | ||
| </label> | ||
| <input | ||
| id="age" | ||
| type="number" | ||
| formControlName="age" | ||
| class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm" /> | ||
| @if (userForm.get('age')?.invalid && userForm.get('age')?.touched) { | ||
| <p class="mt-1 text-xs text-red-500">Age must be positive</p> | ||
| class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 focus:outline-none sm:text-sm" | ||
| [formField]="userSignalForm.age" /> | ||
| @if ( | ||
| userSignalForm.age().invalid() && userSignalForm.age().touched() | ||
| ) { | ||
| @for (error of userSignalForm.age().errors(); track error) { | ||
| <p class="mt-1 text-xs text-red-500">{{ error.message }}</p> | ||
| } | ||
| } | ||
| </div> | ||
|
|
||
| <div> | ||
| <label for="grade" class="block text-sm font-medium text-gray-700"> | ||
| Grade | ||
| </label> | ||
| <input | ||
| id="grade" | ||
| type="number" | ||
| formControlName="grade" | ||
| class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm" /> | ||
| class="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 focus:outline-none sm:text-sm" | ||
| [formField]="userSignalForm.grade" /> | ||
| </div> | ||
|
|
||
| <div class="flex justify-end space-x-3 pt-4"> | ||
| <button | ||
| type="button" | ||
| (click)="onCancel()" | ||
| class="rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"> | ||
| class="rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 focus:outline-none"> | ||
| Cancel | ||
| </button> | ||
|
|
||
| <button | ||
| type="submit" | ||
| [disabled]="userForm.invalid" | ||
| class="rounded-md border border-transparent bg-indigo-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 disabled:opacity-50"> | ||
| [disabled]="userSignalForm().invalid()" | ||
| class="rounded-md border border-transparent bg-indigo-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 focus:outline-none disabled:opacity-50"> | ||
| {{ id() ? 'Update' : 'Add' }} | ||
| </button> | ||
| </div> | ||
|
|
@@ -124,50 +133,51 @@ export class UserFormComponent { | |
| defaultValue: undefined, | ||
| }); | ||
|
|
||
| userForm = new FormGroup({ | ||
| firstname: new FormControl('', { | ||
| nonNullable: true, | ||
| validators: [Validators.required], | ||
| }), | ||
| lastname: new FormControl('', { | ||
| nonNullable: true, | ||
| validators: [Validators.required], | ||
| }), | ||
| age: new FormControl(0, { | ||
| nonNullable: true, | ||
| validators: [Validators.required, Validators.min(0)], | ||
| }), | ||
| grade: new FormControl(0, { | ||
| nonNullable: true, | ||
| validators: [Validators.required], | ||
| }), | ||
| userModel = linkedSignal<Omit<User, 'id'>>(() => { | ||
| const hasValue = this.userResource.hasValue(); | ||
| const user = this.userResource.value(); | ||
|
|
||
| if (hasValue && user) { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const { id, ...rest } = user; | ||
| return rest; | ||
| } else { | ||
| return { | ||
| firstname: '', | ||
| lastname: '', | ||
| age: 0, | ||
| grade: 0, | ||
| }; | ||
| } | ||
| }); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes a lot nicer, isn't it
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that's right! |
||
|
|
||
| userSignalForm = form(this.userModel, (schemePath) => { | ||
| required(schemePath.firstname, { message: 'Firstname is required' }); | ||
| required(schemePath.lastname, { message: 'Lastname is required' }); | ||
| required(schemePath.age); | ||
| min(schemePath.age, 0, { message: 'Age must be positive' }); | ||
| required(schemePath.grade); | ||
| }); | ||
|
|
||
| constructor() { | ||
| effect(() => { | ||
| onSubmit(event: Event): void { | ||
| event.preventDefault(); | ||
|
|
||
| submit(this.userSignalForm, async () => { | ||
| const userValue = this.userResource.value(); | ||
|
|
||
| if (userValue) { | ||
| this.userForm.patchValue(userValue); | ||
| await lastValueFrom( | ||
| this.backend.updateUser({ | ||
| ...this.userModel(), | ||
| id: userValue.id, | ||
| }), | ||
| ); | ||
| } else { | ||
| this.userForm.reset({ firstname: '', lastname: '', age: 0, grade: 0 }); | ||
| await lastValueFrom(this.backend.addUser(this.userModel())); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| onSubmit(): void { | ||
| if (this.userForm.valid) { | ||
| const userValue = this.userResource.value(); | ||
| const obs = userValue | ||
| ? this.backend.updateUser({ | ||
| ...this.userForm.getRawValue(), | ||
| id: userValue.id, | ||
| }) | ||
| : this.backend.addUser(this.userForm.getRawValue()); | ||
|
|
||
| obs.subscribe(() => { | ||
| this.router.navigate(['/']); | ||
| }); | ||
| } | ||
| this.router.navigate(['/']); | ||
| }); | ||
| } | ||
|
|
||
| onCancel(): void { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @tomalaforge , do you think it's ok to only check
if (user)instead ofif (hasValue && user)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, hasValue do !!user() so it's kind of the same