diff --git a/src/app/item-page/edit-item-page/item-delete/item-delete.component.html b/src/app/item-page/edit-item-page/item-delete/item-delete.component.html index 5191e705d0..ed6ba3eabc 100644 --- a/src/app/item-page/edit-item-page/item-delete/item-delete.component.html +++ b/src/app/item-page/edit-item-page/item-delete/item-delete.component.html @@ -6,30 +6,29 @@

{{descriptionMessage | translate}}

- +
{{'virtual-metadata.delete-item.info' | translate}} -
- -
+
-
+
-
- {{getRelationshipMessageKey(getLabel(type) | async) | translate}} +
+ {{getRelationshipMessageKey(typeDto.label$ | async) | translate}}
-
- + -
+
diff --git a/src/app/item-page/edit-item-page/item-delete/item-delete.component.ts b/src/app/item-page/edit-item-page/item-delete/item-delete.component.ts index 9012ebe7d7..0a512c8976 100644 --- a/src/app/item-page/edit-item-page/item-delete/item-delete.component.ts +++ b/src/app/item-page/edit-item-page/item-delete/item-delete.component.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line max-classes-per-file import { Component, Input, OnInit, OnDestroy } from '@angular/core'; import { defaultIfEmpty, filter, map, switchMap, take } from 'rxjs/operators'; import { @@ -37,6 +38,34 @@ import { getItemEditRoute } from '../../item-page-routing-paths'; import { RemoteData } from '../../../core/data/remote-data'; import { NoContent } from '../../../core/shared/NoContent.model'; +/** + * Data Transfer Object used to prevent the HTML template to call function returning Observables + */ +class RelationshipTypeDTO { + + relationshipType: RelationshipType; + + isSelected$: Observable; + + label$: Observable; + + relationshipDTOs$: Observable; + +} + +/** + * Data Transfer Object used to prevent the HTML template to call function returning Observables + */ +class RelationshipDTO { + + relationship: Relationship; + + relatedItem$: Observable; + + virtualMetadata$: Observable; + +} + @Component({ selector: 'ds-item-delete', templateUrl: '../item-delete/item-delete.component.html' @@ -64,7 +93,7 @@ export class ItemDeleteComponent * A list of the relationship types for which this item has relations as an observable. * The list doesn't contain duplicates. */ - types$: BehaviorSubject = new BehaviorSubject([]); + typeDTOs$: BehaviorSubject = new BehaviorSubject([]); /** * A map which stores the relationships of this item for each type as observable lists @@ -93,6 +122,8 @@ export class ItemDeleteComponent */ private subs: Subscription[] = []; + public isDeleting$: BehaviorSubject = new BehaviorSubject(false); + constructor(protected route: ActivatedRoute, protected router: Router, protected notificationsService: NotificationsService, @@ -146,14 +177,25 @@ export class ItemDeleteComponent }, []) ), ); - }) - ).subscribe((types: RelationshipType[]) => this.types$.next(types))); + }), + ).subscribe((types: RelationshipType[]) => this.typeDTOs$.next(types.map((relationshipType: RelationshipType) => Object.assign(new RelationshipTypeDTO(), { + relationshipType: relationshipType, + isSelected$: this.isSelected(relationshipType), + label$: this.getLabel(relationshipType), + relationshipDTOs$: this.getRelationships(relationshipType).pipe( + map((relationships: Relationship[]) => relationships.map((relationship: Relationship) => Object.assign(new RelationshipDTO(), { + relationship: relationship, + relatedItem$: this.getRelatedItem(relationship), + virtualMetadata$: this.getVirtualMetadata(relationship), + } as RelationshipDTO))), + ), + }))))); } - this.subs.push(this.types$.pipe( + this.subs.push(this.typeDTOs$.pipe( take(1), - ).subscribe((types) => - this.objectUpdatesService.initialize(this.url, types, this.item.lastModified) + ).subscribe((types: RelationshipTypeDTO[]) => + this.objectUpdatesService.initialize(this.url, types.map((relationshipTypeDto: RelationshipTypeDTO) => relationshipTypeDto.relationshipType), this.item.lastModified), )); } @@ -326,34 +368,33 @@ export class ItemDeleteComponent * @param selected whether the type should be selected */ setSelected(type: RelationshipType, selected: boolean): void { - this.objectUpdatesService.setSelectedVirtualMetadata(this.url, this.item.uuid, type.uuid, selected); + if (this.isDeleting$.value === false) { + this.objectUpdatesService.setSelectedVirtualMetadata(this.url, this.item.uuid, type.uuid, selected); + } } /** * Perform the delete operation */ - performAction() { - - this.subs.push(this.types$.pipe( - switchMap((types) => + performAction(): void { + this.isDeleting$.next(true); + this.subs.push(this.typeDTOs$.pipe( + switchMap((types: RelationshipTypeDTO[]) => combineLatest( - types.map((type) => this.isSelected(type)) + types.map((type: RelationshipTypeDTO) => type.isSelected$), ).pipe( defaultIfEmpty([]), - map((selection) => types.filter( - (type, index) => selection[index] + map((selection: boolean[]) => types.filter( + (type: RelationshipTypeDTO, index: number) => selection[index], )), - map((selectedTypes) => selectedTypes.map((type) => type.id)), - ) + map((selectedDtoTypes: RelationshipTypeDTO[]) => selectedDtoTypes.map((typeDto: RelationshipTypeDTO) => typeDto.relationshipType.id)), + ), ), - switchMap((types) => - this.itemDataService.delete(this.item.id, types).pipe(getFirstCompletedRemoteData()) - ) - ).subscribe( - (rd: RemoteData) => { - this.notify(rd.hasSucceeded); - } - )); + switchMap((types: string[]) => this.itemDataService.delete(this.item.id, types)), + getFirstCompletedRemoteData(), + ).subscribe((rd: RemoteData) => { + this.notify(rd.hasSucceeded); + })); } /** @@ -363,10 +404,10 @@ export class ItemDeleteComponent notify(succeeded: boolean) { if (succeeded) { this.notificationsService.success(this.translateService.get('item.edit.' + this.messageKey + '.success')); - this.router.navigate(['']); + void this.router.navigate(['']); } else { this.notificationsService.error(this.translateService.get('item.edit.' + this.messageKey + '.error')); - this.router.navigate([getItemEditRoute(this.item)]); + void this.router.navigate([getItemEditRoute(this.item)]); } }