-
-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #311 from stefanoslig/refactor-forms-part-1
refactor forms part 1
- Loading branch information
Showing
18 changed files
with
195 additions
and
118 deletions.
There are no files selected for viewing
This file contains 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
Empty file.
This file contains 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
This file contains 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,56 +1,27 @@ | ||
import { DynamicFormComponent, Field, formsActions, ListErrorsComponent, ngrxFormsQuery } from '@realworld/core/forms'; | ||
import { ChangeDetectionStrategy, Component, inject, OnDestroy, OnInit } from '@angular/core'; | ||
import { Validators } from '@angular/forms'; | ||
import { RouterModule } from '@angular/router'; | ||
import { InputErrorsComponent, ListErrorsComponent } from '@realworld/core/forms'; | ||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; | ||
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'; | ||
import { RouterLink } from '@angular/router'; | ||
import { AuthStore } from '@realworld/auth/data-access'; | ||
import { Store } from '@ngrx/store'; | ||
|
||
const structure: Field[] = [ | ||
{ | ||
type: 'INPUT', | ||
name: 'email', | ||
placeholder: 'Email', | ||
validator: [Validators.required], | ||
}, | ||
{ | ||
type: 'INPUT', | ||
name: 'password', | ||
placeholder: 'Password', | ||
validator: [Validators.required], | ||
attrs: { | ||
type: 'password', | ||
}, | ||
}, | ||
]; | ||
|
||
@Component({ | ||
selector: 'cdt-login', | ||
standalone: true, | ||
templateUrl: './login.component.html', | ||
styleUrls: ['./login.component.css'], | ||
imports: [ListErrorsComponent, DynamicFormComponent, RouterModule], | ||
imports: [ListErrorsComponent, RouterLink, ReactiveFormsModule, InputErrorsComponent], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class LoginComponent implements OnInit, OnDestroy { | ||
private readonly store = inject(Store); | ||
export class LoginComponent { | ||
private readonly authStore = inject(AuthStore); | ||
private readonly fb = inject(FormBuilder); | ||
|
||
structure$ = this.store.select(ngrxFormsQuery.selectStructure); | ||
data$ = this.store.select(ngrxFormsQuery.selectData); | ||
|
||
ngOnInit() { | ||
this.store.dispatch(formsActions.setStructure({ structure })); | ||
} | ||
|
||
updateForm(changes: any) { | ||
this.store.dispatch(formsActions.updateData({ data: changes })); | ||
} | ||
|
||
submit() { | ||
this.authStore.login(); | ||
} | ||
form = this.fb.nonNullable.group({ | ||
email: ['', [Validators.required]], | ||
password: ['', [Validators.required]], | ||
}); | ||
|
||
ngOnDestroy() { | ||
this.store.dispatch(formsActions.initializeForm()); | ||
onSubmit() { | ||
this.authStore.login(this.form.getRawValue()); | ||
this.form.reset(); | ||
} | ||
} |
This file contains 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
61 changes: 14 additions & 47 deletions
61
libs/auth/feature-auth/src/lib/register/register.component.ts
This file contains 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,62 +1,29 @@ | ||
import { DynamicFormComponent, Field, formsActions, ListErrorsComponent, ngrxFormsQuery } from '@realworld/core/forms'; | ||
import { ChangeDetectionStrategy, Component, inject, OnDestroy, OnInit } from '@angular/core'; | ||
import { Validators } from '@angular/forms'; | ||
import { InputErrorsComponent, ListErrorsComponent } from '@realworld/core/forms'; | ||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; | ||
import { RouterModule } from '@angular/router'; | ||
import { AuthStore } from '@realworld/auth/data-access'; | ||
import { Store } from '@ngrx/store'; | ||
|
||
const structure: Field[] = [ | ||
{ | ||
type: 'INPUT', | ||
name: 'username', | ||
placeholder: 'Username', | ||
validator: [Validators.required], | ||
}, | ||
{ | ||
type: 'INPUT', | ||
name: 'email', | ||
placeholder: 'Email', | ||
validator: [Validators.required], | ||
}, | ||
{ | ||
type: 'INPUT', | ||
name: 'password', | ||
placeholder: 'Password', | ||
validator: [Validators.required], | ||
attrs: { | ||
type: 'password', | ||
}, | ||
}, | ||
]; | ||
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'; | ||
|
||
@Component({ | ||
selector: 'cdt-register', | ||
standalone: true, | ||
templateUrl: './register.component.html', | ||
styleUrls: ['./register.component.css'], | ||
imports: [ListErrorsComponent, DynamicFormComponent, RouterModule], | ||
imports: [ListErrorsComponent, RouterModule, ReactiveFormsModule, InputErrorsComponent], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class RegisterComponent implements OnInit, OnDestroy { | ||
private readonly store = inject(Store); | ||
export class RegisterComponent { | ||
private readonly authStore = inject(AuthStore); | ||
private readonly fb = inject(FormBuilder); | ||
|
||
structure$ = this.store.select(ngrxFormsQuery.selectStructure); | ||
data$ = this.store.select(ngrxFormsQuery.selectData); | ||
|
||
ngOnInit() { | ||
this.store.dispatch(formsActions.setStructure({ structure })); | ||
} | ||
|
||
updateForm(changes: any) { | ||
this.store.dispatch(formsActions.updateData({ data: changes })); | ||
} | ||
|
||
submit() { | ||
this.authStore.register(); | ||
} | ||
form = this.fb.nonNullable.group({ | ||
username: ['', [Validators.required]], | ||
email: ['', [Validators.required]], | ||
password: ['', [Validators.required]], | ||
}); | ||
|
||
ngOnDestroy() { | ||
this.store.dispatch(formsActions.initializeForm()); | ||
onSubmit() { | ||
this.authStore.register(this.form.getRawValue()); | ||
this.form.reset(); | ||
} | ||
} |
This file contains 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
Empty file.
This file contains 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,8 +1,8 @@ | ||
<fieldset [formGroup]="group" class="form-group"> | ||
<input | ||
[formControlName]="field?.name" | ||
[attr.placeholder]="field?.placeholder" | ||
[formControlName]="field.name" | ||
[attr.placeholder]="field.placeholder" | ||
class="form-control form-control-lg" | ||
[attr.type]="field?.attrs?.type || 'text'" | ||
[attr.type]="field.attrs?.type || 'text'" | ||
/> | ||
</fieldset> |
This file contains 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
Empty file.
6 changes: 3 additions & 3 deletions
6
libs/core/forms/src/lib/fields/textarea/textarea.component.html
This file contains 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,9 +1,9 @@ | ||
<fieldset [formGroup]="group" class="form-group"> | ||
<textarea | ||
class="form-control form-control-lg" | ||
[attr.rows]="field?.attrs?.rows || 5" | ||
[formControlName]="field?.name" | ||
[attr.placeholder]="field?.placeholder" | ||
[attr.rows]="field.attrs?.rows || 5" | ||
[formControlName]="field.name" | ||
[attr.placeholder]="field.placeholder" | ||
> | ||
</textarea> | ||
</fieldset> |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { inject, Pipe, PipeTransform } from '@angular/core'; | ||
import { VALIDATION_ERROR_MESSAGES } from './error-messages'; | ||
|
||
@Pipe({ | ||
name: 'errorMapper', | ||
standalone: true, | ||
}) | ||
export class ErrorMapperPipe implements PipeTransform { | ||
private errorMessages = inject(VALIDATION_ERROR_MESSAGES); | ||
|
||
transform(key: string, errValue: any): string { | ||
if (!this.errorMessages[key]) { | ||
return ''; | ||
} | ||
return this.errorMessages[key](errValue); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export const ERROR_MESSAGES: { [key in string]: (args?: any) => string } = { | ||
required: () => `Required field`, | ||
email: () => `Not a valid email`, | ||
minlength: ({ requiredLength }) => `The length should be at least ${requiredLength} characters`, | ||
}; | ||
|
||
export const VALIDATION_ERROR_MESSAGES = new InjectionToken(`Validation Messages`, { | ||
providedIn: 'root', | ||
factory: () => ERROR_MESSAGES, | ||
}); |
9 changes: 9 additions & 0 deletions
9
libs/core/forms/src/lib/input-errors/input-errors.component.html
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div *isErrorVisible="control()"> | ||
<ul class="error-messages"> | ||
@for (error of control().errors | keyvalue; track error) { | ||
<li data-e2e-id="error"> | ||
{{ error.key | errorMapper: error.value }} | ||
</li> | ||
} | ||
</ul> | ||
</div> |
16 changes: 16 additions & 0 deletions
16
libs/core/forms/src/lib/input-errors/input-errors.component.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { KeyValuePipe } from '@angular/common'; | ||
import { ChangeDetectionStrategy, Component, input } from '@angular/core'; | ||
import { AbstractControl } from '@angular/forms'; | ||
import { ErrorMapperPipe } from './error-mapper-pipe'; | ||
import { IsErrorVisibleDirective } from './is-error-visible.directive'; | ||
|
||
@Component({ | ||
selector: 'cdt-input-errors', | ||
standalone: true, | ||
templateUrl: './input-errors.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
imports: [KeyValuePipe, ErrorMapperPipe, IsErrorVisibleDirective], | ||
}) | ||
export class InputErrorsComponent { | ||
readonly control = input.required<AbstractControl>(); | ||
} |
Oops, something went wrong.