|
| 1 | +// set-password-modal.component.ts |
| 2 | +import { Component, EventEmitter, Input, Output } from '@angular/core'; |
| 3 | +import { CommonModule } from '@angular/common'; |
| 4 | +import { FormsModule } from '@angular/forms'; |
| 5 | +import { AuthService } from '../../../../../core/services/api-service/auth.service'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'app-set-password-modal', |
| 9 | + standalone: true, |
| 10 | + imports: [CommonModule, FormsModule], |
| 11 | + templateUrl: './set-password-modal.component.html', |
| 12 | + styleUrls: ['./set-password-modal.component.scss'], |
| 13 | +}) |
| 14 | +export class SetPasswordModalComponent { |
| 15 | + @Input() isOpen = false; |
| 16 | + @Output() closed = new EventEmitter<void>(); |
| 17 | + |
| 18 | + password = ''; |
| 19 | + confirmPassword = ''; |
| 20 | + isLoading = false; |
| 21 | + errorMessage = ''; |
| 22 | + |
| 23 | + constructor(private authService: AuthService) {} |
| 24 | + |
| 25 | + close() { |
| 26 | + this.closed.emit(); |
| 27 | + this.resetForm(); |
| 28 | + this.isOpen = !this.isOpen; |
| 29 | + } |
| 30 | + |
| 31 | + resetForm() { |
| 32 | + this.password = ''; |
| 33 | + this.confirmPassword = ''; |
| 34 | + this.errorMessage = ''; |
| 35 | + this.isLoading = false; |
| 36 | + } |
| 37 | + |
| 38 | + onSubmit() { |
| 39 | + if (!this.password || !this.confirmPassword) { |
| 40 | + this.errorMessage = 'Vui lòng nhập đầy đủ thông tin'; |
| 41 | + return; |
| 42 | + } |
| 43 | + if (this.password.length < 6) { |
| 44 | + this.errorMessage = 'Mật khẩu phải có ít nhất 6 ký tự'; |
| 45 | + return; |
| 46 | + } |
| 47 | + if (this.password !== this.confirmPassword) { |
| 48 | + this.errorMessage = 'Mật khẩu nhập lại không khớp'; |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + this.isLoading = true; |
| 53 | + this.authService.createInitialPassword(this.password).subscribe({ |
| 54 | + next: () => { |
| 55 | + this.isLoading = false; |
| 56 | + this.close(); |
| 57 | + // Có thể bắn event success hoặc toast ở đây |
| 58 | + }, |
| 59 | + error: () => { |
| 60 | + this.isLoading = false; |
| 61 | + }, |
| 62 | + }); |
| 63 | + } |
| 64 | +} |
0 commit comments