From 14bb6c520ce58ad909840334a85253d689bbeacf Mon Sep 17 00:00:00 2001 From: Chneemann Date: Wed, 7 May 2025 10:52:28 +0200 Subject: [PATCH] feat: create EmailRequestComponent and integrate into ForgotPasswordComponent to separate responsibilities --- .../email-request.component.html | 36 ++++++++++ .../email-request.component.scss | 3 + .../email-request.component.spec.ts | 23 +++++++ .../email-request/email-request.component.ts | 66 +++++++++++++++++++ .../forgot-password.component.html | 45 +------------ .../forgot-password.component.ts | 13 +++- 6 files changed, 143 insertions(+), 43 deletions(-) create mode 100644 frontend/src/app/components/auth/forgot-password/email-request/email-request.component.html create mode 100644 frontend/src/app/components/auth/forgot-password/email-request/email-request.component.scss create mode 100644 frontend/src/app/components/auth/forgot-password/email-request/email-request.component.spec.ts create mode 100644 frontend/src/app/components/auth/forgot-password/email-request/email-request.component.ts diff --git a/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.html b/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.html new file mode 100644 index 0000000..fc207e7 --- /dev/null +++ b/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.html @@ -0,0 +1,36 @@ +
+

We will send you an email with instructions to reset your password.

+
+ +
+ + +
+ @if (form.controls['email'].touched && form.controls['email'].invalid) { +
+ @if (form.controls['email'].errors?.['required']) { +

Please enter your email

+ } @if (form.controls['email'].errors?.['invalidEmailFormat']) { +

This is not a valid email format

+ } +
+ } +
+ + +
diff --git a/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.scss b/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.scss new file mode 100644 index 0000000..7ccd3a4 --- /dev/null +++ b/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.scss @@ -0,0 +1,3 @@ +@use "./../../../../../assets/style/backgrounds.scss" as *; +@use "./../../../../../assets/style/form.scss" as *; +@use "./../../../../../assets/style/auth-layout.scss" as *; diff --git a/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.spec.ts b/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.spec.ts new file mode 100644 index 0000000..3b42912 --- /dev/null +++ b/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { EmailRequestComponent } from './email-request.component'; + +describe('EmailRequestComponent', () => { + let component: EmailRequestComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [EmailRequestComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(EmailRequestComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.ts b/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.ts new file mode 100644 index 0000000..8f2297f --- /dev/null +++ b/frontend/src/app/components/auth/forgot-password/email-request/email-request.component.ts @@ -0,0 +1,66 @@ +import { Component, EventEmitter, OnInit, Output } from '@angular/core'; +import { BtnLargeComponent } from '../../../../shared/components/buttons/btn-large/btn-large.component'; +import { + FormBuilder, + FormGroup, + FormsModule, + ReactiveFormsModule, + Validators, +} from '@angular/forms'; +import { ErrorService } from '../../../../services/error.service'; +import { AuthService } from '../../../../services/auth.service'; +import { emailFormatValidator } from '../../../../validators/email-format.validator'; + +@Component({ + selector: 'app-email-request', + imports: [BtnLargeComponent, FormsModule, ReactiveFormsModule], + templateUrl: './email-request.component.html', + styleUrl: './email-request.component.scss', +}) +export class EmailRequestComponent implements OnInit { + @Output() submittedChange = new EventEmitter(); + + form: FormGroup = new FormGroup({}); + + constructor( + private errorService: ErrorService, + private authService: AuthService, + private fb: FormBuilder + ) {} + + ngOnInit(): void { + this.createForm(); + } + + async onSubmit(): Promise { + if (this.form.valid) { + const body = this.createFormRequestBody(); + this.form.disable(); + + try { + await this.authService.forgotPassword(body); + this.emitFormSubmitted(); + } catch (error) { + this.errorService.handleError(error); + } finally { + this.form.enable(); + } + } + } + + private createForm(): void { + this.form = this.fb.group({ + email: ['', [Validators.required, emailFormatValidator()]], + }); + } + + private createFormRequestBody(): { email: string } { + return { + email: this.form.value.email.toLowerCase(), + }; + } + + private emitFormSubmitted(): void { + this.submittedChange.emit(true); + } +} diff --git a/frontend/src/app/components/auth/forgot-password/forgot-password.component.html b/frontend/src/app/components/auth/forgot-password/forgot-password.component.html index 0890840..e20c37c 100644 --- a/frontend/src/app/components/auth/forgot-password/forgot-password.component.html +++ b/frontend/src/app/components/auth/forgot-password/forgot-password.component.html @@ -2,48 +2,9 @@
Forgot your password?
@if (!queryEmail && !sendEmailSuccess && !queryEmailSuccess) { -
-

We will send you an email with instructions to reset your password.

-
-
- -
- @if (!email.valid && email.touched) { -

Please enter your email

- } @else { @if (email.touched && authData.email.length > 0 && - !isUserEmailValid(authData.email.toLowerCase())) { -

This is not a valid email format

- }} -
- -
+ } @else { @if (sendEmailSuccess) {

diff --git a/frontend/src/app/components/auth/forgot-password/forgot-password.component.ts b/frontend/src/app/components/auth/forgot-password/forgot-password.component.ts index f500b45..d5745d0 100644 --- a/frontend/src/app/components/auth/forgot-password/forgot-password.component.ts +++ b/frontend/src/app/components/auth/forgot-password/forgot-password.component.ts @@ -5,11 +5,18 @@ import { FormsModule, NgForm } from '@angular/forms'; import { ActivatedRoute, Params, RouterLink } from '@angular/router'; import { AuthService } from '../../../services/auth.service'; import { ErrorService } from '../../../services/error.service'; +import { EmailRequestComponent } from './email-request/email-request.component'; @Component({ selector: 'app-forgot-password', standalone: true, - imports: [CommonModule, BtnLargeComponent, FormsModule, RouterLink], + imports: [ + CommonModule, + BtnLargeComponent, + FormsModule, + RouterLink, + EmailRequestComponent, + ], templateUrl: './forgot-password.component.html', styleUrl: './forgot-password.component.scss', }) @@ -43,6 +50,10 @@ export class ForgotPasswordComponent implements OnInit { this.handleQueryParams(); } + submittedChange(value: boolean): void { + this.sendEmailSuccess = value; + } + /** * Subscribes to query parameters and sets internal flags based on them. */