From 90a9570e7dfe79be0ae390f0504edb5db7402885 Mon Sep 17 00:00:00 2001 From: lotte Date: Tue, 20 Aug 2019 17:44:40 +0200 Subject: [PATCH] bug with deleting after adding --- src/app/core/cache/object-cache.service.ts | 3 +- src/app/core/data/relationship.service.ts | 19 ++++++++--- ...ynamic-form-control-container.component.ts | 6 ++-- ...dynamic-lookup-relation-modal.component.ts | 33 ++++++++++--------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/app/core/cache/object-cache.service.ts b/src/app/core/cache/object-cache.service.ts index c2ade1e2fb..c61ed981ec 100644 --- a/src/app/core/cache/object-cache.service.ts +++ b/src/app/core/cache/object-cache.service.ts @@ -195,8 +195,9 @@ export class ObjectCacheService { * false otherwise */ hasByUUID(uuid: string): boolean { - let result: boolean; + let result = false; + /* NB: that this is only a solution because the select method is synchronous, see: https://github.com/ngrx/store/issues/296#issuecomment-269032571*/ this.store.pipe( select(selfLinkFromUuidSelector(uuid)), take(1) diff --git a/src/app/core/data/relationship.service.ts b/src/app/core/data/relationship.service.ts index 04e602df4c..c82dbdc8b9 100644 --- a/src/app/core/data/relationship.service.ts +++ b/src/app/core/data/relationship.service.ts @@ -2,8 +2,8 @@ import { Injectable } from '@angular/core'; import { RequestService } from './request.service'; import { HALEndpointService } from '../shared/hal-endpoint.service'; import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; -import { hasValue, hasValueOperator, isNotEmptyOperator } from '../../shared/empty.util'; -import { distinctUntilChanged, filter, map, startWith, switchMap, take, tap } from 'rxjs/operators'; +import { hasNoValue, hasValue, hasValueOperator, isNotEmptyOperator } from '../../shared/empty.util'; +import { distinctUntilChanged, filter, map, mergeMap, skip, startWith, switchMap, take, tap } from 'rxjs/operators'; import { configureRequest, getRemoteDataPayload, getResponseFromEntry, getSucceededRemoteData } from '../shared/operators'; import { DeleteRequest, FindAllOptions, PostRequest, RestRequest } from './request.models'; import { Observable } from 'rxjs/internal/Observable'; @@ -91,7 +91,7 @@ export class RelationshipService extends DataService { switchMap((restRequest: RestRequest) => this.requestService.getByUUID(restRequest.uuid)), getResponseFromEntry(), tap(() => this.removeRelationshipItemsFromCache(item1)), - tap(() => this.removeRelationshipItemsFromCache(item2)), + tap(() => this.removeRelationshipItemsFromCache(item2)) ); } @@ -114,6 +114,11 @@ export class RelationshipService extends DataService { private removeRelationshipItemsFromCache(item) { this.objectCache.remove(item.self); this.requestService.removeByHrefSubstring(item.self); + this.objectCache.hasBySelfLinkObservable(item.self).pipe( + filter((exists) => !exists), + take(1), + switchMap(() => this.itemService.findByHref(item.self).pipe(take(1))) + ).subscribe(); } /** @@ -246,9 +251,14 @@ export class RelationshipService extends DataService { getRelationshipByItemsAndLabel(item1: Item, item2: Item, label: string): Observable { + console.log('item 1: ', this.objectCache.hasByUUID(item1.uuid)); + console.log('item 1 uuid: ', item1.uuid); + console.log('item 2: ', this.objectCache.hasByUUID(item2.uuid)); + console.log('item 2 uuid: ', item2.uuid); + return this.getItemRelationshipsByLabel(item1, label) .pipe( - switchMap((relationships: Relationship[]) => { + mergeMap((relationships: Relationship[]) => { return observableCombineLatest(...relationships.map((relationship: Relationship) => { console.log('relationship: ', relationship.uuid); return observableCombineLatest( @@ -261,6 +271,7 @@ export class RelationshipService extends DataService { ); })) }), + skip(1), map((relationships: Relationship[]) => relationships.find((relationship => hasValue(relationship)))), ) } diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control-container.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control-container.component.ts index f26e8acf5d..d040a98efb 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control-container.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control-container.component.ts @@ -184,7 +184,6 @@ export class DsDynamicFormControlContainerComponent extends DynamicFormControlCo modelValueMDRepresentation; listId: string; searchConfig: string; - uuid; /* tslint:disable:no-output-rename */ @Output('dfBlur') blur: EventEmitter = new EventEmitter(); @Output('dfChange') change: EventEmitter = new EventEmitter(); @@ -219,7 +218,6 @@ export class DsDynamicFormControlContainerComponent extends DynamicFormControlCo this.listId = 'list-' + this.model.relationship.relationshipType; this.model.workspaceItem.item.pipe( getSucceededRemoteData(), - tap((itemRD: RemoteData) => this.uuid = itemRD.payload.uuid), switchMap((itemRD: RemoteData) => this.relationService.getRelatedItemsByLabel(itemRD.payload, this.model.relationship.relationshipType)), map((items: Item[]) => items.map((item) => Object.assign(new SearchResult(), { indexableObject: item }))), ).subscribe((relatedItems) => this.selectableListService.select(this.listId, relatedItems)); @@ -280,12 +278,12 @@ export class DsDynamicFormControlContainerComponent extends DynamicFormControlCo modalComp.listId = this.listId; modalComp.relationship = this.model.relationship; modalComp.label = this.model.label; - modalComp.uuid = this.uuid; + modalComp.itemRD$ = this.model.workspaceItem.item; } removeSelection(object: SearchResult) { this.selectableListService.deselectSingle(this.listId, object); - setTimeout(() => this.itemService.findById(this.uuid).pipe( + setTimeout(() => this.model.workspaceItem.item.pipe( getSucceededRemoteData(), switchMap((itemRD: RemoteData) => this.relationshipService.getRelationshipByItemsAndLabel(itemRD.payload, object.indexableObject, this.model.relationship.relationshipType)), tap(t => console.log(t)), 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 c00ad4589e..99c024f965 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 @@ -55,7 +55,7 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy pageSize: 10 }); selection$: Observable; - uuid; + itemRD$; constructor( public modal: NgbActiveModal, @@ -140,7 +140,9 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy getSucceededRemoteData(), map((resultsRD) => resultsRD.payload.page), tap(() => this.selectAllLoading = false), - ).subscribe((results) => this.selectableListService.select(this.listId, results)); + ).subscribe((results) => + this.selectableListService.select(this.listId, results) + ); } deselectAll() { @@ -150,24 +152,24 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy select(selectableObject: SearchResult) { - this.itemService.findById(this.uuid) + this.itemRD$ .pipe( getSucceededRemoteData(), mergeMap((itemRD: RemoteData) => { const type1: string = itemRD.payload.firstMetadataValue('relationship.type'); const type2: string = selectableObject.indexableObject.firstMetadataValue('relationship.type'); - return this.relationshipTypeService.getRelationshipTypeByLabelAndTypes(this.relationship.relationshipType, type1, type2).pipe( - mergeMap((type: RelationshipType) => { - const isSwitched = type.rightLabel === this.relationship.relationshipType; - if (isSwitched) { - return this.relationshipService.addRelationship(type.id, selectableObject.indexableObject, itemRD.payload); - } else { - return this.relationshipService.addRelationship(type.id, itemRD.payload, selectableObject.indexableObject); + return this.relationshipTypeService.getRelationshipTypeByLabelAndTypes(this.relationship.relationshipType, type1, type2) + .pipe( + mergeMap((type: RelationshipType) => { + const isSwitched = type.rightLabel === this.relationship.relationshipType; + if (isSwitched) { + return this.relationshipService.addRelationship(type.id, selectableObject.indexableObject, itemRD.payload); + } else { + return this.relationshipService.addRelationship(type.id, itemRD.payload, selectableObject.indexableObject); + } } - } - ) - ); - + ) + ); }), take(1) ) @@ -176,10 +178,9 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy deselect(selectableObject: SearchResult) { - this.itemService.findById(this.uuid).pipe( + this.itemRD$.pipe( getSucceededRemoteData(), switchMap((itemRD: RemoteData) => this.relationshipService.getRelationshipByItemsAndLabel(itemRD.payload, selectableObject.indexableObject, this.relationship.relationshipType)), - switchMap((relationship: Relationship) => this.relationshipService.deleteRelationship(relationship.id)), take(1) ).subscribe(); }