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

269 lines
9.4 KiB
TypeScript
Raw Normal View History

2023-11-29 14:26:40 +01:00
import { Location } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { UntypedFormControl } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { Role } from '@app/core/common/enum/role';
import { AuthService } from '@app/core/services/auth/auth.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 { 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/dmp-invitation-dialog.component';
import { BaseComponent } from '@common/base/base.component';
import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component';
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 { Description } from '@app/core/model/description/description';
import { DescriptionService } from '@app/core/services/description/description.service';
import { IsActive } from '@app/core/common/enum/is-active.enum';
import { Guid } from '@common/types/guid';
@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;
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 httpClient: HttpClient,
private matomoService: MatomoService,
private fileUtils: FileUtils
) {
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();
}
}
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));
}
}
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/publicOverview/' + this.description.id] : ['/descriptions/overview/' + this.description.id];
}
getDmpLink(): string[] {
return this.isPublic ? [`/explore-plans/publicOverview/${this.description.dmp.id}`] : [`/plans/edit/${this.description.dmp.id}`];
}
downloadPDF(description: Description): void {
//TODO: add file transformer service
// this.descriptionService.downloadPDF(description.id as string)
// .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);
// });
}
downloadDOCX(description: Description): void {
//TODO: add file transformer service
// this.descriptionService.downloadDOCX(description.id as string)
// .pipe(takeUntil(this._destroyed))
// .subscribe(response => {
// const blob = new Blob([response.body], { type: 'application/msword' });
// const filename = this.fileUtils.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
// FileSaver.saveAs(blob, filename);
// this.matomoService.trackDownload('descriptions', "docx", description.id);
// });
}
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(dmpRowId: any, dmpRowName: any) {
// 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: dmpRowId,
dmpName: dmpRowName
}
});
}
openDmpSearchDialogue(description: Description) {
// TODO: add this.
// const formControl = new UntypedFormControl();
// const dialogRef = this.dialog.open(DescriptionCopyDialogueComponent, {
// width: '500px',
// restoreFocus: false,
// data: {
// formControl: formControl,
// descriptionId: description.id,
// descriptionProfileId: description.profile.id,
// descriptionProfileExist: false,
// confirmButton: this.language.instant('DATASET-WIZARD.DIALOGUE.COPY'),
// cancelButton: this.language.instant('DATASET-WIZARD.DIALOGUE.CANCEL')
// }
// });
// dialogRef.afterClosed().pipe(takeUntil(this._destroyed))
// .subscribe(result => {
// if (result && result.descriptionProfileExist) {
// const newDmpId = result.formControl.value.id;
// this.router.navigate(['/descriptions/copy/' + result.descriptionId], { queryParams: { newDmpId: newDmpId } });
// }
// });
}
deleteClicked(id: Guid) {
this.lockService.checkLockStatus(id.toString()).pipe(takeUntil(this._destroyed))
.subscribe(lockStatus => {
if (!lockStatus) {
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('DATASET-WIZARD.ACTIONS.LOCK')
}
});
}
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);
}
roleDisplay(value: any) {
const principalId: string = this.authService.userId()?.toString();
let role: number;
if (principalId) {
value.forEach(element => {
if (principalId === element.id) {
role = element.role;
}
});
}
if (role === 0) {
return this.language.instant('DMP-LISTING.OWNER');
}
else if (role === 1) {
return this.language.instant('DMP-LISTING.MEMBER');
}
else {
return this.language.instant('DMP-LISTING.OWNER');
}
}
isUserDescriptionRelated() {
//TODO: add user to description objects
const principalId: string = this.authService.userId()?.toString();
let isRelated: boolean = false;
// if (this.description && principalId) {
// this.description.users.forEach(element => {
// if (element.id === principalId) {
// isRelated = true;
// }
// })
// }
return isRelated;
}
}