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==undefined)//undefined for integer value { isValid=true; } else 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 = error); } UpdateUserProfile() { this.alerts = ''; if (this.userFrm.value.firstName == null ||((this.userFrm.value.firstName).trim() == '')) { this.alerts += 'First name is required.
'; } if (this.userFrm.value.lastName == null ||((this.userFrm.value.lastName).trim() == '')) { this.alerts += 'Last name is required.
'; } if (this.userFrm.value.emailId == null ||((this.userFrm.value.emailId).trim() == '')) { this.alerts += 'Email Id is required.'; } else if((!this.userFrm.controls.emailId.valid)) { this.alerts += 'Enter Valid Email Id.'; } if (this.userFrm.valid && this.alerts == '') { var obj = this.userFrm.value; this._loadingService.ShowLoading("global-loading"); return this.userservice.UpdateUserProfileById(obj) .subscribe( n => (this.AfterInsertData(n)), error => this.error = error); } } AfterInsertData(data) { if (data.Status == "False") { this._loadingService.HideLoading("global-loading"); 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._loadingService.HideLoading("global-loading"); 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); this._loadingService.HideLoading("global-loading"); } validationMessages = { 'firstName': { 'required': 'First name is required.' }, 'lastName': { 'required': 'Last name is required.' }, 'email': { 'required': 'Email is required.', 'pattern': 'Email pattern is not valid.' } } }