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 { Global } 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 ) { } ngOnInit(): void { this.user = new User(); this.alerts = ''; //this.userservice.GetUserById(this.UserId); this.userFrm = this.fb.group({ id: [''], firstName: ['', Validators.required], lastName: ['', Validators.required], emailId: ['', Validators.required] // LastName: [''], // Gender: ['', Validators.required], // Email: [''] }); this._loadingService.ShowLoading("global-loading"); this.GetUserById(); } redirect() { this.router.navigate(['/']); } //ngAfterviewint() { // this.LoadUsers(); //} //getCustomerById(UserId) { // return this.userservice.GetUserById(UserId) // .map((response: Response) => response.json()) // .catch(this._errorHandler) //} //formErrors = { // 'firstName': '', // 'lastName': '', // 'email': '' //}; GetUserById() { this.userservice.GetUserById() .subscribe(x => { console.log(x); this.bindUsers(x) }, error => this.error = error); this._loadingService.HideLoading("global-loading"); } UpdateUserProfile(this) { // debugger; this.user = this.userFrm.value; //if(this.user.) //console.log(this.user); var obj = this.user if (this.userFrm.valid) { return this.userservice.UpdateUserProfileById(obj) .subscribe( n => (this.AfterInsertData(n)), error => this.error = error); } } AfterInsertData(data) { //debugger; if (data.Status == "False") { // this._confirmService.activate(data.ResponseMessage, "alertMsg"); //setTimeout(() => this.amCode.nativeElement.focus(), 0); // this.closeflag = false; return false; } else { this.status = true; this._confirmService.activate("User Profile Updated Successfully.", "alertMsg"); //this.profileModal.open(); // this.submitted = false; // this.GetAllAcctMgr(); // this.DisableAllControls(); // this.AccountManagerID.enable(); // let accountManagerID: string = data.Id == null ? "" : data.Id.toString(); // console.log(accountManagerID); // this.GetAcctMgr(Number(accountManagerID)); //this.GetAccountManagerListOptions(false); // this.defautValue = data.id; // this.BtnReset = true; // this.BtnSave = true; // this.BtnEdit = false; // this.BtnNew = false; // this.BtnDelete = false; // this.Abbrevtion = this.Abbrev.value; // setTimeout(() => this.AMform.controls["AccountManagerID"].setValue(accountManagerID.toString()), 1000); // setTimeout(() => this.AMform.markAsPristine(), 2000); } //if (this.closeflag) { // this.close.emit(null); //} //else { //} } 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.GetClientListOptions(false); // this.GetCrossRefClientListOptions(false); } //extractData(res: Response) { // //debugger; // let body = res.json(); // return body; //} //handleError(error: any) { // // In a real world app, we might use a remote logging infrastructure // // We'd also dig deeper into the error to get a better message // let errMsg = (error.message) ? error.message : // error.status ? `${error.status} - ${error.statusText}` : 'Server error'; // console.error(errMsg); // log to console instead // return Observable.throw(errMsg); //} validationMessages = { 'firstName': { 'required': 'First name is required.' }, 'lastName': { 'required': 'Last name is required.' }, 'email': { 'required': 'Email is required.', 'pattern': 'Email pattern is not valid.' } } }