|
| 1 | +import { Component, EventEmitter, Input, Output } from '@angular/core'; |
| 2 | +import { FormBuilder, Validators } from '@angular/forms'; |
| 3 | + |
| 4 | +import { ILogin } from '@interfaces/user.interface'; |
| 5 | + |
| 6 | +import { RegexClass } from '@utils/regex.util'; |
| 7 | +import Storage from '@utils/storage.util'; |
| 8 | + |
| 9 | +@Component({ |
| 10 | + selector: 'app-login-form', |
| 11 | + templateUrl: './login-form.component.html', |
| 12 | + styleUrls: ['./login-form.component.scss'] |
| 13 | +}) |
| 14 | +export class LoginFormComponent { |
| 15 | + private regexExpressions = RegexClass; |
| 16 | + @Input() showErrors = false; |
| 17 | + @Input() errors: string[] = []; |
| 18 | + @Output() loginData: EventEmitter<ILogin> = new EventEmitter<ILogin>(); |
| 19 | + loginForm = this.fb.group({ |
| 20 | + email: [Storage.getLocalStorage('email') || '', [ |
| 21 | + Validators.required, |
| 22 | + Validators.pattern(this.regexExpressions.EMAIL), |
| 23 | + ]], |
| 24 | + password: ['', [ |
| 25 | + Validators.required, |
| 26 | + ]], |
| 27 | + remember: [Storage.getLocalStorage('email') ? true : false], |
| 28 | + }); |
| 29 | + |
| 30 | + constructor( |
| 31 | + private fb: FormBuilder, |
| 32 | + ) { } |
| 33 | + |
| 34 | + validateForm(field: string): boolean | undefined | null { |
| 35 | + const myForm = this.loginForm.get(field); |
| 36 | + return myForm?.errors && (myForm?.dirty || myForm?.touched); |
| 37 | + } |
| 38 | + |
| 39 | + validateField(field: string, error: string): boolean | undefined | null { |
| 40 | + return (this.loginForm.get(field)?.hasError(error)); |
| 41 | + } |
| 42 | + |
| 43 | + showPassword(id: string): void { |
| 44 | + const password = document.getElementById(id); |
| 45 | + const icon = document.getElementById(`${id}-icon`); |
| 46 | + if (password?.getAttribute('type') === 'password') { |
| 47 | + password.setAttribute('type', 'text'); |
| 48 | + icon?.classList.remove('bx-hide'); |
| 49 | + icon?.classList.add('bx-show'); |
| 50 | + } else { |
| 51 | + password?.setAttribute('type', 'password'); |
| 52 | + icon?.classList.remove('bx-show'); |
| 53 | + icon?.classList.add('bx-hide'); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + login(): void { |
| 58 | + if (this.loginForm.valid) { |
| 59 | + this.loginData.emit(this.loginForm.value); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + changeRemember(event: Event): void { |
| 64 | + const checked = (event.target as HTMLInputElement).checked; |
| 65 | + this.loginForm.patchValue({ |
| 66 | + remember: checked |
| 67 | + }); |
| 68 | + } |
| 69 | +} |
0 commit comments