From a2cfe78445a197a7902a230412ee6e397ae3c231 Mon Sep 17 00:00:00 2001 From: Chneemann Date: Fri, 9 May 2025 09:36:47 +0200 Subject: [PATCH] refactor: apply email format validator to LandingPageComponent --- .../landing-page/landing-page.component.html | 68 +++++++++--------- .../landing-page/landing-page.component.scss | 50 +++++++++----- .../landing-page/landing-page.component.ts | 69 +++++++++---------- 3 files changed, 98 insertions(+), 89 deletions(-) diff --git a/frontend/src/app/components/auth/landing-page/landing-page.component.html b/frontend/src/app/components/auth/landing-page/landing-page.component.html index 23fe0a0..79d465f 100644 --- a/frontend/src/app/components/auth/landing-page/landing-page.component.html +++ b/frontend/src/app/components/auth/landing-page/landing-page.component.html @@ -1,48 +1,42 @@
-

Movies, TV shows, and more

-

Watch whenever you want wherever you want

-

Enter your email to create or restart your subscription.

+

Movies, TV shows, and more

+

Watch whenever you want wherever you want

+

Enter your email to create or restart your subscription.

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

Please enter your email

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

This is not a valid email format

- }} + + + + +
+ @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'] && + form.value.email) { +

This is not a valid email format

+ } +
+ } +
+
diff --git a/frontend/src/app/components/auth/landing-page/landing-page.component.scss b/frontend/src/app/components/auth/landing-page/landing-page.component.scss index 4e28842..528b97e 100644 --- a/frontend/src/app/components/auth/landing-page/landing-page.component.scss +++ b/frontend/src/app/components/auth/landing-page/landing-page.component.scss @@ -4,20 +4,25 @@ @use "./../../../../assets/style/auth-layout.scss" as *; .headline { - p:nth-child(1) { + display: flex; + flex-direction: column; + text-align: center; + max-width: 90%; + gap: 12px; + + .title { font-size: 48px; font-weight: 700; - padding: 12px; } - p:nth-child(2) { + + .subtitle { font-size: 24px; font-weight: 500; - padding: 12px; } - p:nth-child(3) { + + .note { font-size: 18px; font-weight: 400; - padding: 12px; } } @@ -26,24 +31,29 @@ form { position: relative; width: 500px; padding: 24px; + input { - width: 65%; - padding: 12px 18px; + width: 100%; + padding: 12px 24px !important; margin: 8px 0; - color: $white; border: 2px solid $white; border-radius: 24px; - font-size: 18px; &::placeholder { color: rgba($white, 0.6); } } + .error-border { border: 2px solid $red; } + + app-btn-large { + margin-left: 24px; + width: 230px; + } } -.error-msg { +.error-container { position: absolute; bottom: 0; padding: 6px; @@ -56,28 +66,36 @@ form { form { flex-direction: column; width: 90%; + input { width: 100%; } + + app-btn-large { + margin-left: 0; + width: fit-content; + } } - .error-msg { + .error-container { position: relative; padding: 0; height: 12px; - margin-bottom: 8px; + margin-bottom: 12px; } } @media screen and (max-width: 500px) { .headline { - p:nth-child(1) { + .title { font-size: 36px; } - p:nth-child(2) { + + .subtitle { font-size: 20px; } - p:nth-child(3) { + + .note { font-size: 16px; } } diff --git a/frontend/src/app/components/auth/landing-page/landing-page.component.ts b/frontend/src/app/components/auth/landing-page/landing-page.component.ts index e11d02f..1682601 100644 --- a/frontend/src/app/components/auth/landing-page/landing-page.component.ts +++ b/frontend/src/app/components/auth/landing-page/landing-page.component.ts @@ -1,79 +1,76 @@ import { Component } from '@angular/core'; -import { FormsModule, NgForm } from '@angular/forms'; +import { + FormBuilder, + FormGroup, + FormsModule, + NgForm, + ReactiveFormsModule, + Validators, +} from '@angular/forms'; import { BtnLargeComponent } from '../../../shared/components/buttons/btn-large/btn-large.component'; import { Router } from '@angular/router'; import { CommonModule } from '@angular/common'; import { ErrorService } from '../../../services/error.service'; import { AuthService } from '../../../services/auth.service'; +import { emailFormatValidator } from '../../../validators/email-format.validator'; @Component({ selector: 'app-landing-page', standalone: true, - imports: [CommonModule, BtnLargeComponent, FormsModule], + imports: [CommonModule, BtnLargeComponent, FormsModule, ReactiveFormsModule], templateUrl: './landing-page.component.html', styleUrl: './landing-page.component.scss', }) export class LandingPageComponent { - authData = { - email: '', - send: false, - }; + form: FormGroup = new FormGroup({}); /** - * Initializes the LandingPageComponent with Router, AuthService, and ErrorService. + * Initializes the LandingPageComponent with Router, FormBuilder, AuthService, and ErrorService. */ constructor( private router: Router, + private fb: FormBuilder, private authService: AuthService, public errorService: ErrorService ) {} /** - * Validates the given email address. - * Converts to lowercase before checking against the regex. - * - * @param emailValue The email address to validate. - * @returns True if the email format is valid, false otherwise. + * Initializes the component by setting up necessary data. */ - isUserEmailValid(emailValue: string): boolean { - const emailRegex = /^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,}$/; - return emailRegex.test(emailValue.toLowerCase()); + ngOnInit(): void { + this.createForm(); } /** - * Handles form submission. - * If the form is valid, checks for duplicate email addresses. - * - * @param ngForm The submitted form. - * @param emailInput The input field for the email. + * Initializes the form with required fields and validators. */ - async onSubmit(ngForm: NgForm, emailInput: any): Promise { - if (ngForm.submitted && ngForm.form.valid) { - await this.checkDuplicatesEmail(); - } else { - emailInput.control.markAsTouched(); - } + private createForm(): void { + this.form = this.fb.group({ + email: ['', [Validators.required, emailFormatValidator()]], + }); } /** - * Checks whether the entered email already exists. - * Navigates to the registration page if it's available. - * Handles UI state and errors accordingly. + * Handles the submission of the registration form. + * Validates the form, sends a check if user already exists request, + * and navigates to the registration page if the user does not exist. + * + * @returns A Promise that resolves when the registration process is complete. */ - private async checkDuplicatesEmail(): Promise { - const email = this.authData.email.trim().toLowerCase(); - if (!email) return; + async onSubmit(): Promise { + if (this.form.invalid) return; - this.authData.send = true; + this.form.disable(); + const email = this.form.value.email?.toLowerCase().trim(); try { await this.authService.checkAuthUserEmail({ email }); - this.errorService.clearError(); - this.router.navigate(['/register'], { queryParams: { email: email } }); + this.router.navigate(['/register'], { queryParams: { email } }); + this.form.reset(); } catch (error) { this.errorService.handleError(error); } finally { - this.authData.send = false; + this.form.enable(); } } }