import { Component, OnInit } from '@angular/core'; import { BaseComponent } from '@common/base/base.component'; import { DatasetOverviewModel } from '@app/core/model/dataset/dataset-overview'; import { BreadcrumbItem } from '@app/ui/misc/breadcrumb/definition/breadcrumb-item'; import { Observable, of as observableOf, interval } from 'rxjs'; import { ActivatedRoute, Router, Params } from '@angular/router'; import { DatasetService } from '@app/core/services/dataset/dataset.service'; import { TranslateService } from '@ngx-translate/core'; import { AuthService } from '@app/core/services/auth/auth.service'; import { MatDialog } from '@angular/material'; import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service'; import { ConfigurationService } from '@app/core/services/configuration/configuration.service'; import { Oauth2DialogService } from '@app/ui/misc/oauth2-dialog/service/oauth2-dialog.service'; import { UserService } from '@app/core/services/user/user.service'; import { takeUntil } from 'rxjs/operators'; import { Principal } from '@app/core/model/auth/principal'; import { Role } from '@app/core/common/enum/role'; import { Location } from '@angular/common'; import { UserInfoListingModel } from '@app/core/model/user/user-info-listing'; import { DatasetStatus } from '@app/core/common/enum/dataset-status'; import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component'; @Component({ selector: 'app-dataset-overview', templateUrl: './dataset-overview.component.html', styleUrls: ['./dataset-overview.component.scss'] }) export class DatasetOverviewComponent extends BaseComponent implements OnInit { dataset: DatasetOverviewModel; isNew = true; isFinalized = false; isPublicView = true; hasPublishButton: boolean = true; breadCrumbs: Observable = observableOf(); isUserOwner: boolean; expand = false; hasDOIToken = false; constructor( private route: ActivatedRoute, private router: Router, private datasetService: DatasetService, private translate: TranslateService, private authentication: AuthService, private dialog: MatDialog, private language: TranslateService, private uiNotificationService: UiNotificationService, private configurationService: ConfigurationService, private oauth2DialogService: Oauth2DialogService, private userService: UserService, private location: Location ) { super(); } ngOnInit() { // Gets dataset data using parameter id this.route.params .pipe(takeUntil(this._destroyed)) .subscribe((params: Params) => { const itemId = params['id']; const publicId = params['publicId']; if (itemId != null) { this.isNew = false; this.isPublicView = false; this.datasetService.getOverviewSingle(itemId) .pipe(takeUntil(this._destroyed)) .subscribe(data => { this.dataset = data; // this.checkLockStatus(this.dataset.id); this.setIsUserOwner(); const breadCrumbs = []; breadCrumbs.push({ parentComponentName: null, label: this.language.instant('NAV-BAR.MY-DATASET-DESCRIPTIONS'), url: "/datasets" }); breadCrumbs.push({ parentComponentName: 'DatasetListingComponent', label: this.dataset.label, url: '/datasets/overview/' + this.dataset.id }); this.breadCrumbs = observableOf(breadCrumbs); }, (error: any) => { if (error.status === 404) { return this.onFetchingDeletedCallbackError('/datasets/'); } if (error.status === 403) { return this.onFetchingForbiddenCallbackError('/datasets/'); } }); } else if (publicId != null) { this.isNew = false; this.isFinalized = true; this.isPublicView = true; this.datasetService.getOverviewSinglePublic(publicId) .pipe(takeUntil(this._destroyed)) .subscribe(data => { this.dataset = data; // this.checkLockStatus(this.dataset.id); this.setIsUserOwner(); const breadCrumbs = []; breadCrumbs.push({ parentComponentName: null, label: this.language.instant('NAV-BAR.PUBLIC DATASETS'), url: "/explore" }); breadCrumbs.push({ parentComponentName: 'DatasetListingComponent', label: this.dataset.label, url: '/datasets/publicOverview/' + this.dataset.id }); this.breadCrumbs = observableOf(breadCrumbs); }, (error: any) => { if (error.status === 404) { return this.onFetchingDeletedCallbackError('/explore'); } if (error.status === 403) { return this.onFetchingForbiddenCallbackError('/explore'); } }); } }); } onFetchingDeletedCallbackError(redirectRoot: string) { this.uiNotificationService.snackBarNotification(this.language.instant('DATASET-OVERVIEW.ERROR.DELETED-DATASET'), SnackBarNotificationLevel.Error); this.router.navigate([redirectRoot]); } onFetchingForbiddenCallbackError(redirectRoot: string) { this.uiNotificationService.snackBarNotification(this.language.instant('DATASET-OVERVIEW.ERROR.FORBIDEN-DATASET'), SnackBarNotificationLevel.Error); this.router.navigate([redirectRoot]); } goBack(): void { this.location.back(); } setIsUserOwner() { if (this.dataset) { const principal: Principal = this.authentication.current(); if (principal) this.isUserOwner = principal.id === this.dataset.users.find(x => x.role === Role.Owner).id; } } isUserDatasetRelated() { const principal: Principal = this.authentication.current(); let isRelated: boolean = false; if (this.dataset && principal) { this.dataset.users.forEach(element => { if (element.id === principal.id) { isRelated = true; } }) } return isRelated; } roleDisplayFromList(value: UserInfoListingModel[]) { const principal: Principal = this.authentication.current(); let role: number; if (principal) { value.forEach(element => { if (principal.id === element.id) { role = element.role; } }); } if (role === Role.Owner) { return this.translate.instant('DMP-LISTING.OWNER'); } else if (role === Role.Member) { return this.translate.instant('DMP-LISTING.MEMBER'); } else { return this.translate.instant('DMP-LISTING.OWNER'); } } public isAuthenticated(): boolean { return !(!this.authentication.current()); } cloneClicked(dataset: DatasetOverviewModel) { this.router.navigate(['/datasets/clone/' + dataset.id]); } isDraftDataset(dataset: DatasetOverviewModel) { // return dataset.status == DatasetStatus.Draft; } editClicked(dataset: DatasetOverviewModel) { this.router.navigate(['/datasets/edit/' + dataset.id]); } deleteClicked() { const dialogRef = this.dialog.open(ConfirmationDialogComponent, { maxWidth: '300px', 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.datasetService.delete(this.dataset.id) .pipe(takeUntil(this._destroyed)) .subscribe( complete => { this.onCallbackSuccess() }, error => this.onDeleteCallbackError(error) ); } }); } onCallbackSuccess(): void { this.uiNotificationService.snackBarNotification(this.isNew ? this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-CREATION') : this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success); this.router.navigate(['/datasets']); } onDeleteCallbackError(error) { this.uiNotificationService.snackBarNotification(error.error.message ? error.error.message : this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-DELETE'), SnackBarNotificationLevel.Error); } public getOrcidPath(): string { return this.configurationService.orcidPath; } }