import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core'; import { UserService } from './user.service'; //import { ChangeUserPasswordService } from '../ChangePassword/changeuserpassword.service'; import { Router } from '@angular/router'; import { FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; import { User } from '../UserEntity/datamodel'; import { Http, Response } from '@angular/http'; //import { Global } from '../../Shared/global'; //import { DBOperation } from 'S'; //import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable'; import { ConfirmService } from '../../Shared/Confirm/confirm.service'; import 'rxjs/Rx'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/filter'; @Component({ templateUrl: './changeuserpassword.component.html' }) export class ChangeUserPassword implements OnInit { user: User; changeUserPasswordFrm: FormGroup; error: any; alerts: string; constructor(private changeUserPasswordService: UserService, private router: Router, private fb: FormBuilder, private _confirmService: ConfirmService) { } ngOnInit(): void { this.user = new User(); this.alerts = ''; this.changeUserPasswordFrm = this.fb.group({ userId: [''], loginId: ['', Validators.required], oldPassword: ['', Validators.required], newPassword: ['', [Validators.required, Validators.minLength(8)]], confirmPassword: ['', Validators.required] }); this.GetUserById(); } GetUserById() { this.changeUserPasswordService.GetUserById() .subscribe(x => { this.BindFormFields(x) }, error => this.error = error); } redirect() { this.router.navigate(['/']); } public onFormSubmit() { this.alerts = ''; if (this.user.Password != this.changeUserPasswordFrm.value.oldPassword) { this.alerts = 'Old password is invalid'; } if (this.user.Password == this.changeUserPasswordFrm.value.newPassword) { this.alerts += '
New password and old password must be different'; } if (this.changeUserPasswordFrm.value.newPassword != this.changeUserPasswordFrm.value.confirmPassword) { this.alerts += '
New password and confirm password must be same'; } if (this.alerts == '') { var obj = this.changeUserPasswordFrm.value; return this.changeUserPasswordService.ChangeUserPassword(obj) .subscribe( n => (this.AfterInsertData(n)), error => this.error = error); } } AfterInsertData(data) { if (data.Status == "false") { this.alerts = "Password change unsuccessfull"; } else { this._confirmService.activate("Password changed successfully.", "alertMsg"); //this.alerts = "Password changed successfully"; } } BindFormFields(data){ this.user = data[0]; this.changeUserPasswordFrm.controls['userId'].setValue(this.user.Id); this.changeUserPasswordFrm.controls['loginId'].setValue(this.user.LoginId); } ResetFormFields(){ this.changeUserPasswordFrm.reset() this.changeUserPasswordFrm.controls['loginId'].setValue(this.user.LoginId); this.changeUserPasswordFrm.controls['oldPassword'].setValue(''); this.changeUserPasswordFrm.controls['newPassword'].setValue(''); this.changeUserPasswordFrm.controls['confirmPassword'].setValue(''); this.alerts = ''; } }