argos/dmp-frontend/src/app/homepage/homepage.component.ts

134 lines
5.0 KiB
TypeScript
Raw Normal View History

2018-11-27 18:33:17 +01:00
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';
2017-12-15 12:52:11 +01:00
import { DashboardService } from '../../app/services/dashboard/dashboard.service';
2018-11-27 18:33:17 +01:00
import { BaseComponent } from '../core/common/base/base.component';
import { ProjectCriteria } from '../models/criteria/project/ProjectCriteria';
import { RequestItem } from '../models/criteria/RequestItem';
2017-12-15 12:52:11 +01:00
import { DashboardStatisticsModel } from '../models/dashboard/DashboardStatisticsModel';
2018-11-27 18:33:17 +01:00
import { SearchBarItem } from '../models/dashboard/SearchBarItem';
2018-05-14 08:44:35 +02:00
import { AuthService } from '../services/auth/auth.service';
2018-08-24 17:21:02 +02:00
import { ProjectService } from '../services/project/project.service';
import { UserReferenceService } from '../services/user-reference/user-reference-data.service';
2018-11-27 18:33:17 +01:00
import { SingleAutoCompleteConfiguration } from '../shared/components/autocompletes/single/single-auto-complete-configuration';
2018-08-24 17:21:02 +02:00
import { SearchBarType } from '../shared/components/search-bar/types/search-bar-type';
2018-11-27 18:33:17 +01:00
import { RecentActivityTypes } from '../users/activity/RecentActivityTypes';
import { JsonSerializer } from '../utilities/JsonSerializer';
@Component({
2018-10-05 17:00:54 +02:00
selector: 'app-homepage',
templateUrl: './homepage.component.html',
styleUrls: ['./homepage.component.scss'],
providers: [ProjectService, UserReferenceService]
})
2018-11-27 18:33:17 +01:00
export class HomepageComponent extends BaseComponent implements OnInit {
2017-11-16 18:07:27 +01:00
2018-10-05 17:00:54 +02:00
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<SearchBarItem[]>;
RecentActivityTypes = RecentActivityTypes;
public search = false;
constructor(
private route: ActivatedRoute,
private router: Router,
private projectService: ProjectService,
private dashBoardService: DashboardService,
private authentication: AuthService,
private userReferenceService: UserReferenceService
) {
2018-11-27 18:33:17 +01:00
super();
2018-10-05 17:00:54 +02:00
this.dashboardStatisticsData.totalDataManagementPlanCount = 0;
this.dashboardStatisticsData.totalDataSetCount = 0;
this.dashboardStatisticsData.totalProjectCount = 0;
}
ngOnInit() {
if (this.isAuthenticated()) {
2018-11-27 18:33:17 +01:00
this.userReferenceService.getRecentActivity()
.pipe(takeUntil(this._destroyed))
.subscribe(response => {
this.datasetActivities = response['recentDatasetActivities'];
this.dmpActivities = response['recentDmpActivities'];
this.projectActivities = response['recentProjectActivities'];
});
2018-10-05 17:00:54 +02:00
}
this.projectAutoCompleteConfiguration = {
filterFn: this.searchProject.bind(this),
items: this.searchProject(''),
displayFn: (item) => item['label'],
titleFn: (item) => item['label'],
//mapFn: (item) => new JsonSerializer<ProjectReference>().fromJSONArray(item, ProjectReference).map(item => item.toDropdownList()),
loadDataOnStart: true
};
if (!this.isAuthenticated()) {
2018-11-27 18:33:17 +01:00
this.dashBoardService.getStatistics()
.pipe(takeUntil(this._destroyed))
.subscribe(results => {
//let data = results['payload'];
this.dashboardStatisticsData = JsonSerializer.fromJSONObject(results, DashboardStatisticsModel);
});
2018-10-05 17:00:54 +02:00
} else {
2018-11-27 18:33:17 +01:00
this.dashBoardService.getStatisticsSpecificuser()
.pipe(takeUntil(this._destroyed))
.subscribe(results => {
this.dashboardStatisticsData = JsonSerializer.fromJSONObject(results, DashboardStatisticsModel);
});
2018-10-05 17:00:54 +02:00
}
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<ProjectCriteria> = 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]); }
}
2018-05-28 11:50:42 +02:00
}