argos/dmp-frontend/src/app/ui/dashboard/dashboard.component.ts

123 lines
4.2 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/internal/Observable';
import { takeUntil } from 'rxjs/operators';
import { JsonSerializer } from '../../common/types/json/json-serializer';
import { BaseComponent } from '../../core/common/base/base.component';
import { RecentActivityType } from '../../core/common/enum/recent-activity-type';
import { DashboardStatisticsModel } from '../../core/model/dashboard/dashboard-statistics-model';
import { SearchBarItem } from '../../core/model/dashboard/search-bar-item';
import { ProjectCriteria } from '../../core/query/project/project-criteria';
import { AuthService } from '../../core/services/auth/auth.service';
import { DashboardService } from '../../core/services/dashboard/dashboard.service';
import { ProjectService } from '../../core/services/project/project.service';
import { SearchBarService } from '../../core/services/search-bar/search-bar.service';
import { UserService } from '../../core/services/user/user.service';
import { SingleAutoCompleteConfiguration } from '../../library/auto-complete/single/single-auto-complete-configuration';
import { RequestItem } from '../../core/query/request-item';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss'],
})
export class DashboardComponent extends BaseComponent implements OnInit {
public userInfo: any;
datasetActivities: any[];
projectActivities: any[];
dmpActivities: any[];
public dashboardStatisticsData: DashboardStatisticsModel;
public formControl = new FormControl();
projectAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
public searchControl = new FormControl();
filteredOptions: Observable<SearchBarItem[]>;
recentActivityTypeEnum = RecentActivityType;
public search = false;
constructor(
private router: Router,
private projectService: ProjectService,
private dashboardService: DashboardService,
private searchBarService: SearchBarService,
private authentication: AuthService,
private userService: UserService
) {
super();
// this.dashboardStatisticsData.totalDataManagementPlanCount = 0;
// this.dashboardStatisticsData.totalDataSetCount = 0;
// this.dashboardStatisticsData.totalProjectCount = 0;
}
ngOnInit() {
if (this.isAuthenticated()) {
this.userService.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']
// };
if (!this.isAuthenticated()) {
this.dashboardService.getStatistics()
.pipe(takeUntil(this._destroyed))
.subscribe(results => {
//let data = results['payload'];
this.dashboardStatisticsData = results;
});
} else {
this.dashboardService.getUserStatistics()
.pipe(takeUntil(this._destroyed))
.subscribe(results => {
this.dashboardStatisticsData = results;
});
}
this.filteredOptions = this.searchControl.valueChanges.flatMap(x => {
return this.searchBarService.search(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: RecentActivityType) {
switch (type) {
case RecentActivityType.Project: {
this.router.navigate(['projects/edit/' + id]);
return;
}
case RecentActivityType.Dataset: {
this.router.navigate(['datasets/edit/' + id]);
return;
}
case RecentActivityType.Dmp: {
this.router.navigate(['plans/edit/' + id]);
return;
}
default: throw new Error('Unsupported Activity Type ');
}
}
}