argos/dmp-frontend/src/app/ui/description/listing/listing-item/description-listing-item.co...

264 lines
10 KiB
TypeScript

import { Location } from '@angular/common';
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { DmpAccessType } from '@app/core/common/enum/dmp-access-type';
import { IsActive } from '@app/core/common/enum/is-active.enum';
import { Description } from '@app/core/model/description/description';
import { AuthService } from '@app/core/services/auth/auth.service';
import { DescriptionService } from '@app/core/services/description/description.service';
import { DmpService } from '@app/core/services/dmp/dmp.service';
import { FileTransformerService } from '@app/core/services/file-transformer/file-transformer.service';
import { LockService } from '@app/core/services/lock/lock.service';
import { MatomoService } from '@app/core/services/matomo/matomo-service';
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
import { ReferenceTypeService } from '@app/core/services/reference-type/reference-type.service';
import { ReferenceService } from '@app/core/services/reference/reference.service';
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
import { FileUtils } from '@app/core/services/utilities/file-utils.service';
import { DmpInvitationDialogComponent } from '@app/ui/dmp/invitation/dialog/dmp-invitation-dialog.component';
import { BaseComponent } from '@common/base/base.component';
import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component';
import { Guid } from '@common/types/guid';
import { TranslateService } from '@ngx-translate/core';
import * as FileSaver from 'file-saver';
import { takeUntil } from 'rxjs/operators';
import { DescriptionStatus } from '../../../../core/common/enum/description-status';
import { DescriptionCopyDialogComponent } from '../../description-copy-dialog/description-copy-dialog.component';
import { AppPermission } from '@app/core/common/enum/permission.enum';
@Component({
selector: 'app-description-listing-item-component',
templateUrl: './description-listing-item.component.html',
styleUrls: ['./description-listing-item.component.scss']
})
export class DescriptionListingItemComponent extends BaseComponent implements OnInit {
@Input() description: Description;
@Input() showDivider: boolean = true;
@Input() isPublic: boolean = false;
@Output() onClick: EventEmitter<Description> = new EventEmitter();
isDraft: boolean;
isDeleted: boolean;
isUserOwner: boolean;
descriptionStatusEnum = DescriptionStatus;
dmpAccessTypeEnum = DmpAccessType;
canDelete: boolean = false;
canEdit: boolean = false;
canInviteDmpUsers: boolean = false;
constructor(
private router: Router,
public enumUtils: EnumUtils,
private descriptionService: DescriptionService,
public dialog: MatDialog,
private language: TranslateService,
private authService: AuthService,
private uiNotificationService: UiNotificationService,
private lockService: LockService,
private location: Location,
private matomoService: MatomoService,
private fileUtils: FileUtils,
public dmpService: DmpService,
public referenceService: ReferenceService,
public referenceTypeService: ReferenceTypeService,
public fileTransformerService: FileTransformerService,
private fb: UntypedFormBuilder,
) {
super();
}
ngOnInit() {
this.matomoService.trackPageView('Description Listing Item');
if (this.description.isActive === IsActive.Inactive) {
this.isDeleted = true;
} else if (this.description.status === DescriptionStatus.Draft) {
this.isDraft = true;
this.isDeleted = false;
this.setIsUserOwner();
} else {
this.isDraft = false;
this.isDeleted = false;
this.setIsUserOwner();
}
this.canDelete = this.authService.hasPermission(AppPermission.DeleteDescription) ||
this.description.authorizationFlags?.some(x => x === AppPermission.DeleteDescription);
this.canEdit = this.authService.hasPermission(AppPermission.EditDescription) ||
this.description.authorizationFlags?.some(x => x === AppPermission.EditDescription);
this.canInviteDmpUsers = this.authService.hasPermission(AppPermission.InviteDmpUsers) ||
this.description.authorizationFlags?.some(x => x === AppPermission.InviteDmpUsers);
}
setIsUserOwner() {
if (this.description) {
const principalId: string = this.authService.userId()?.toString();
//TODO: add user to description objects
//if (principalId) this.isUserOwner = !!this.description.users.find(x => (x.role === Role.Owner) && (principalId === x.id));
}
}
isUserDMPRelated() {
const principalId: Guid = this.authService.userId();
return this.description.dmp.dmpUsers?.some(x => (x.user.id === principalId));
}
public isAuthenticated(): boolean {
return this.authService.currentAccountIsAuthenticated();
}
getItemLink(): string[] {
// return this.isPublic ? [`/descriptions/publicEdit/${this.description.id}`] : [`/descriptions/edit/${this.description.id}`];
return this.isPublic ? ['/descriptions/overview/public/' + this.description.id] : ['/descriptions/overview/' + this.description.id];
}
getDmpLink(): string[] {
return this.isPublic ? [`/explore-plans/overview/public/${this.description.dmp.id}`] : [`/plans/edit/${this.description.dmp.id}`];
}
// downloadPDF(description: Description): void {
// this.descriptionService.downloadPDF(description.id.toString())
// .pipe(takeUntil(this._destroyed))
// .subscribe(response => {
// const blob = new Blob([response.body], { type: 'application/pdf' });
// const filename = this.fileUtils.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
// FileSaver.saveAs(blob, filename);
// this.matomoService.trackDownload('descriptions', "pdf", description.id.toString());
// });
// }
download(description: Description, format: string): void {
this.descriptionService.download(description.id.toString(), format)
.pipe(takeUntil(this._destroyed))
.subscribe(response => {
const blob = new Blob([response.body], { type: 'application/octet-stream' });
const filename = this.fileUtils.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
FileSaver.saveAs(blob, filename);
this.matomoService.trackDownload('descriptions', format, description.id.toString());
});
}
downloadXML(description: Description): void {
//TODO: add file transformer service
// this.descriptionService.downloadXML(description.id as string)
// .pipe(takeUntil(this._destroyed))
// .subscribe(response => {
// const blob = new Blob([response.body], { type: 'application/xml' });
// const filename = this.fileUtils.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
// FileSaver.saveAs(blob, filename);
// this.matomoService.trackDownload('descriptions', "xml", description.id);
// });
}
openShareDialog() {
// TODO: This is a shared component. Put it in a seperate module.
const dialogRef = this.dialog.open(DmpInvitationDialogComponent, {
// height: '250px',
// width: '700px',
autoFocus: false,
restoreFocus: false,
data: {
dmpId: this.description.dmp.id,
dmpName: this.description.dmp.label,
blueprint: this.description.dmp.blueprint
}
});
}
copyToDmp(description: Description) {
const formGroup = this.fb.group({
dmpId: this.fb.control(null, Validators.required),
sectionId: this.fb.control(null, Validators.required),
})
const dialogRef = this.dialog.open(DescriptionCopyDialogComponent, {
width: '500px',
restoreFocus: false,
data: {
formGroup: formGroup,
descriptionId: description.id,
descriptionTemplate: description.descriptionTemplate,
descriptionProfileExist: false,
confirmButton: this.language.instant('DESCRIPTION-LISTING.COPY-DIALOG.COPY'),
cancelButton: this.language.instant('DESCRIPTION-LISTING.COPY-DIALOG.CANCEL')
}
});
dialogRef.afterClosed().pipe(takeUntil(this._destroyed))
.subscribe(formGroup => {
if (formGroup) {
this.router.navigate(['descriptions/edit/copy/' + description.id + '/' + formGroup.get('dmpId').value + '/' + formGroup.get('sectionId').value]);
}
});
}
deleteClicked(id: Guid) {
this.lockService.checkLockStatus(id).pipe(takeUntil(this._destroyed))
.subscribe(lockStatus => {
if (!lockStatus.status) {
this.openDeleteDialog(id);
} else {
this.openLockedByUserDialog();
}
});
}
openDeleteDialog(id: Guid): void {
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
maxWidth: '300px',
restoreFocus: false,
data: {
message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.DELETE-ITEM'),
confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.DELETE'),
cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL'),
isDeleteConfirmation: true
}
});
dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => {
if (result) {
this.descriptionService.delete(id)
.pipe(takeUntil(this._destroyed))
.subscribe(
complete => this.onDeleteCallbackSuccess(),
error => this.onDeleteCallbackError(error)
);
}
});
}
openLockedByUserDialog() {
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
maxWidth: '400px',
restoreFocus: false,
data: {
message: this.language.instant('DESCRIPTION-LISTING.LOCKED')
}
});
}
reloadPage(): void {
const path = this.location.path();
this.router.navigateByUrl('/reload', { skipLocationChange: true }).then(() => {
this.router.navigate([path]);
});
}
onDeleteCallbackSuccess(): void {
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-DELETE'), SnackBarNotificationLevel.Success);
this.reloadPage();
}
onDeleteCallbackError(error) {
this.uiNotificationService.snackBarNotification(error.error.message ? error.error.message : this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-DELETE'), SnackBarNotificationLevel.Error);
}
}