updateuserprofile.component.ts
5.14 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { Component, OnInit, AfterViewInit,ViewChild } from '@angular/core';
import { UserService } from '../userentity/user.service';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { User } from '../userentity/datamodel';
import { Http, Response } from '@angular/http';
import { GlobalService } from '../../shared/global';
//import { DBOperation } from 'S';
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:'./updateuserprofile.component.html' // '../../../../../wwwroot/html/UpdateProfile/updateuserprofile.component.html'
})
export class UpdateUserProfile implements OnInit {
UserId: number=1;
//users: IUser[];
user: User;
msg: string;
indLoading: boolean = false;
// dbops: DBOperation;
modalTitle: string;
modalBtnTitle: string;
baseUrl: string = "User";
userFrm: FormGroup;
useFname: string;
error;
status: boolean;
alerts: string;
emailPattern = "^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$";
//@ViewChild("profileModal")
//profileModal: ModalComponent;
//errorMessage: any;
constructor(private _loadingService: LoadingService,private userservice: UserService, private router: Router, private fb: FormBuilder, private http: Http,
private _confirmService: ConfirmService, public global: GlobalService
) { }
ngOnInit(): void {
this.user = new User();
this.alerts = '';
this.userFrm = this.fb.group({
id: [''],
firstName: ['', [Validators.required,this.noWhitespaceValidator]],
lastName: ['', [Validators.required,this.noWhitespaceValidator]],
emailId: ['', [Validators.required,this.noWhitespaceValidator]]
});
this._loadingService.ShowLoading("global-loading");
this.GetUserById();
}
redirect() {
this.router.navigate(['/']);
}
public noWhitespaceValidator(control: FormControl) {
// new validation for intial whaite space
//****Birendra *****/
var isValid=false;
if(control.value!=null)
{
var controlLen=control.value.length;
if(controlLen!=0)
{
const isWhitespace = (control.value || '').trim().length === 0;
isValid = !isWhitespace;
if(!isValid)
{
control.setValue('');
}
}
}
// can use also on page of input control
//
return isValid ? null: { 'whitespace': true };
}
GetUserById() {
this.userservice.GetUserById()
.subscribe(x => { console.log(x); this.bindUsers(x) }, error => this.error = <any>error);
this._loadingService.HideLoading("global-loading");
}
UpdateUserProfile() {
this.alerts = '';
if (this.userFrm.value.firstName == null ||((this.userFrm.value.firstName).trim() == ''))
{
this.alerts += '<span>First name is required.</span></br>';
}
if (this.userFrm.value.lastName == null ||((this.userFrm.value.lastName).trim() == ''))
{
this.alerts += '<span>Last name is required.</span></br>';
}
if (this.userFrm.value.emailId == null ||((this.userFrm.value.emailId).trim() == '')) {
this.alerts += '<span>Email Id is required.</span>';
}
else if((!this.userFrm.controls.emailId.valid))
{
this.alerts += '<span>Enter Valid Email Id.</span>';
}
if (this.userFrm.valid && this.alerts == '') {
var obj = this.userFrm.value;
return this.userservice.UpdateUserProfileById(obj)
.subscribe(
n => (this.AfterInsertData(n)),
error => this.error = <any>error);
}
}
AfterInsertData(data) {
if (data.Status == "False") {
return false;
} else {
this.status = true;
this.user = this.userFrm.value;
var loggedInUser = JSON.parse(localStorage.getItem("loggedInUserDetails"));
loggedInUser.FirstName = this.userFrm.value.firstName;
loggedInUser.LastName = this.userFrm.value.lastName;
localStorage.setItem("loggedInUserDetails", JSON.stringify(loggedInUser));
this.global.DisplayName = loggedInUser.FirstName + " " + loggedInUser.LastName;
this._confirmService.activate("User Profile Updated Successfully.", "alertMsg");
}
}
bindUsers(data) {
//console.log(data);
//alert(JSON.stringify(data));
this.user = data[0];
console.log(this.user);
this.userFrm.controls['id'].setValue(this.user.Id)
this.userFrm.controls['firstName'].setValue(this.user.FirstName)
this.userFrm.controls['lastName'].setValue(this.user.LastName)
this.userFrm.controls['emailId'].setValue(this.user.EmailId)
}
validationMessages = {
'firstName': {
'required': 'First name is required.'
},
'lastName': {
'required': 'Last name is required.'
},
'email': {
'required': 'Email is required.',
'pattern': 'Email pattern is not valid.'
}
}
}