refactor: apply email format validator to LandingPageComponent

This commit is contained in:
Chneemann 2025-05-09 09:36:47 +02:00
parent aaa2d11194
commit a2cfe78445
3 changed files with 98 additions and 89 deletions

View file

@ -1,48 +1,42 @@
<section class="home-background">
<div class="headline">
<p>Movies, TV shows, and more</p>
<p>Watch whenever you want wherever you want</p>
<p>Enter your email to create or restart your subscription.</p>
<p class="title">Movies, TV shows, and more</p>
<p class="subtitle">Watch whenever you want wherever you want</p>
<p class="note">Enter your email to create or restart your subscription.</p>
</div>
<form
#taskForm="ngForm"
(ngSubmit)="onSubmit(taskForm, email)"
onsubmit="return false"
>
<input
type="email"
id="email"
name="email"
#email="ngModel"
placeholder="Email Address"
class="custom-input"
autocomplete="email"
[(ngModel)]="authData.email"
[class.error-border]="
(!email.valid && email.touched) ||
(email.touched &&
!isUserEmailValid(authData.email) &&
authData.email.length > 0)
"
required
/>
<div class="error-msg">
@if (!email.valid && email.touched) {
<p>Please enter your email</p>
} @else { @if (email.touched && authData.email.length > 0 &&
!isUserEmailValid(authData.email)) {
<p>This is not a valid email format</p>
}}
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="email-field">
<input
formControlName="email"
type="email"
id="email"
placeholder="Email Address"
autocomplete="email"
[class.error-border]="
form.controls['email'].invalid && form.controls['email'].touched
"
/>
</div>
<div class="error-container">
@if (form.controls['email'].touched && form.controls['email'].invalid) {
<div class="error-msg">
@if (form.controls['email'].errors?.['required']) {
<p role="alert">Please enter your email</p>
} @if (form.controls['email'].errors?.['invalidEmailFormat'] &&
form.value.email) {
<p role="alert">This is not a valid email format</p>
}
</div>
}
</div>
<app-btn-large
[value]="'Sign Up'"
[imgPath]="'play'"
[imgPath]="'arrow_right'"
[imgReverse]="true"
[disabled]="
(!isUserEmailValid(authData.email) && authData.email.length > 0) ||
authData.send
"
[disabled]="form.invalid || form.disabled"
></app-btn-large>
</form>
</section>

View file

@ -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;
}
}

View file

@ -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<void> {
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<void> {
const email = this.authData.email.trim().toLowerCase();
if (!email) return;
async onSubmit(): Promise<void> {
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();
}
}
}