You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aConnect-UX/src/app/user-management/reset-password-modal/reset-password-modal.compon...

173 lines
5.7 KiB
TypeScript

6 days ago
import { Component, Input, OnInit, OnChanges, SimpleChanges, Output, EventEmitter, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, Validators, AbstractControl, ValidationErrors, ReactiveFormsModule } from '@angular/forms';
import { HttpURIService } from '../../app.http.uri.service';
import { URIKey } from '../../utils/uri-enums';
import { I18NService } from '../../services/i18n.service';
import { StorageService } from '../../shared/services/storage.service';
6 days ago
import { SuccessMessages, ErrorMessages } from '../../utils/enums';
import { HttpErrorResponse } from '@angular/common/http';
import { TranslateModule } from '@ngx-translate/core';
import { CommonModule } from '@angular/common';
import { PasswordHideShowComponent } from '../../shared/components/password-hide-show/password-hide-show.component';
@Component({
selector: 'app-reset-password-modal',
standalone: true,
imports: [TranslateModule, ReactiveFormsModule, CommonModule],
templateUrl: './reset-password-modal.component.html'
})
6 days ago
export class ResetPasswordModalComponent implements OnInit, OnChanges {
newPasswordType: string = 'password';
confirmPasswordType: string = 'password';
@Input() userId: string | null = null;
@Output() modalClosed = new EventEmitter<void>();
resetPasswordForm!: FormGroup;
6 days ago
isSubmitting: boolean = false;
@ViewChild('newPasswordPsh') passwordHideShow?: PasswordHideShowComponent;
@ViewChild('confirmPasswordPsh') confirmPasswordHideShow?: PasswordHideShowComponent;
constructor(
private fb: FormBuilder,
private httpURIService: HttpURIService,
private i18nService: I18NService,
private storageService: StorageService
) {}
6 days ago
ngOnInit(): void {
6 days ago
this.initializeForm();
}
initializeForm(): void {
this.resetPasswordForm = this.fb.group({
newPassword: ['', [
Validators.required,
Validators.minLength(8),
Validators.maxLength(20),
6 days ago
Validators.pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$/)
]],
confirmPassword: ['', Validators.required]
6 days ago
}, {
validators: this.passwordMatchValidator,
updateOn: 'blur'
});
6 days ago
// Update confirm password validation when new password changes
this.resetPasswordForm.get('newPassword')?.valueChanges.subscribe(() => {
this.resetPasswordForm.get('confirmPassword')?.updateValueAndValidity();
});
6 days ago
}
6 days ago
ngOnChanges(changes: SimpleChanges): void {
if (changes['userId'] && this.resetPasswordForm) {
console.log('ResetPasswordModal: userId changed to:', this.userId);
// Reset the form when userId changes
this.resetForm();
// Update the form with new userId if needed
if (this.userId) {
// You might want to fetch user details here if needed
console.log('Preparing reset password for user:', this.userId);
}
}
}
6 days ago
togglePasswordType() {
this.newPasswordType = this.newPasswordType === 'password' ? 'text' : 'password';
}
6 days ago
toggleConfirmPasswordType() {
this.confirmPasswordType = this.confirmPasswordType === 'password' ? 'text' : 'password';
}
passwordMatchValidator(group: AbstractControl): ValidationErrors | null {
const newPassword = group.get('newPassword')?.value;
const confirmPassword = group.get('confirmPassword')?.value;
6 days ago
if (!newPassword || !confirmPassword) return null;
// If passwords don't match, set error on confirmPassword field
if (newPassword !== confirmPassword) {
group.get('confirmPassword')?.setErrors({ passwordMismatch: true });
return { passwordMismatch: true };
} else {
group.get('confirmPassword')?.setErrors(null);
return null;
}
}
submit() {
6 days ago
if (this.resetPasswordForm.invalid || !this.userId) {
console.error('Form is invalid or no userId available');
this.resetPasswordForm.markAllAsTouched();
return;
}
console.log('Submitting reset password for userId:', this.userId);
this.isSubmitting = true;
const payload = {
userId: this.userId,
newPassword: this.resetPasswordForm.get('newPassword')?.value,
porOrgaCode: this.storageService.getItem('POR_ORGACODE')
};
this.httpURIService.requestPOST(URIKey.RESET_PASSWORD_URI, payload)
.subscribe({
6 days ago
next: (response) => {
if (!(response instanceof HttpErrorResponse)) {
this.i18nService.success(SuccessMessages.RESET_PASSWORD_SUCCESS, []);
this.closeModal();
6 days ago
} else {
this.i18nService.error(ErrorMessages.RESET_PASSWORD_FAILED, []);
}
6 days ago
this.isSubmitting = false;
},
error: (error) => {
console.error('Error resetting password:', error);
this.isSubmitting = false;
}
});
}
6 days ago
resetForm(): void {
this.resetPasswordForm.reset();
6 days ago
this.isSubmitting = false;
this.newPasswordType = 'password';
this.confirmPasswordType = 'password';
6 days ago
// Reset password hide/show components
this.passwordHideShow?.reset();
this.confirmPasswordHideShow?.reset();
6 days ago
// Reset form validation state
Object.keys(this.resetPasswordForm.controls).forEach(key => {
const control = this.resetPasswordForm.get(key);
control?.markAsPristine();
control?.markAsUntouched();
});
}
closeModal() {
this.resetForm();
this.modalClosed.emit();
// Remove the modal backdrop and reset modal state
const modal = document.getElementById('resetPasswordModal');
6 days ago
if (modal) {
modal.classList.remove('show');
modal.setAttribute('aria-hidden', 'true');
modal.style.display = 'none';
}
document.body.classList.remove('modal-open');
6 days ago
const backdrop = document.querySelector('.modal-backdrop');
if (backdrop) {
backdrop.remove();
}
}
6 days ago
}