import { Component, ElementRef, OnDestroy, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { CommonService } from '../../services/common.service'; import { DataTableDirective } from 'angular-datatables'; import { Location } from '@angular/common'; import { MatTreeNestedDataSource } from '@angular/material/tree'; import { NestedTreeControl } from '@angular/cdk/tree'; import { AdminService } from 'src/app/services/adminuser-profile/adminuser-profile.service'; import { User } from 'src/app/models/user-profile/user-profile'; interface LogNode { name: string; children?: LogNode[]; } const TREE_DATA: LogNode[] = []; @Component({ selector: 'app-userprofile', templateUrl: './userprofile.component.html', styleUrls: ['./userprofile.component.css'] }) export class UserprofileComponent implements OnInit, OnDestroy { modelEditProfile = new User(); EditProfileFormcontact: FormGroup; formData: FormData = new FormData(); UserAPIResponse: any; email: string = sessionStorage.getItem('Email'); loggedInUserType: string = sessionStorage.getItem('UserType'); addressnumber: Array = sessionStorage.getItem('MapingCustomer').split(','); @ViewChild('#accordion') closeBtn: ElementRef; collapse: boolean = false; _paramId: string; ISpasswordMatch: boolean = true; imageName_PS: string = ''; imageData_PS: string = './assets/img/profile.png'; defaultImage: string = './assets/img/profile.png'; imageType: string = ""; ResultData: any[]; CSUserDetailAPIResponse: any; @ViewChildren(DataTableDirective) dtElements: QueryList; IsWrongFileFormate: boolean = false; CSUserAPIResponse: any; SaveShipToAddressAPIResponse: any; logFolderContents: any; treeControl = new NestedTreeControl(node => node.children); dataSource = new MatTreeNestedDataSource(); activeNode; constructor( private formBuilder: FormBuilder, private common: CommonService, private adminservice: AdminService, private elementRef: ElementRef, private location: Location ) { } hasChild = (_: number, node: LogNode) => !!node.children && node.children.length > 0; changeState(node) { node.expanded = !node.expanded; } ngOnInit(): void { this.setFormBuilder(); this.adminservice.getById(Number(sessionStorage.getItem('UserId')), data => { this.UserAPIResponse = data; this.modelEditProfile = this.UserAPIResponse.objResponse.responseContent; this.imageData_PS = this.defaultImage; this.setConactForm(); }, (error) => { console.error('Error: ' + error); return error(error); }); } setFormBuilder() { this.EditProfileFormcontact = this.formBuilder.group({ UserId: 0, FirstName: ['', Validators.required], LastName: ['', Validators.required], Email: [null, Validators.compose([ Validators.email, Validators.required]) ], CompanyName: [''], JobTitle: ['', Validators.required], Mobile: ['', [ Validators.minLength(11)]], OfficeNumber: ['', [Validators.required, Validators.minLength(11)]], Fax: ['', [Validators.minLength(11)]], // Fax: ['', [Validators.pattern(/^\-\d{3}\-\d{3}-\d{4}$/)]], IsActive: ['1'], }); } setConactForm() { this.EditProfileFormcontact.setValue({ UserId: this.modelEditProfile.UserId, FirstName: this.modelEditProfile.FirstName == null ? "" : this.modelEditProfile.FirstName, LastName: this.modelEditProfile.LastName == null ? "" : this.modelEditProfile.LastName, Email: this.modelEditProfile.Email == null ? "" : this.modelEditProfile.Email, CompanyName: this.modelEditProfile.CompanyId == 1 ? "Cornerstone Building Brands" : sessionStorage.getItem('GlobalSearchCustomerName'), JobTitle: this.modelEditProfile.JobTitle == null ? "" : this.modelEditProfile.JobTitle, Mobile: this.modelEditProfile.Mobile == null ? "" : this.modelEditProfile.Mobile, OfficeNumber: this.modelEditProfile.OfficeNumber == null ? "" : this.modelEditProfile.OfficeNumber, Fax: this.modelEditProfile.Fax == null ? "" : this.modelEditProfile.Fax, IsActive: this.modelEditProfile.IsActive == null ? "" : this.modelEditProfile.IsActive, }); // this.EditProfileFormcontact.updateValueAndValidity(); } SaveProfileInfo() { if (!this.EditProfileFormcontact.invalid) { this.modelEditProfile.OfficeNumber = this.EditProfileFormcontact.value.OfficeNumber.replace(/-/g, ""); this.modelEditProfile.Mobile = this.EditProfileFormcontact.value.Mobile.replace(/-/g, ""); this.modelEditProfile.Fax = this.EditProfileFormcontact.value.Fax.replace(/-/g, ""); this.modelEditProfile.FirstName = this.EditProfileFormcontact.value.FirstName; this.modelEditProfile.LastName = this.EditProfileFormcontact.value.LastName; this.modelEditProfile.JobTitle = this.EditProfileFormcontact.value.JobTitle; // this.modelEditProfile = this.EditProfileFormcontact.value; this.common.checkUserExistence(); this.adminservice.updateUser(this.modelEditProfile, data => { this.UserAPIResponse = data; if (this.UserAPIResponse.objResponse.statusCode == "200") { this.common.showSuccess("Successfully Updated", "Success") } else { this.common.showError("Successfully Updated", "Upate Fail") } }, (error) => { console.error('Error: ' + error); return error(error); }); } else { this.common.showError("Please fill required fields.", "Validation Alert"); Object.keys(this.EditProfileFormcontact.controls).forEach(field => { const control = this.EditProfileFormcontact.get(field); control.markAsTouched({ onlySelf: true }); }); for (const key of Object.keys(this.EditProfileFormcontact.controls)) { if (this.EditProfileFormcontact.controls[key].invalid) { const invalidControl = this.elementRef.nativeElement.querySelector('[formcontrolname="' + key + '"]'); invalidControl.focus(); break; } } } } ngOnDestroy(): void { } back() { this.location.back(); } }