//Importing the libraries import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core'; import { formatDate } from "@angular/common"; import { Router } from '@angular/router'; import { DialogService } from 'primeng/api'; //Importing the core classes import { IOptions } from '../../shared/core/IOptions'; import { IContactDetails } from '../../shared/core/IContactDetails'; import { ISelectOptionList } from '../../shared/core/ISelectOptionList'; //Importing the service classes import { DashboardService } from '../../shared/service/dashboard.service'; import { CompanyService } from '../../shared/service/company.service'; import { BehaviorSubjectService } from '../../shared/service/behavior-subject.service'; //Importing the components import { BulkDeletionConfirmComponent } from '../popups/bulk-deletion-confirm/bulk-deletion-confirm.component'; import { DatePipe } from '@angular/common'; import { IFilterKey } from '../../shared/core/IFilterKey'; import { CookieService } from 'ngx-cookie-service'; @Component({ selector: 'app-contact-list', templateUrl: './contact-list.component.html', styleUrls: ['./contact-list.component.scss'], providers: [DialogService] }) export class ContactListComponent implements OnInit { public cars: any[]; @Input() LoadData: boolean = false; @Input() OptionsObj: IOptions; @Input() noOfRecordsPerPage_selected: number = 20; @Input() filterKeys: IFilterKey[]; @Output() filterListOutput: EventEmitter = new EventEmitter(); @Output() filterResultsByNoOfRecordsInPaginator: EventEmitter = new EventEmitter(); /** For tables */ public cols: any[] public cols1: any[] public isExpanded: boolean = false public rows: number = 10 public expandedRows = {} public temDataLength: number = 0 /** For bulk action dropdown */ public bulkActions: any[]; selectedAction: any; /** For checkboxes */ selectedValues: string[] = [] selectedValues1: string[] = [] selectedCompanyContactList: any[] = []; contactOptions: IOptions; //contactList : IContactDetails[] = [] contactList: any[] = [] contactLstFrTblRendering: any[] = [] //get number of rows in returned data list contactListCount: number = 0; filterList: ISelectOptionList[] = []; msgs: any; /** for number of records */ noOfRecordsPerPage: any /** for bulk action validation*/ isSelectContacts: boolean = true /** for bulk deletion reply */ bulkDeletionReply: string allCount: number = 0; datePipe: any; constructor( private dashboardService: DashboardService, private companyService: CompanyService, private router: Router, public dialogService: DialogService, private cookieService: CookieService, public behaviorSubjectService: BehaviorSubjectService) { } ngOnChange() { if (this.filterKeys != []) { console.log(this.filterKeys); } } /** ngOnInit() starts */ ngOnInit() { this.datePipe = new DatePipe('en-US'); this.msgs = []; this.cols = [ { field: 'fullName', header: 'Full name' }, { field: 'email', header: 'Email' }, { field: 'company', header: 'Company' }, { field: 'contact_status', header: 'Contact Status' }, { field: 'last_contacted', header: 'Last contacted' }, { field: 'last_activity', header: 'Last activity' } ]; //preparing parameters and options to retrieve contact list, this collection is the one accepts as the parameter in API this.contactOptions = this.OptionsObj; //calling contact list method this.getContactList(this.contactOptions); /** List of the activity table columns */ this.cols1 = [ { field: 'type', header: 'Type', width: '20%' }, { field: 'stage', header: 'Stage', width: '10%' }, { field: 'status', header: 'Status', width: '15%' }, { field: 'subject', header: 'Subject', width: '10%' }, { field: 'due_date', header: 'Due date', width: '10%' }, { field: 'time', header: 'Time', width: '10%' }, ]; /** Data for bulk actions dropdown */ this.bulkActions = [ { label: 'Bulk Action', value: null }, { label: 'Copy', value: null }, { label: 'Paste', value: null }, { label: 'Delete', value: { id: 3, name: 'Delete' } }]; this.noOfRecordsPerPage = 1; this.dashboardService.currentNoOfRecords.subscribe((noOfRecords: number) => { this.noOfRecordsPerPage = noOfRecords; }); } /** Ends - ngOnInit() */ /** Bulk action dropdown onChange() */ onChangeDropdown(event) { if (event.value != null) { if (event.value['name'] == 'Delete') /** check contacts are selected */ if (this.selectedValues.length == 0) { this.isSelectContacts = false; } else { this.isSelectContacts = true; /** show bulk delete confirmation popup */ this.show(this.selectedValues); } } //console.log(event.value); } /** delete contacts method */ disableContacts() { let comContactIds = [] //To pass data into service this.selectedValues.forEach(element => { comContactIds.push(element); }); this.companyService.DisableCompanyContact(comContactIds).subscribe( (data: any) => { //To display in the view contact deletion this.selectedValues.forEach(element => { this.contactLstFrTblRendering = this.contactLstFrTblRendering.filter(function (item) { return item.companyContactsId != element }); }); }); } /** Bulk deletion popup */ show(selectedContacts: string[]) { let ref = this.dialogService.open(BulkDeletionConfirmComponent, { data: { selectedContacts: selectedContacts }, header: 'Confirm Bulk Deletion', width: '70%', contentStyle: { "max-height": "350px", "overflow": "auto" } }); ref.onClose.subscribe(() => { this.bulkDeletionReply = this.dialogService.dialogComponentRef.instance.componentRef.instance.reply if (this.bulkDeletionReply == "Yes") { this.disableContacts() } }); } /** this will call service and pass params to call the api */ getContactList(options: IOptions) { this.contactLstFrTblRendering = []; this.filterList = []; this.msgs = []; this.contactList = []; this.dashboardService.getContactList(options, +this.cookieService.get('CompanyId')).subscribe(res => { this.contactList = []; this.contactList = res; this.contactListCount = res.length; if (this.contactListCount > 0) { this.allCount = this.contactList[0]['AllCount']; } else { this.allCount = 0; } if (this.contactListCount < 1) { this.msgs.push({ severity: 'error', summary: '', detail: 'No result found!' }); } // preparing fiter option list if (this.contactList.length > 0) { var filterByType = this.contactList.filter(x => x.CompanyDetails); filterByType.forEach(element => { this.filterList.push({ label: 'Company:' + element.CompanyDetails.CompanyName, value: 'Company:' + element.CompanyDetails.CompanyName }) }); } //preparing from date var dateFrom = formatDate(new Date(), 'yyyy-MM-dd', 'en-US'); //preparing todate for 30 dayes var dateTo = formatDate(this.addDays(new Date(), 30), 'yyyy-MM-dd', 'en-US'); this.filterList.push({ label: 'Last activity date to now > 30 days', value: "activitydate:" + dateFrom + ',' + dateTo }); //preparing to date for 60 days dateTo = formatDate(this.addDays(new Date(), 60), 'yyyy-MM-dd', 'en-US'); this.filterList.push({ label: 'Last activity date to now > 60 days', value: "activitydate:" + dateFrom + ',' + dateTo }); //preparing to date for 90 days dateTo = formatDate(this.addDays(new Date(), 90), 'yyyy-MM-dd', 'en-US'); this.filterList.push({ label: 'Last activity date to now > 90 days', value: "activitydate:" + dateFrom + ',' + dateTo }); this.filterList.push({ label: 'Custom - Last activity', value: 'CustomLastActivity' }); //preparing todate for 30 dayes dateTo = formatDate(this.addDays(new Date(), 30), 'yyyy-MM-dd', 'en-US'); this.filterList.push({ label: 'Date joined to now > 30 days', value: "joindate:" + dateFrom + ',' + dateTo }); //preparing to date for 60 days dateTo = formatDate(this.addDays(new Date(), 60), 'yyyy-MM-dd', 'en-US'); this.filterList.push({ label: 'Date joined date to now > 60 days', value: "joindate:" + dateFrom + ',' + dateTo }); //preparing to date for 90 days dateTo = formatDate(this.addDays(new Date(), 90), 'yyyy-MM-dd', 'en-US'); this.filterList.push({ label: 'Date joined date to now > 90 days', value: "joindate:" + dateFrom + ',' + dateTo }); this.filterList.push({ label: 'Custom - Date joined', value: 'CustomDateJoined' }); //output options list for dropdown this.filterListOutput.emit(this.filterList); //end of filter options //debugger; this.contactLstFrTblRendering = []; this.contactList.forEach(element => { this.contactLstFrTblRendering.push({ companyContactsId: element.CompanyContactsId, contactId: element.ContactDetails.ContactDetailsId, fullName: element.ContactDetails.FirstName + " " + element.ContactDetails.LastName, email: element.Email, company: element.CompanyDetails.CompanyName, contact_status: element.ContactStatus.statusName, last_contacted: element.Lastcontacted, last_activity: element.LastActivity, tasks: [] }); }); //console.log(this.contactLstFrTblRendering); }); } expandAll() { if (!this.isExpanded) { this.cars.forEach(data => { this.expandedRows[data.Full_name] = 1; }) } else { this.expandedRows = {}; } this.isExpanded = !this.isExpanded; } onRowExpand() { //console.log("row expanded", Object.keys(this.expandedRows).length); if (Object.keys(this.expandedRows).length === this.temDataLength) { this.isExpanded = true; } } onRowCollapse() { //console.log("row collapsed", Object.keys(this.expandedRows).length); if (Object.keys(this.expandedRows).length === 0) { this.isExpanded = false; } } onPage(event: any) { this.temDataLength = this.cars.slice(event.first, event.first + 10).length; //console.log(this.temDataLength); this.isExpanded = false; this.expandedRows = {}; } //Contact name on click funtion contactNameOnClick(contactId: number) { //Redirecting to the contact details page this.router.navigate(['contact', contactId]); } addDays(date: Date, days: number): Date { date.setDate(date.getDate() + days); return date; } //Paginator function paginate(event) { //event.first = Index of the first record //event.rows = Number of rows to display in new page //event.page = Index of the new page //event.pageCount = Total number of pages //console.log("Page number: "+event.page); this.filterResultsByNoOfRecordsInPaginator.emit(event.page + 1); } filterContactListByParent(filterKeys: IFilterKey[]) { console.log("Filter Keys"); console.log(filterKeys); this.dashboardService.GetContactListWithFilter(this.contactOptions, filterKeys).subscribe( res => { this.contactList = []; this.contactList = res; this.contactListCount = res.length; this.allCount = this.contactListCount; // if (res.length > 0) { // this.allCount = this.contactList[0]['AllCount']; // } else { // this.allCount = 0; // } if (this.contactListCount < 1) { this.msgs = []; this.msgs.push({ severity: 'error', summary: '', detail: 'No result found!' }); } else { this.msgs = []; } this.contactLstFrTblRendering = []; this.contactList.forEach(element => { this.contactLstFrTblRendering.push({ companyContactsId: element.CompanyContactsId, contactId: element.ContactDetails.ContactDetailsId, fullName: element.ContactDetails.FirstName + " " + element.ContactDetails.LastName, email: element.Email, company: element.CompanyDetails.CompanyName, contact_status: element.ContactStatus.statusName, last_contacted: element.Lastcontacted, last_activity: element.LastActivity, tasks: [] }); }); } ); } contactCheckBoxOnChange(e) { // console.log(this.selectedCompanyContactList); this.behaviorSubjectService.contactIdListObjectBehavior.next(this.selectedCompanyContactList); } }