From 836492d2af730a011b5a9f68cef06fe2b1a1a39b Mon Sep 17 00:00:00 2001 From: lotte Date: Wed, 6 Nov 2019 16:43:25 +0100 Subject: [PATCH] sending name variant update requests to the server --- src/app/core/data/data.service.ts | 10 ++-- src/app/core/data/relationship.service.ts | 38 +++++++++++-- src/app/core/shared/operators.ts | 1 + ...dynamic-lookup-relation-modal.component.ts | 57 +++++++++---------- 4 files changed, 67 insertions(+), 39 deletions(-) diff --git a/src/app/core/data/data.service.ts b/src/app/core/data/data.service.ts index ad0db51980..072946669b 100644 --- a/src/app/core/data/data.service.ts +++ b/src/app/core/data/data.service.ts @@ -184,17 +184,19 @@ export abstract class DataService { * @return {Observable>} * Return an observable that emits response from the server */ - protected searchBy(searchMethod: string, options: FindAllOptions = {}): Observable>> { + protected searchBy(searchMethod: string, options: FindAllOptions = {}, refresh: boolean = false): Observable>> { const hrefObs = this.getSearchByHref(searchMethod, options); hrefObs.pipe( - first((href: string) => hasValue(href))) + find((href: string) => hasValue(href))) .subscribe((href: string) => { + if (refresh) { + this.requestService.removeByHrefSubstring(href); + } const request = new FindAllRequest(this.requestService.generateRequestId(), href, options); this.requestService.configure(request, true); }); - return this.rdbService.buildList(hrefObs) as Observable>>; } @@ -219,7 +221,7 @@ export abstract class DataService { if (isNotEmpty(operations)) { this.objectCache.addPatch(object.self, operations); } - return this.findById(object.uuid); + return this.findByHref(object.self); } )); diff --git a/src/app/core/data/relationship.service.ts b/src/app/core/data/relationship.service.ts index bac7ddcdb2..d2a8f92775 100644 --- a/src/app/core/data/relationship.service.ts +++ b/src/app/core/data/relationship.service.ts @@ -26,9 +26,9 @@ import { HttpClient, HttpHeaders } from '@angular/common/http'; import { DefaultChangeAnalyzer } from './default-change-analyzer.service'; import { SearchParam } from '../cache/models/search-param.model'; import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service'; -import { RemoveNameVariantAction, SetNameVariantAction } from '../../shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/relationship.actions'; import { AppState, keySelector } from '../../app.reducer'; -import { NameVariantListState, RelationshipState } from '../../shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/name-variant.reducer'; +import { NameVariantListState } from '../../shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/name-variant.reducer'; +import { RemoveNameVariantAction, SetNameVariantAction } from '../../shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/name-variant.actions'; const relationshipListsStateSelector = (state: AppState) => state.relationshipLists; @@ -37,8 +37,8 @@ const relationshipListStateSelector = (listID: string): MemoizedSelector(listID, relationshipListsStateSelector); }; -const relationshipStateSelector = (listID: string, itemID: string): MemoizedSelector => { - return keySelector(itemID, relationshipListStateSelector(listID)); +const relationshipStateSelector = (listID: string, itemID: string): MemoizedSelector => { + return keySelector(itemID, relationshipListStateSelector(listID)); }; /** @@ -225,7 +225,7 @@ export class RelationshipService extends DataService { } else { findAllOptions.searchParams = searchParams; } - return this.searchBy('byLabel', findAllOptions); + return this.searchBy('byLabel', findAllOptions, true); } /** @@ -263,6 +263,7 @@ export class RelationshipService extends DataService { return this.getItemRelationshipsByLabel(item1, label) .pipe( getSucceededRemoteData(), + tap((t) => console.log(t)), map((relationshipListRD: RemoteData>) => relationshipListRD.payload.page), mergeMap((relationships: Relationship[]) => { return observableCombineLatest(...relationships.map((relationship: Relationship) => { @@ -293,7 +294,7 @@ export class RelationshipService extends DataService { public getNameVariant(listID: string, itemID: string): Observable { return this.appStore.pipe( - select(relationshipStateSelector(listID, itemID)), tap((t) => console.log(t)), map((state: RelationshipState) => hasValue(state) ? state.nameVariant : undefined) + select(relationshipStateSelector(listID, itemID)) ); } @@ -304,4 +305,29 @@ export class RelationshipService extends DataService { public getNameVariantsByListID(listID: string) { return this.appStore.pipe(select(relationshipListStateSelector(listID))); } + + public updateNameVariant(item1: Item, item2: Item, relationshipLabel: string, nameVariant: string): Observable> { + return this.getRelationshipByItemsAndLabel(item1, item2, relationshipLabel) + .pipe( + switchMap((relation: Relationship) => + relation.relationshipType.pipe( + getSucceededRemoteData(), + getRemoteDataPayload(), + map(type => { + return { relation, type } + }) + ) + ), + switchMap((relationshipAndType: {relation: Relationship, type: RelationshipType}) => { + const { relation, type } = relationshipAndType; + let updatedRelationship; + if (relationshipLabel === type.leftwardType) { + updatedRelationship = Object.assign(new Relationship(), relation, { rightWardValue: nameVariant }); + } else { + updatedRelationship = Object.assign(new Relationship(), relation, { leftWardValue: nameVariant }); + } + return this.update(updatedRelationship); + }) + ) + } } diff --git a/src/app/core/shared/operators.ts b/src/app/core/shared/operators.ts index 875b3435ed..098dda921b 100644 --- a/src/app/core/shared/operators.ts +++ b/src/app/core/shared/operators.ts @@ -47,6 +47,7 @@ export const getResponseFromEntry = () => export const getResourceLinksFromResponse = () => (source: Observable): Observable => source.pipe( + tap((t) => console.log(t)), filterSuccessfulResponses(), map((response: DSOSuccessResponse) => response.resourceSelfLinks), ); 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 b4a4d8dbc9..b427bc6021 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 @@ -47,7 +47,7 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { metadataFields: string; subMap: { [uuid: string]: Subscription - }; + } = {}; constructor( public modal: NgbActiveModal, @@ -78,7 +78,7 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { this.subMap[sri.indexableObject.uuid] = nameVariant$ .pipe(skip(1)) .subscribe((nameVariant: string) => - this.relationshipService.updateNameVariant(this.item, sri.indexableObject, this.relationshipOptions.relationshipType, nameVariant) + this.relationshipService.updateNameVariant(this.item, sri.indexableObject, this.relationshipOptions.relationshipType, nameVariant).subscribe() ); return nameVariant$ .pipe( @@ -97,7 +97,6 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { .subscribe((obs: any[]) => { return obs.forEach((object: any) => { this.store.dispatch(new AddRelationshipAction(this.item, object.item, this.relationshipOptions.relationshipType, object.nameVariant)); - this.addSelectSubscription(object); } ); }) @@ -107,39 +106,39 @@ export class DsDynamicLookupRelationModalComponent implements OnInit { deselect(...selectableObjects: SearchResult[]) { this.zone.runOutsideAngular( () => selectableObjects.forEach((object) => { + this.subMap[object.indexableObject.uuid].unsubscribe(); this.store.dispatch(new RemoveRelationshipAction(this.item, object.indexableObject, this.relationshipOptions.relationshipType)); - this.addSelectSubscription(object); }) ) ; } - subscriptions = new Map(); + // 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); + // } - 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(); - } - } + // 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);