changeuserpassword.component.ts
4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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';
import { LoadingService } from '../../shared/loading.service';
@Component({
templateUrl: './changeuserpassword.component.html'
})
export class ChangeUserPassword implements OnInit {
user: User;
changeUserPasswordFrm: FormGroup;
error: any;
alerts: string;
constructor(private _loadingService: LoadingService,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._loadingService.ShowLoading("global-loading");
this.GetUserById();
this._loadingService.HideLoading("global-loading");
}
GetUserById() {
this.changeUserPasswordService.GetUserById()
.subscribe(x => { this.BindFormFields(x) }, error => this.error = error);
}
redirect() {
this.router.navigate(['/']);
}
public onFormSubmit() {
this.alerts = '';
if ((this.changeUserPasswordFrm.value.oldPassword)=='')
{
this.alerts = '<span>Old password is required.</span>';
}
if (this.user.Password != this.changeUserPasswordFrm.value.oldPassword && this.changeUserPasswordFrm.value.oldPassword!='') {
this.alerts += '</br><span>Old password is invalid.</span>';
}
if ((this.changeUserPasswordFrm.value.newPassword) == '') {
this.alerts += '</br><span>New password is required.</span>';
}
if (this.user.Password == this.changeUserPasswordFrm.value.newPassword) {
this.alerts += '</br><span>New password and old password must be different.</span>';
}
if ((this.changeUserPasswordFrm.value.newPassword) == '') {
this.alerts += '</br><span>Confirm password is required.</span>';
}
if (this.changeUserPasswordFrm.value.newPassword != this.changeUserPasswordFrm.value.confirmPassword) {
this.alerts += '</br><span>New password and confirm password must be same.</span>';
}
if (this.alerts == '') {
var obj = this.changeUserPasswordFrm.value;
return this.changeUserPasswordService.ChangeUserPassword(obj)
.subscribe(
n => (this.AfterInsertData(n)),
error => this.error = <any>error);
}
}
AfterInsertData(data) {
if (data.Status == "false") {
this.alerts = "<span>Password change unsuccessfull</span>";
} else {
this._confirmService.activate("Password changed successfully.", "alertMsg");
//this.alerts = "<span>Password changed successfully</span>";
}
}
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 = '';
}
}