From 2e50d99fc45cbffb0819a2ec505f996925c082bf Mon Sep 17 00:00:00 2001 From: lotte Date: Fri, 25 Oct 2019 16:35:10 +0200 Subject: [PATCH] working on sending name variants to the server on patch --- .../items/normalized-relationship.model.ts | 24 ++++++--- src/app/core/data/relationship.service.ts | 5 +- .../item-relationships/relationship.model.ts | 10 ++++ ...dynamic-lookup-relation-modal.component.ts | 49 ++++++++++++++++--- 4 files changed, 73 insertions(+), 15 deletions(-) diff --git a/src/app/core/cache/models/items/normalized-relationship.model.ts b/src/app/core/cache/models/items/normalized-relationship.model.ts index 0930134b18..1c1dcf8d5b 100644 --- a/src/app/core/cache/models/items/normalized-relationship.model.ts +++ b/src/app/core/cache/models/items/normalized-relationship.model.ts @@ -1,4 +1,4 @@ -import { autoserialize, autoserializeAs, inheritSerialization } from 'cerialize'; +import { autoserialize, deserialize, deserializeAs, inheritSerialization } from 'cerialize'; import { Relationship } from '../../../shared/item-relationships/relationship.model'; import { mapsTo, relationship } from '../../builders/build-decorators'; import { NormalizedObject } from '../normalized-object.model'; @@ -16,20 +16,20 @@ export class NormalizedRelationship extends NormalizedObject { /** * The identifier of this Relationship */ - @autoserialize + @deserialize id: string; /** * The item to the left of this relationship */ - @autoserialize + @deserialize @relationship(Item, false) leftItem: string; /** * The item to the right of this relationship */ - @autoserialize + @deserialize @relationship(Item, false) rightItem: string; @@ -46,15 +46,27 @@ export class NormalizedRelationship extends NormalizedObject { rightPlace: number; /** - * The type of Relationship + * The name variant of the Item to the left side of this Relationship */ @autoserialize + leftwardValue: string; + + /** + * The name variant of the Item to the right side of this Relationship + */ + @autoserialize + rightwardValue: string; + + /** + * The type of Relationship + */ + @deserialize @relationship(RelationshipType, false) relationshipType: string; /** * The universally unique identifier of this Relationship */ - @autoserializeAs(new IDToUUIDSerializer(Relationship.type.value), 'id') + @deserializeAs(new IDToUUIDSerializer(Relationship.type.value), 'id') uuid: string; } diff --git a/src/app/core/data/relationship.service.ts b/src/app/core/data/relationship.service.ts index 07701011d3..0841a47642 100644 --- a/src/app/core/data/relationship.service.ts +++ b/src/app/core/data/relationship.service.ts @@ -292,7 +292,6 @@ export class RelationshipService extends DataService { } public getNameVariant(listID: string, itemID: string): Observable { - console.log(listID, itemID); return this.appStore.pipe( select(relationshipStateSelector(listID, itemID)), tap((t) => console.log(t)), map((state: RelationshipState) => hasValue(state) ? state.nameVariant : undefined) ); @@ -301,4 +300,8 @@ export class RelationshipService extends DataService { public removeNameVariant(listID: string, itemID: string) { this.appStore.dispatch(new RemoveNameVariantAction(listID, itemID)); } + + public getNameVariantsByListID(listID: string) { + return this.appStore.pipe(select(relationshipListStateSelector(listID))); + } } diff --git a/src/app/core/shared/item-relationships/relationship.model.ts b/src/app/core/shared/item-relationships/relationship.model.ts index 9ab9751489..2adcf42c04 100644 --- a/src/app/core/shared/item-relationships/relationship.model.ts +++ b/src/app/core/shared/item-relationships/relationship.model.ts @@ -46,6 +46,16 @@ export class Relationship implements CacheableObject { */ rightPlace: number; + /** + * The name variant of the Item to the left side of this Relationship + */ + leftwardValue: string; + + /** + * The name variant of the Item to the right side of this Relationship + */ + rightwardValue: string; + /** * The type of Relationship */ 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 a3c9f155a5..bf448fb603 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,8 +1,8 @@ import { Component, NgZone, OnInit } from '@angular/core'; -import { combineLatest, Observable } from 'rxjs'; +import { combineLatest, Observable, Subscription } from 'rxjs'; import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; import { hasValue, hasValueOperator } from '../../../../empty.util'; -import { filter, map, switchMap, take, tap } from 'rxjs/operators'; +import { map, switchMap, take, tap } from 'rxjs/operators'; import { SEARCH_CONFIG_SERVICE } from '../../../../../+my-dspace-page/my-dspace-page.component'; import { SearchConfigurationService } from '../../../../../core/shared/search/search-configuration.service'; import { SelectableListService } from '../../../../object-list/selectable-list/selectable-list.service'; @@ -21,6 +21,8 @@ import { AppState } from '../../../../../app.reducer'; import { Context } from '../../../../../core/shared/context.model'; import { Relationship } from '../../../../../core/shared/item-relationships/relationship.model'; import { MetadataValue } from '../../../../../core/shared/metadata.models'; +import { ItemSearchResult } from '../../../../object-collection/shared/item-search-result.model'; +import { RelationshipListState } from './relationship.reducer'; @Component({ selector: 'ds-dynamic-lookup-relation-modal', @@ -84,8 +86,10 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { combineLatest(this.itemRD$.pipe(getSucceededRemoteData()), obs) .subscribe(([itemRD, obs]: [RemoteData, any[]]) => { - return obs.forEach((object: any) => - this.store.dispatch(new AddRelationshipAction(itemRD.payload, object.item, this.relationshipOptions.relationshipType, object.nameVariant)) + return obs.forEach((object: any) => { + this.store.dispatch(new AddRelationshipAction(itemRD.payload, object.item, this.relationshipOptions.relationshipType, object.nameVariant)); + this.addSelectSubscription(object); + } ); }) }); @@ -96,15 +100,44 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { () => this.itemRD$.pipe( getSucceededRemoteData(), tap((itemRD: RemoteData) => { - return selectableObjects.forEach((object) => - this.store.dispatch(new RemoveRelationshipAction(itemRD.payload, object.indexableObject, this.relationshipOptions.relationshipType)) - ); + return selectableObjects.forEach((object) => { + this.store.dispatch(new RemoveRelationshipAction(itemRD.payload, object.indexableObject, this.relationshipOptions.relationshipType)); + this.addSelectSubscription(object); + }); } ) ).subscribe() ); } + subscriptions = new Map(); + + addSelectSubscription(itemSR: SearchResult) { + const item$ = this.itemRD$.pipe(getSucceededRemoteData(), getRemoteDataPayload()); + const nameVariant$ = this.relationshipService.getNameVariant(this.listId, itemSR.indexableObject.uuid).pipe(hasValueOperator()); + const subscription = combineLatest(item$, nameVariant$) + .pipe( + switchMap(([item, nameVariant]: [Item, string]) => { + return this.relationshipService.getRelationshipByItemsAndLabel(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.itemRD$.pipe( getSucceededRemoteData(), @@ -119,7 +152,7 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { relationship.rightItem.pipe(getSucceededRemoteData(), getRemoteDataPayload()) )) ) - ), + ) ); const relatedItems$: Observable = combineLatest(relatedItemPairs$, this.itemRD$).pipe(