import { Component, OnInit, ViewChild } from '@angular/core'; import { ContactHomeComponent } from '../../contact-home/contact-home.component'; import { CookieService } from 'ngx-cookie-service'; import { IRecentlyAdded } from '../../../shared/core/IRecentlyAdded'; import { DashboardService } from '../../../shared/service/dashboard.service'; import { debounceTime } from 'rxjs/operators'; import { DatePipe } from '@angular/common'; import { IDashboardCounts } from '../../../shared/core/IDashboardCounts'; import dayGridPlugin from '@fullcalendar/daygrid'; import timeGridPlugin from '@fullcalendar/timegrid'; import interactionPlugin from '@fullcalendar/interaction'; import { ISchedule } from '../../../shared/core/ISchedule'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-common-dashboard', templateUrl: './common-dashboard.component.html', styleUrls: ['./common-dashboard.component.css'], providers: [DatePipe, DashboardService] }) export class CommonDashboardComponent implements OnInit { dateFromAndTo: Date[] = [new Date, new Date]; loggedId: number = 1; loggedCompanyId: number = 1; @ViewChild(ContactHomeComponent) ContactHomeComponent: ContactHomeComponent; /** For me and myteam dropdown options */ drpdwn_contactOptions: any /** To keep me and myteam contact options values*/ drpdwn_contactOptions_selected: any recentlyAddedReports: IRecentlyAdded[] = []; statisticsCounts: IDashboardCounts; scheduleList: ISchedule[] = []; events: any[]; options: any; displayContactHome:boolean = false; constructor(private cookieService: CookieService, private route: ActivatedRoute, private dashboardService: DashboardService, private datePipe: DatePipe) { } ngOnInit() { this.route.paramMap.subscribe(params => { this.loggedId = +params.get("loggedId"); this.loggedCompanyId = +params.get("loggedCompanyId"); }) this.cookieService.set('CompanyId', this.loggedCompanyId.toString()); this.cookieService.set('LoggedUserId', this.loggedId.toString()); this.options = { plugins: [dayGridPlugin, timeGridPlugin, interactionPlugin], defaultDate: this.datePipe.transform(Date(), "yyyy-MM-dd"), header: { left: 'prev,next', center: 'title', right: 'dayGridMonth,timeGridWeek,timeGridDay' }, editable: false }; /** For Me and MyTeam */ this.drpdwn_contactOptions = [ { label: 'Me', value: "ME" }, { label: 'My team', value: "MYTEAM" }, ]; this.drpdwn_contactOptions_selected = this.drpdwn_contactOptions[0]['value']; //getting the recently added reports let loggedUserId = +this.cookieService.get('LoggedUserId'); let fromDate = this.datePipe.transform((this.dateFromAndTo[0] == null) ? Date() : this.dateFromAndTo[0].toString(), "MM-dd-yyyy"); let toDate = this.datePipe.transform((this.dateFromAndTo[1] == null) ? Date() : this.dateFromAndTo[1].toString(), "MM-dd-yyyy"); // Getting the statistics counts this.dashboardService.GetAllDashboardReportCounts(fromDate, toDate, this.drpdwn_contactOptions_selected, loggedUserId, +this.cookieService.get('CompanyId')).subscribe( data => { this.statisticsCounts = data; this.displayContactHome = true; // Getting the recently added contacts this.dashboardService.GetRecentlyAddedList(fromDate, toDate, this.drpdwn_contactOptions_selected, loggedUserId, +this.cookieService.get('CompanyId')).subscribe( data => { this.recentlyAddedReports = data; } ); // Getting the schedule list this.dashboardService.GetScheduleList(fromDate, toDate, this.drpdwn_contactOptions_selected, loggedUserId).subscribe( data => { this.scheduleList = data; this.events = []; for (let i = 0; i < this.scheduleList.length; i++) { this.events.push( { "title": this.scheduleList[i].title, "start": this.datePipe.transform(this.scheduleList[i].start, "yyyy-MM-dd"), "end": this.datePipe.transform(this.scheduleList[i].end, "yyyy-MM-dd") } ); } } ); // ------------------- End ---------// } ); } // My Team and Me on change filterResultsByContactOption() { this.ContactHomeComponent.filterResultsByContactOptionFromParent(this.drpdwn_contactOptions_selected); this.RefreshReports(); } RefreshReports() { let fromDate = this.datePipe.transform((this.dateFromAndTo[0] == null) ? Date() : this.dateFromAndTo[0].toString(), "MM-dd-yyyy"); let toDate = this.datePipe.transform((this.dateFromAndTo[1] == null) ? Date() : this.dateFromAndTo[1].toString(), "MM-dd-yyyy"); let loggedUserId = +this.cookieService.get('LoggedUserId'); // Getting the recently added contacts if (this.dateFromAndTo[0] !== null && this.dateFromAndTo[1] !== null) { this.dashboardService.GetRecentlyAddedList(fromDate, toDate, this.drpdwn_contactOptions_selected, loggedUserId, +this.cookieService.get('CompanyId')).subscribe( data => { this.recentlyAddedReports = data; } ); } // Getting the statistics counts this.dashboardService.GetAllDashboardReportCounts(fromDate, toDate, this.drpdwn_contactOptions_selected, loggedUserId, +this.cookieService.get('CompanyId')).subscribe( data => { this.statisticsCounts = data; } ); // Getting the schedule list this.dashboardService.GetScheduleList(fromDate, toDate, this.drpdwn_contactOptions_selected, loggedUserId).subscribe( data => { this.scheduleList = data; this.events = []; for (let i = 0; i < this.scheduleList.length; i++) { this.events.push( { "title": this.scheduleList[i].title, "start": this.datePipe.transform(this.scheduleList[i].start, "yyyy-MM-dd"), "end": this.datePipe.transform(this.scheduleList[i].end, "yyyy-MM-dd") } ); } } ); } }