Files
smartguide/webapp/src/app/modules/institution-projects/institution-projects.component.ts
T

281 lines
8.0 KiB
TypeScript

import {
Component,
computed,
effect,
input,
OnInit,
TemplateRef,
viewChild,
} from '@angular/core';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import {
BundProjectPage,
EuCordisProjectSearchPage,
InstitutionFunderSummary,
EuCordisProjectSummary,
} from '../../api';
import { BundProjectSummary } from '../../api/model/bundProjectSummary';
import { MatCardModule } from '@angular/material/card';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatTabChangeEvent, MatTabsModule } from '@angular/material/tabs';
import { FormsModule } from '@angular/forms';
import { MatInputModule } from '@angular/material/input';
import {
GenericColumn,
DataTableComponent,
} from '../../shared/ui/data-table/data-table.component';
import { CurrencyPipe, DatePipe } from '@angular/common';
import { toSignal } from '@angular/core/rxjs-interop';
import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button';
import { InstitutionProjectsFundingGraphComponent } from '../institution-projects-funding-graph/institution-projects-funding-graph.component';
import { AppInfoCardComponent } from '../../shared/ui/app-info-card/app-info-card.component';
import { QueryParamPaginatorComponent } from '../../shared/ui/query-param-paginator/query-param-paginator.component';
import { QueryParamInputComponent } from '../../shared/ui/query-param-input/query-param-input.component';
type BundProjectRow = {
id: string;
topic: string;
funding_start: Date;
funding_end: Date;
funding_amount: number;
department: string;
};
type EuProjectRow = {
id: string;
topic: string;
funding_start: Date;
funding_end: Date;
funding_amount: number;
};
@Component({
selector: 'app-institution-projects',
imports: [
MatCardModule,
MatFormFieldModule,
MatTabsModule,
FormsModule,
MatInputModule,
DataTableComponent,
DatePipe,
CurrencyPipe,
MatIconModule,
MatButtonModule,
InstitutionProjectsFundingGraphComponent,
AppInfoCardComponent,
RouterModule,
QueryParamPaginatorComponent,
QueryParamInputComponent,
],
templateUrl: './institution-projects.component.html',
styleUrl: './institution-projects.component.scss',
})
export class InstitutionProjectsComponent implements OnInit {
fundingStartTmp = viewChild.required<TemplateRef<any>>('fundingStartTmp');
fundingEndTmp = viewChild.required<TemplateRef<any>>('fundingEndTmp');
fundingAmountTmp = viewChild.required<TemplateRef<any>>('fundingAmountTmp');
cordisEuProjectPage = input.required<EuCordisProjectSearchPage>();
euProjectColumns: GenericColumn<EuProjectRow>[] = [];
euProjectData: EuProjectRow[] = [];
euProjectActionsTmp = viewChild.required<TemplateRef<any>>(
'euProjectActionsTmp',
);
acronymTmp = viewChild.required<TemplateRef<any>>('acronymTmp');
bundProjectPage = input.required<BundProjectPage>();
bundProjectColumns: GenericColumn<BundProjectRow>[] = [];
bundProjectData: BundProjectRow[] = [];
bundProjectActionsTmp = viewChild.required<TemplateRef<any>>(
'bundProjectActionsTmp',
);
bundProjectsFunding = input.required<InstitutionFunderSummary>();
bundTotalFunding = computed(() => {
const funding = this.bundProjectsFunding();
return Object.values(
funding.funding_per_year as Record<string, number>,
).reduce((sum, yearFunding) => sum + yearFunding, 0);
});
bundAverageAnnualFunding = computed(() => {
const funding = this.bundProjectsFunding();
return funding.annual_average_over_period ?? 0;
});
cordisEuProjectsFunding = input.required<InstitutionFunderSummary>();
euTotalFunding = computed(() => {
const funding = this.cordisEuProjectsFunding();
return Object.values(
funding.funding_per_year as Record<string, number>,
).reduce((sum, yearFunding) => sum + yearFunding, 0);
});
euAverageAnnualFunding = computed(() => {
const funding = this.cordisEuProjectsFunding();
return funding.annual_average_over_period ?? 0;
});
constructor(
private readonly activatedRoute: ActivatedRoute,
private readonly router: Router,
) {
effect(() => {
const euPage = this.cordisEuProjectPage();
if (euPage) {
this.euProjectData = euPage.elements
? euPage.elements.map(this.transformEuRow.bind(this))
: [];
}
});
effect(() => {
const bundPage = this.bundProjectPage();
if (bundPage) {
this.bundProjectData = bundPage.elements
? bundPage.elements.map(this.transformBundRow.bind(this))
: [];
}
});
}
readonly queryParams = toSignal(this.activatedRoute.queryParamMap, {
initialValue: null,
});
readonly displayedProjects = computed(() => {
return this.queryParams()?.get('project_tab') ?? 'eu';
});
readonly selectedPage = computed(() => {
if (this.displayedProjects() === 'eu') {
return this.cordisEuProjectPage();
} else {
return this.bundProjectPage();
}
});
readonly pageTotal = computed(() => {
return this.selectedPage()?.total ?? 0;
});
ngOnInit(): void {
this.euProjectColumns = [
{
id: 'title',
header: 'Titel',
type: 'accessor',
accessorKey: 'topic',
},
{
id: 'funding_start',
header: 'Förderbeginn',
type: 'accessor',
accessorKey: 'funding_start',
cellTemplate: this.fundingStartTmp(),
},
{
id: 'funding_end',
header: 'Förderende',
type: 'accessor',
accessorKey: 'funding_end',
cellTemplate: this.fundingEndTmp(),
},
{
id: 'funding_amount',
header: 'Fördersumme (EC)',
type: 'accessor',
accessorKey: 'funding_amount',
cellTemplate: this.fundingAmountTmp(),
},
{
id: 'actions',
header: '',
type: 'display',
cellTemplate: this.euProjectActionsTmp(),
},
];
this.bundProjectColumns = [
{
id: 'title',
header: 'Titel',
type: 'accessor',
accessorKey: 'topic',
},
{
id: 'funding_start',
header: 'Förderbeginn',
type: 'accessor',
accessorKey: 'funding_start',
cellTemplate: this.fundingStartTmp(),
},
{
id: 'funding_end',
header: 'Fördersende',
type: 'accessor',
accessorKey: 'funding_end',
cellTemplate: this.fundingEndTmp(),
},
{
id: 'funding_amount',
header: 'Fördersumme',
type: 'accessor',
accessorKey: 'funding_amount',
cellTemplate: this.fundingAmountTmp(),
},
{
id: 'actions',
header: '',
type: 'display',
cellTemplate: this.bundProjectActionsTmp(),
},
];
}
transformEuRow(project: EuCordisProjectSummary): EuProjectRow {
return {
id: project.id,
topic: project.title || '',
funding_start: project.start_date
? new Date(project.start_date)
: new Date(0),
funding_end: project.end_date ? new Date(project.end_date) : new Date(0),
funding_amount: Number(project.ec_max_contribution) || 0,
};
}
transformBundRow(project: BundProjectSummary): BundProjectRow {
return {
id: project.funding_code || '',
topic: project.topic || '',
funding_start: project.funding_start
? new Date(project.funding_start)
: new Date(0),
funding_end: project.funding_end
? new Date(project.funding_end)
: new Date(0),
funding_amount: project.grant_amount || 0,
department: project.department_fullname || '',
};
}
readonly tabIndex = computed(() => {
switch (this.displayedProjects()) {
case 'eu':
return 0;
case 'bund':
return 1;
default:
return 0;
}
});
selectedTabChange(event: MatTabChangeEvent): void {
const tab = ['eu', 'bund'][event.index];
this.router.navigate([], {
queryParams: { project_tab: tab, page: 0, size: 10 },
queryParamsHandling: 'merge',
});
}
}