From 190203cb482e445461799ad11095dc863c99a27b Mon Sep 17 00:00:00 2001 From: lotte Date: Tue, 12 Nov 2019 16:34:26 +0100 Subject: [PATCH] add/remove subscriptions for name variants on init/destroy --- src/app/+item-page/item-page.module.ts | 2 + ...dynamic-lookup-relation-modal.component.ts | 51 ++++++++----------- ...ic-lookup-relation-search-tab.component.ts | 4 +- src/app/shared/shared.module.ts | 1 - 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/app/+item-page/item-page.module.ts b/src/app/+item-page/item-page.module.ts index 36652f5223..2a5d0b6da7 100644 --- a/src/app/+item-page/item-page.module.ts +++ b/src/app/+item-page/item-page.module.ts @@ -52,6 +52,7 @@ import { MetadataFieldWrapperComponent } from './field-components/metadata-field RelatedItemsComponent, ItemComponent, GenericItemPageFieldComponent, + MetadataRepresentationListComponent, RelatedEntitiesSearchComponent ], exports: [ @@ -61,6 +62,7 @@ import { MetadataFieldWrapperComponent } from './field-components/metadata-field GenericItemPageFieldComponent, RelatedEntitiesSearchComponent, RelatedItemsComponent, + MetadataRepresentationListComponent, ItemPageTitleFieldComponent ], entryComponents: [ diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts index 09c88403f8..25ff9d8d50 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts @@ -1,4 +1,4 @@ -import { Component, NgZone, OnInit } from '@angular/core'; +import { Component, NgZone, OnDestroy, OnInit } from '@angular/core'; import { combineLatest, Observable, Subscription } from 'rxjs'; import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; import { hasValue } from '../../../../empty.util'; @@ -33,7 +33,7 @@ import { MetadataValue } from '../../../../../core/shared/metadata.models'; ] }) -export class DsDynamicLookupRelationModalComponent implements OnInit { +export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy { label: string; relationshipOptions: RelationshipOptions; listId: string; @@ -57,7 +57,12 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { } ngOnInit(): void { - this.selection$ = this.selectableListService.getSelectableList(this.listId).pipe(map((listState: SelectableListState) => hasValue(listState) && hasValue(listState.selection) ? listState.selection : [])); + this.selection$ = this.selectableListService + .getSelectableList(this.listId) + .pipe(map((listState: SelectableListState) => hasValue(listState) && hasValue(listState.selection) ? listState.selection : [])); + this.selection$.subscribe((selection) => + selection.map((s: SearchResult) => this.addNameVariantSubscription(s)) + ); if (this.relationshipOptions.nameVariants) { this.context = Context.Workspace; } @@ -89,7 +94,6 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { ) }) ); - obs .subscribe((obs: any[]) => { return obs.forEach((object: any) => { @@ -100,6 +104,13 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { }); } + addNameVariantSubscription(sri: SearchResult) { + const nameVariant$ = this.relationshipService.getNameVariant(this.listId, sri.indexableObject.uuid); + this.subMap[sri.indexableObject.uuid] = nameVariant$.pipe(switchMap((nameVariant: string) => + this.relationshipService.updateNameVariant(this.item, sri.indexableObject, this.relationshipOptions.relationshipType, nameVariant) + )).subscribe() + } + deselect(...selectableObjects: SearchResult[]) { this.zone.runOutsideAngular( () => selectableObjects.forEach((object) => { @@ -110,33 +121,6 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { ; } - // subscriptions = new Map(); - // - // addSelectSubscription(itemSR: SearchResult) { - // const nameVariant$ = this.relationshipService.getNameVariant(this.listId, itemSR.indexableObject.uuid).pipe(hasValueOperator()); - // const subscription = nameVariant$ - // .pipe( - // switchMap((nameVariant: string) => { - // return this.relationshipService.getRelationshipByItemsAndLabel(this.item, itemSR.indexableObject, this.relationshipOptions.relationshipType) - // .pipe(map((relationship: Relationship) => Object.assign(new Relationship(), relationship, { leftwardValue: nameVariant }))) - // }), - // switchMap((updatedRelation: Relationship) => this.relationshipService.update(updatedRelation)) - // ) - // .subscribe(); - // this.subscriptions.set(itemSR.indexableObject.uuid, subscription); - // } - - // removeSelectSubscription(itemSR: SearchResult) { - // this.subscriptions.get(itemSR.indexableObject.uuid).unsubscribe(); - // } - // - // ngOnDestroy() { - // let sub; - // while (sub = this.subscriptions.values().next(), !sub.done) { - // sub.unsubscribe(); - // } - // } - setExistingNameVariants() { const virtualMDs$: Observable = this.item.allMetadata(this.metadataFields).filter((mdValue) => mdValue.isVirtual); @@ -166,4 +150,9 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { } ) } + + + ngOnDestroy() { + Object.values(this.subMap).forEach((subscription) => subscription.unsubscribe()); + } } \ No newline at end of file diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/search-tab/dynamic-lookup-relation-search-tab.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/search-tab/dynamic-lookup-relation-search-tab.component.ts index d473e193f5..270e2d88fa 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/search-tab/dynamic-lookup-relation-search-tab.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/search-tab/dynamic-lookup-relation-search-tab.component.ts @@ -20,7 +20,6 @@ import { getSucceededRemoteData } from '../../../../../../core/shared/operators' import { RouteService } from '../../../../../../core/services/route.service'; import { CollectionElementLinkType } from '../../../../../object-collection/collection-element-link.type'; import { Context } from '../../../../../../core/shared/context.model'; -import { relationship } from '../../../../../../core/cache/builders/build-decorators'; @Component({ selector: 'ds-dynamic-lookup-relation-search-tab', @@ -152,8 +151,7 @@ export class DsDynamicLookupRelationSearchTabComponent implements OnInit, OnDest } ngOnDestroy(): void { - if (hasValue(this.subscription) - ) { + if (hasValue(this.subscription)) { this.subscription.unsubscribe(); } } diff --git a/src/app/shared/shared.module.ts b/src/app/shared/shared.module.ts index 0a163d1ae8..3488b4eaab 100644 --- a/src/app/shared/shared.module.ts +++ b/src/app/shared/shared.module.ts @@ -321,7 +321,6 @@ const COMPONENTS = [ ItemSelectComponent, CollectionSelectComponent, MetadataRepresentationLoaderComponent, - MetadataRepresentationListComponent, ]; const ENTRY_COMPONENTS = [