import { Component, OnInit } from '@angular/core'; import { FormControl } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; import { Observable } from 'rxjs/internal/Observable'; import { takeUntil } from 'rxjs/operators'; import { DashboardService } from '../../app/services/dashboard/dashboard.service'; import { BaseComponent } from '../core/common/base/base.component'; import { ProjectCriteria } from '../models/criteria/project/ProjectCriteria'; import { RequestItem } from '../models/criteria/RequestItem'; import { DashboardStatisticsModel } from '../models/dashboard/DashboardStatisticsModel'; import { SearchBarItem } from '../models/dashboard/SearchBarItem'; import { AuthService } from '../services/auth/auth.service'; import { ProjectService } from '../services/project/project.service'; import { UserReferenceService } from '../services/user-reference/user-reference-data.service'; import { SingleAutoCompleteConfiguration } from '../shared/components/autocompletes/single/single-auto-complete-configuration'; import { SearchBarType } from '../shared/components/search-bar/types/search-bar-type'; import { RecentActivityTypes } from '../users/activity/RecentActivityTypes'; import { JsonSerializer } from '../utilities/JsonSerializer'; @Component({ selector: 'app-homepage', templateUrl: './homepage.component.html', styleUrls: ['./homepage.component.scss'], providers: [ProjectService, UserReferenceService] }) export class HomepageComponent extends BaseComponent implements OnInit { public userInfo: any; datasetActivities: any[]; projectActivities: any[]; dmpActivities: any[]; public dashboardStatisticsData: DashboardStatisticsModel = new DashboardStatisticsModel(); public formControl = new FormControl(); projectAutoCompleteConfiguration: SingleAutoCompleteConfiguration; public searchControl = new FormControl(); filteredOptions: Observable; RecentActivityTypes = RecentActivityTypes; public search = false; constructor( private route: ActivatedRoute, private router: Router, private projectService: ProjectService, private dashBoardService: DashboardService, private authentication: AuthService, private userReferenceService: UserReferenceService ) { super(); this.dashboardStatisticsData.totalDataManagementPlanCount = 0; this.dashboardStatisticsData.totalDataSetCount = 0; this.dashboardStatisticsData.totalProjectCount = 0; } ngOnInit() { if (this.isAuthenticated()) { this.userReferenceService.getRecentActivity() .pipe(takeUntil(this._destroyed)) .subscribe(response => { this.datasetActivities = response['recentDatasetActivities']; this.dmpActivities = response['recentDmpActivities']; this.projectActivities = response['recentProjectActivities']; }); } this.projectAutoCompleteConfiguration = { filterFn: this.searchProject.bind(this), items: this.searchProject(''), displayFn: (item) => item['label'], titleFn: (item) => item['label'], //mapFn: (item) => new JsonSerializer().fromJSONArray(item, ProjectReference).map(item => item.toDropdownList()), loadDataOnStart: true }; if (!this.isAuthenticated()) { this.dashBoardService.getStatistics() .pipe(takeUntil(this._destroyed)) .subscribe(results => { //let data = results['payload']; this.dashboardStatisticsData = JsonSerializer.fromJSONObject(results, DashboardStatisticsModel); }); } else { this.dashBoardService.getStatisticsSpecificuser() .pipe(takeUntil(this._destroyed)) .subscribe(results => { this.dashboardStatisticsData = JsonSerializer.fromJSONObject(results, DashboardStatisticsModel); }); } this.filteredOptions = this.searchControl.valueChanges.flatMap(x => { return this.dashBoardService.searchUserItems(x); }); } public isAuthenticated(): boolean { return !(!this.authentication.current()); } searchProject(query: string) { const projectRequestItem: RequestItem = new RequestItem(); projectRequestItem.criteria = new ProjectCriteria(); projectRequestItem.criteria.like = query; return this.projectService.getWithExternal(projectRequestItem); } redirect(id: string, type: RecentActivityTypes) { switch (type) { case RecentActivityTypes.PROJECT: { this.router.navigate(['projects/edit/' + id]); return; } case RecentActivityTypes.DATASET: { this.router.navigate(['datasets/edit/' + id]); return; } case RecentActivityTypes.DMP: { this.router.navigate(['dmps/edit/' + id]); return; } default: throw new Error('Unsupported Activity Type '); } } onOptionSelected(event: any) { const selectedSearchBarItem = event.option.value; if (selectedSearchBarItem.type === SearchBarType.DATASET) { this.router.navigate(['datasets/edit/' + selectedSearchBarItem.id]); } if (selectedSearchBarItem.type === SearchBarType.PROJECT) { this.router.navigate(['projects/edit/' + selectedSearchBarItem.id]); } if (selectedSearchBarItem.type === SearchBarType.DATAMANAGEMENTPLAN) { this.router.navigate(['dmps/edit/' + selectedSearchBarItem.id]); } } }