324 lines
8.8 KiB
TypeScript
324 lines
8.8 KiB
TypeScript
import {
|
|
Component,
|
|
computed,
|
|
effect,
|
|
input,
|
|
OnInit,
|
|
signal,
|
|
TemplateRef,
|
|
viewChild,
|
|
} from '@angular/core';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import {
|
|
BundProject,
|
|
BundProjectPage,
|
|
EuProject,
|
|
EuProjectPage,
|
|
InstitutionFunderSummary,
|
|
} from '../../api';
|
|
import { MatCardModule } from '@angular/material/card';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatTabChangeEvent, MatTabsModule } from '@angular/material/tabs';
|
|
import { MatPaginatorModule, PageEvent } from '@angular/material/paginator';
|
|
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 { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
|
|
import { Sort, SortDirection } from '@angular/material/sort';
|
|
import { DebounceDirective } from '../../directives/debounce.directive';
|
|
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 { PersonNetworkInfoCardComponent } from '../person-network-info-card/person-network-info-card.component';
|
|
|
|
type BundProjectRow = {
|
|
topic: string;
|
|
funding_start: Date;
|
|
funding_end: Date;
|
|
grant_amount: number;
|
|
department: string;
|
|
};
|
|
|
|
type EuProjectRow = {
|
|
topic: string;
|
|
funding_start: Date;
|
|
funding_end: Date;
|
|
grant_amount: number;
|
|
};
|
|
|
|
@Component({
|
|
selector: 'app-institution-projects',
|
|
imports: [
|
|
MatCardModule,
|
|
MatFormFieldModule,
|
|
MatTabsModule,
|
|
MatPaginatorModule,
|
|
FormsModule,
|
|
MatInputModule,
|
|
DataTableComponent,
|
|
DatePipe,
|
|
CurrencyPipe,
|
|
DebounceDirective,
|
|
MatIconModule,
|
|
MatButtonModule,
|
|
InstitutionProjectsFundingGraphComponent,
|
|
PersonNetworkInfoCardComponent,
|
|
],
|
|
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');
|
|
grantAmountTmp = viewChild.required<TemplateRef<any>>('grantAmountTmp');
|
|
|
|
euProjectPage = input.required<EuProjectPage>();
|
|
euProjectColumns: GenericColumn<EuProjectRow>[] = [];
|
|
euProjectData: EuProjectRow[] = [];
|
|
|
|
bundProjectPage = input.required<BundProjectPage>();
|
|
bundProjectColumns: GenericColumn<BundProjectRow>[] = [];
|
|
bundProjectData: BundProjectRow[] = [];
|
|
|
|
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;
|
|
});
|
|
|
|
urlSortKey = signal<string>('');
|
|
urlSortDirection = signal<SortDirection>('');
|
|
urlSearchTerm = signal<string>('');
|
|
|
|
constructor(
|
|
private readonly activatedRoute: ActivatedRoute,
|
|
private readonly router: Router,
|
|
) {
|
|
this.activatedRoute.queryParams
|
|
.pipe(takeUntilDestroyed())
|
|
.subscribe((params) => {
|
|
this.urlSortKey.set(params['sort_by'] ?? '');
|
|
this.urlSortDirection.set(
|
|
(params['sort_direction'] as SortDirection) ?? '',
|
|
);
|
|
this.urlSearchTerm.set(params['search'] ?? '');
|
|
});
|
|
|
|
effect(() => {
|
|
const euPage = this.euProjectPage();
|
|
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))
|
|
: [];
|
|
}
|
|
});
|
|
|
|
effect(() => {
|
|
const searchTerm = this.urlSearchTerm();
|
|
if (searchTerm) {
|
|
this.router.navigate([], {
|
|
queryParams: { search: searchTerm, page: 0, size: 10 },
|
|
queryParamsHandling: 'merge',
|
|
});
|
|
}
|
|
});
|
|
|
|
effect(() => {
|
|
console.log('Funding over time data:', this.bundProjectsFunding());
|
|
});
|
|
}
|
|
|
|
readonly queryParams = toSignal(this.activatedRoute.queryParamMap, {
|
|
initialValue: null,
|
|
});
|
|
|
|
readonly displayedProjects = computed(() => {
|
|
return this.queryParams()?.get('project_tab') ?? 'eu';
|
|
});
|
|
|
|
readonly page = computed(() => {
|
|
const pageParam = this.queryParams()?.get('page');
|
|
const page = pageParam ? Number.parseInt(pageParam, 10) : 0;
|
|
return Number.isNaN(page) ? 0 : page;
|
|
});
|
|
|
|
readonly selectedPage = computed(() => {
|
|
if (this.displayedProjects() === 'eu') {
|
|
return this.euProjectPage();
|
|
} else {
|
|
return this.bundProjectPage();
|
|
}
|
|
});
|
|
|
|
readonly pageTotal = computed(() => {
|
|
return this.selectedPage()?.total ?? 0;
|
|
});
|
|
|
|
readonly pageSize = computed(() => {
|
|
return this.selectedPage()?.size ?? 10;
|
|
});
|
|
|
|
readonly pageIndex = computed(() => {
|
|
return this.selectedPage()?.page ?? 0;
|
|
});
|
|
|
|
ngOnInit(): void {
|
|
this.euProjectColumns = [
|
|
{
|
|
id: 'topic',
|
|
header: 'Thema',
|
|
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: 'grant_amount',
|
|
header: 'Fördersumme',
|
|
type: 'accessor',
|
|
accessorKey: 'grant_amount',
|
|
cellTemplate: this.grantAmountTmp(),
|
|
},
|
|
];
|
|
this.bundProjectColumns = [
|
|
{
|
|
id: 'topic',
|
|
header: 'Thema',
|
|
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: 'grant_amount',
|
|
header: 'Fördersumme',
|
|
type: 'accessor',
|
|
accessorKey: 'grant_amount',
|
|
cellTemplate: this.grantAmountTmp(),
|
|
},
|
|
{
|
|
id: 'department',
|
|
header: 'Abteilung',
|
|
type: 'accessor',
|
|
accessorKey: 'department',
|
|
},
|
|
];
|
|
}
|
|
|
|
transformEuRow(project: EuProject): EuProjectRow {
|
|
return {
|
|
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),
|
|
grant_amount: Number(project.total_cost) || 0,
|
|
};
|
|
}
|
|
|
|
transformBundRow(project: BundProject): BundProjectRow {
|
|
return {
|
|
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),
|
|
grant_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: 1, size: 10 },
|
|
queryParamsHandling: 'merge',
|
|
});
|
|
}
|
|
|
|
handlePageChange(event: PageEvent): void {
|
|
this.router.navigate([], {
|
|
queryParams: { page: event.pageIndex, size: event.pageSize },
|
|
queryParamsHandling: 'merge',
|
|
});
|
|
}
|
|
|
|
handleSortChange(event: Sort): void {
|
|
this.router.navigate([], {
|
|
queryParams: { sort_by: event.active, sort_direction: event.direction },
|
|
queryParamsHandling: 'merge',
|
|
});
|
|
}
|
|
|
|
handleSearchTermChange(newTerm: string): void {
|
|
this.router.navigate([], {
|
|
queryParams: { search: newTerm, page: 0, size: 10 },
|
|
queryParamsHandling: 'merge',
|
|
});
|
|
}
|
|
|
|
clearSearchTerm(): void {
|
|
this.router.navigate([], {
|
|
queryParams: { search: null, page: 0, size: 10 },
|
|
queryParamsHandling: 'merge',
|
|
});
|
|
}
|
|
}
|