238 lines
7.0 KiB
TypeScript
238 lines
7.0 KiB
TypeScript
import { CommonModule } from '@angular/common';
|
|
import {
|
|
Component,
|
|
inject,
|
|
input,
|
|
OnInit,
|
|
TemplateRef,
|
|
viewChild,
|
|
} from '@angular/core';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
|
|
import type {
|
|
OrganizationDialogComponent,
|
|
OrganizationDialogData,
|
|
OrganizationDialogResult,
|
|
} from './organization-dialog/organization-dialog.component';
|
|
import { OrganizationService } from '../../api';
|
|
import {
|
|
GenericColumn,
|
|
DataTableComponent,
|
|
} from '../../shared/ui/data-table/data-table.component';
|
|
import { MatCardModule } from '@angular/material/card';
|
|
import { StatusPipe } from '../../shared/pipes/status.pipe';
|
|
import { QueryParamPaginatorComponent } from '../../shared/ui/query-param-paginator/query-param-paginator.component';
|
|
import { Router } from '@angular/router';
|
|
import {
|
|
OrganizationPageWithInstitutions,
|
|
OrganizationWithInstitutions,
|
|
} from '../../resolvers/organizations-organizations.resolver';
|
|
import { CredentialService } from '../../shared/services/credential.service';
|
|
|
|
@Component({
|
|
selector: 'app-organizations',
|
|
imports: [
|
|
CommonModule,
|
|
MatButtonModule,
|
|
MatDialogModule,
|
|
MatIconModule,
|
|
MatSnackBarModule,
|
|
DataTableComponent,
|
|
MatCardModule,
|
|
StatusPipe,
|
|
QueryParamPaginatorComponent,
|
|
],
|
|
templateUrl: './organizations.component.html',
|
|
styleUrl: './organizations.component.scss',
|
|
standalone: true,
|
|
})
|
|
export class OrganizationsComponent implements OnInit {
|
|
private readonly router = inject(Router);
|
|
private readonly dialog = inject(MatDialog);
|
|
private readonly snackBar = inject(MatSnackBar);
|
|
private readonly organizationService = inject(OrganizationService);
|
|
private readonly credentialService = inject(CredentialService);
|
|
|
|
organizationPage = input.required<OrganizationPageWithInstitutions>();
|
|
organizationColumns: GenericColumn<OrganizationWithInstitutions>[] = [];
|
|
|
|
organizationStatusTmp = viewChild.required<TemplateRef<any>>(
|
|
'organizationStatusTmp',
|
|
);
|
|
organizationActionsTmp = viewChild.required<TemplateRef<any>>(
|
|
'organizationActionsTmp',
|
|
);
|
|
organizationInstitutionsTmp = viewChild.required<TemplateRef<any>>(
|
|
'organizationInstitutionsTmp',
|
|
);
|
|
|
|
ngOnInit(): void {
|
|
this.organizationColumns = [
|
|
{
|
|
id: 'name',
|
|
header: 'Name',
|
|
type: 'accessor',
|
|
accessorKey: 'name',
|
|
},
|
|
{
|
|
id: 'institutions',
|
|
header: 'OpenAlex-Institutionen',
|
|
type: 'display',
|
|
cellTemplate: this.organizationInstitutionsTmp(),
|
|
},
|
|
{
|
|
id: 'member_count',
|
|
header: 'Mitglieder',
|
|
type: 'accessor',
|
|
accessorKey: 'member_count',
|
|
},
|
|
{
|
|
id: 'deleted',
|
|
header: 'Status',
|
|
type: 'accessor',
|
|
accessorKey: 'deleted',
|
|
cellTemplate: this.organizationStatusTmp(),
|
|
},
|
|
{
|
|
id: 'actions',
|
|
header: 'Aktionen',
|
|
type: 'display',
|
|
cellTemplate: this.organizationActionsTmp(),
|
|
},
|
|
];
|
|
}
|
|
|
|
protected startCreate(): void {
|
|
this.openDialog({ mode: 'create' });
|
|
}
|
|
|
|
protected startEdit(organization: OrganizationWithInstitutions): void {
|
|
this.openDialog({
|
|
mode: 'edit',
|
|
organization,
|
|
isCurrentOrganization: this.isCurrentOrganization(organization),
|
|
});
|
|
}
|
|
|
|
protected isCurrentOrganization(
|
|
organization: OrganizationWithInstitutions,
|
|
): boolean {
|
|
return organization.id === this.credentialService.effectiveOrganizationId();
|
|
}
|
|
|
|
protected deleteOrganization(
|
|
organization: OrganizationWithInstitutions,
|
|
): void {
|
|
if (
|
|
organization.deleted ||
|
|
this.isCurrentOrganization(organization)
|
|
) {
|
|
return;
|
|
}
|
|
|
|
import('../../shared/ui/confirmation-dialog/confirmation-dialog.component').then(
|
|
({ ConfirmationDialogComponent }) => {
|
|
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
|
|
data: {
|
|
title: 'Organisation deaktivieren',
|
|
message: `Möchtest Du die Organisation „${organization.name}“ wirklich deaktivieren?`,
|
|
confirmText: 'Deaktivieren',
|
|
cancelText: 'Abbrechen',
|
|
},
|
|
});
|
|
|
|
dialogRef.componentInstance.confirmDelete.subscribe(() => {
|
|
this.softDeleteOrganization(organization);
|
|
});
|
|
},
|
|
);
|
|
}
|
|
|
|
private softDeleteOrganization(
|
|
organization: OrganizationWithInstitutions,
|
|
): void {
|
|
this.organizationService.deleteOrganisation(organization.id).subscribe({
|
|
next: () => {
|
|
this.snackBar.open('Organisation wurde deaktiviert.', 'OK', {
|
|
duration: 3000,
|
|
});
|
|
this.router.navigate([], {
|
|
queryParams: { page: 0 },
|
|
queryParamsHandling: 'merge',
|
|
});
|
|
},
|
|
error: (error) =>
|
|
this.showError(
|
|
this.isProtectedOrganizationError(error)
|
|
? 'Die eigene Organisation kann nicht deaktiviert werden.'
|
|
: 'Organisation konnte nicht deaktiviert werden.',
|
|
),
|
|
});
|
|
}
|
|
|
|
private openDialog(data: OrganizationDialogData): void {
|
|
import('./organization-dialog/organization-dialog.component').then(
|
|
({ OrganizationDialogComponent }) => {
|
|
const dialogRef = this.dialog.open<
|
|
OrganizationDialogComponent,
|
|
OrganizationDialogData,
|
|
OrganizationDialogResult | undefined
|
|
>(OrganizationDialogComponent, {
|
|
width: '780px',
|
|
maxWidth: 'calc(100vw - 32px)',
|
|
data,
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result) => {
|
|
if (result) this.save(result);
|
|
});
|
|
},
|
|
);
|
|
}
|
|
|
|
private save(result: OrganizationDialogResult): void {
|
|
const request =
|
|
result.mode === 'create'
|
|
? this.organizationService.createOrganisation(result.payload)
|
|
: this.organizationService.updateOrganisation(
|
|
result.organizationId,
|
|
result.payload,
|
|
);
|
|
|
|
request.subscribe({
|
|
next: () => {
|
|
this.snackBar.open('Organisation wurde gespeichert.', 'OK', {
|
|
duration: 3000,
|
|
});
|
|
this.router.navigate([], {
|
|
queryParams: { page: 0 },
|
|
queryParamsHandling: 'merge',
|
|
});
|
|
},
|
|
error: (error) =>
|
|
this.showError(
|
|
this.isProtectedOrganizationError(error)
|
|
? 'Die eigene Organisation kann nicht deaktiviert werden.'
|
|
: error.status === 409
|
|
? 'Eine Organisation mit diesem Namen existiert bereits.'
|
|
: 'Organisation konnte nicht gespeichert werden.',
|
|
),
|
|
});
|
|
}
|
|
|
|
private isProtectedOrganizationError(error: {
|
|
error?: { detail?: string };
|
|
}): boolean {
|
|
return (
|
|
error.error?.detail ===
|
|
'The organization of the real or effective account cannot be deleted.'
|
|
);
|
|
}
|
|
|
|
private showError(message: string): void {
|
|
this.snackBar.open(message, 'OK', { duration: 5000 });
|
|
}
|
|
}
|