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.
137 lines
4.8 KiB
TypeScript
137 lines
4.8 KiB
TypeScript
import { CommonModule } from '@angular/common';
|
|
import { Component, OnInit, ViewChild } from '@angular/core';
|
|
import { AbstractControl, FormBuilder, FormControl, FormGroup, FormsModule, ReactiveFormsModule, ValidationErrors, Validators } from '@angular/forms';
|
|
import { TranslateModule } from '@ngx-translate/core';
|
|
import { PasswordHideShowComponent } from '../../shared/components/password-hide-show/password-hide-show.component';
|
|
import { HttpURIService } from '../../app.http.uri.service';
|
|
import { URIKey } from '../../utils/uri-enums';
|
|
import { StorageService } from '../../shared/services/storage.service';
|
|
|
|
@Component({
|
|
selector: 'app-change-password',
|
|
imports: [TranslateModule, FormsModule, ReactiveFormsModule, CommonModule, PasswordHideShowComponent,],
|
|
templateUrl: './change-password.component.html',
|
|
styleUrl: './change-password.component.scss'
|
|
})
|
|
|
|
export class ChangePasswordComponent implements OnInit{
|
|
isFirstLogin = false;
|
|
firstTimeLoginForm!: FormGroup;
|
|
changePasswordForm!: FormGroup;
|
|
currentLanguage = new FormControl();
|
|
httpService: any;
|
|
|
|
passwordType: string = 'password';
|
|
passwordType1: string = 'password';
|
|
passwordType2: string = 'password';
|
|
|
|
@ViewChild('psh') passwordHideShow?: PasswordHideShowComponent;
|
|
@ViewChild('psh1') passwordHideShow1 ?: PasswordHideShowComponent;
|
|
@ViewChild('psh2') passwordHideShow2 ?: PasswordHideShowComponent;
|
|
constructor(private fb: FormBuilder, private httpURIService: HttpURIService, private storageService: StorageService){}
|
|
|
|
togglePasswordType() {
|
|
this.passwordType = this.passwordHideShow?.showPassword ? 'password' : 'text';
|
|
}
|
|
togglePasswordType1() {
|
|
this.passwordType1 = this.passwordHideShow1?.showPassword ? 'password' : 'text';
|
|
}
|
|
togglePasswordType2() {
|
|
this.passwordType2 = this.passwordHideShow2?.showPassword ? 'password' : 'text';
|
|
}
|
|
|
|
passwordMatchValidator(group: AbstractControl): ValidationErrors | null {
|
|
const newPassword = group.get('newPassword')?.value;
|
|
const confirmPassword = group.get('confirmPassword')?.value;
|
|
return newPassword === confirmPassword ? null : { passwordMismatch: true };
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
this.checkIfFirstTimeChangePasswordOrNot();
|
|
|
|
if (this.isFirstLogin) {
|
|
this.firstTimeLoginForm = this.fb.group({
|
|
newPassword: ['', [Validators.required, Validators.minLength(6)]],
|
|
confirmPassword: ['', [Validators.required, Validators.minLength(6)]]
|
|
}, { validators: this.passwordMatchValidator });
|
|
} else {
|
|
this.changePasswordForm = this.fb.group({
|
|
oldPassword: ['', Validators.required],
|
|
newPassword: ['', [Validators.required, Validators.minLength(6)]],
|
|
confirmPassword: ['', [Validators.required, Validators.minLength(6)]]
|
|
}, { validators: this.passwordMatchValidator });
|
|
|
|
}
|
|
}
|
|
|
|
checkIfFirstTimeChangePasswordOrNot(): void {
|
|
const fromMenu = history.state?.['fromMenu'];
|
|
|
|
if (fromMenu) {
|
|
this.isFirstLogin = false;
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const currentUser: any = JSON.parse(
|
|
this.storageService.getItem('user') || '{}'
|
|
);
|
|
|
|
this.isFirstLogin = !!currentUser?.user?.isFirstLogin;
|
|
} catch (error) {
|
|
console.error('Error parsing user data:', error);
|
|
this.isFirstLogin = false;
|
|
}
|
|
}
|
|
|
|
initChangePasswordForm(): void {
|
|
this.changePasswordForm = this.fb.group(
|
|
{
|
|
oldPassword: ['', Validators.required],
|
|
enterNewPassword: ['', [Validators.required, Validators.minLength(6)]],
|
|
confirmPassword: ['', [Validators.required, Validators.minLength(6)]],
|
|
},
|
|
{ validators: this.passwordMatchValidator }
|
|
);
|
|
}
|
|
|
|
|
|
get newPasswordError$() {
|
|
const control = this.changePasswordForm.get('newPassword');
|
|
if (!control || !control.touched) return null;
|
|
|
|
if (control.hasError('required')) return 'fieldRequired';
|
|
if (control.hasError('minlength')) return 'passwordTooShort';
|
|
return null;
|
|
}
|
|
|
|
get confirmPasswordError$() {
|
|
const control = this.changePasswordForm.get('confirmPassword');
|
|
if (!control || !control.touched) return null;
|
|
|
|
if (control.hasError('required')) return 'fieldRequired';
|
|
if (control.hasError('minlength')) return 'passwordTooShort';
|
|
if (this.changePasswordForm.hasError('passwordMismatch')) return 'passwordsDoNotMatch';
|
|
|
|
return null;
|
|
}
|
|
getFormPayload() {
|
|
const form = this.isFirstLogin ? this.firstTimeLoginForm : this.changePasswordForm;
|
|
|
|
return {
|
|
oldPassword: form.get('oldPassword')?.value || null,
|
|
// confirmPassword: form.get('confirmPassword')?.value || null,
|
|
newPassword: form.get('newPassword')?.value,
|
|
userId: this.storageService.getItem('USER_ID'),
|
|
porOrgaCode: this.storageService.getItem('POR_ORGACODE')
|
|
};
|
|
}
|
|
onSubmit(){
|
|
const payload = this.getFormPayload();
|
|
|
|
this.httpURIService.requestPOST(URIKey.CHANGE_PASSWORD_URI, payload)
|
|
.subscribe();
|
|
}
|
|
|
|
}
|