62849: fixed delete/add relationships

This commit is contained in:
lotte
2019-08-09 16:57:06 +02:00
parent 827ffc2d79
commit 7406ab5d47
2 changed files with 56 additions and 44 deletions

View File

@@ -3,7 +3,7 @@ import { RequestService } from './request.service';
import { HALEndpointService } from '../shared/hal-endpoint.service'; import { HALEndpointService } from '../shared/hal-endpoint.service';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { hasValue, hasValueOperator, isNotEmptyOperator } from '../../shared/empty.util'; import { hasValue, hasValueOperator, isNotEmptyOperator } from '../../shared/empty.util';
import { distinctUntilChanged, filter, flatMap, map, startWith, switchAll, switchMap, tap } from 'rxjs/operators'; import { distinctUntilChanged, filter, find, flatMap, map, startWith, switchAll, switchMap, tap } from 'rxjs/operators';
import { configureRequest, getRemoteDataPayload, getResponseFromEntry, getSucceededRemoteData } from '../shared/operators'; import { configureRequest, getRemoteDataPayload, getResponseFromEntry, getSucceededRemoteData } from '../shared/operators';
import { DeleteRequest, GetRequest, PostRequest, RestRequest } from './request.models'; import { DeleteRequest, GetRequest, PostRequest, RestRequest } from './request.models';
import { Observable } from 'rxjs/internal/Observable'; import { Observable } from 'rxjs/internal/Observable';
@@ -44,10 +44,10 @@ export class RelationshipService {
/** /**
* Send a delete request for a relationship by ID * Send a delete request for a relationship by ID
* @param uuid * @param id
*/ */
deleteRelationship(uuid: string): Observable<RestResponse> { deleteRelationship(id: string): Observable<RestResponse> {
return this.getRelationshipEndpoint(uuid).pipe( return this.getRelationshipEndpoint(id).pipe(
isNotEmptyOperator(), isNotEmptyOperator(),
distinctUntilChanged(), distinctUntilChanged(),
map((endpointURL: string) => new DeleteRequest(this.requestService.generateRequestId(), endpointURL)), map((endpointURL: string) => new DeleteRequest(this.requestService.generateRequestId(), endpointURL)),
@@ -118,23 +118,6 @@ export class RelationshipService {
// ); // );
// } // }
/**
* Get an item its relationship types in the form of an array
* @param item
*/
getItemRelationshipTypesArray(item: Item): Observable<RelationshipType[]> {
return this.getItemRelationshipsArray(item).pipe(
flatMap((rels: Relationship[]) =>
observableZip(...rels.map((rel: Relationship) => rel.relationshipType)).pipe(
map(([...arr]: Array<RemoteData<RelationshipType>>) => arr.map((d: RemoteData<RelationshipType>) => d.payload).filter((type) => hasValue(type))),
filter((arr) => arr.length === rels.length)
)
),
distinctUntilChanged(compareArraysUsingIds())
);
}
/** /**
* Get an array of the labels of an items unique relationship types * Get an array of the labels of an items unique relationship types
* The array doesn't contain any duplicate labels * The array doesn't contain any duplicate labels
@@ -248,4 +231,34 @@ export class RelationshipService {
map((item: Item) => uuids.includes(item.uuid)) map((item: Item) => uuids.includes(item.uuid))
); );
} }
getRelationshipByItemsAndLabel(item1: Item, item2: Item, label: string): Observable<Relationship> {
return this.getItemRelationshipsByLabel(item1, label)
.pipe(
switchMap((relationships: Relationship[]) => {
return observableCombineLatest(...relationships.map((relationship: Relationship) => {
return observableCombineLatest(
this.isItemMatchWithItemRD(relationship.leftItem, item2),
this.isItemMatchWithItemRD(relationship.rightItem, item2)
).pipe(
filter(([isLeftItem, isRightItem]) => isLeftItem || isRightItem),
map(() => relationship),
startWith(undefined)
);
}))
}),
map((relationships: Relationship[]) => relationships.find((relationship => hasValue(relationship)))),
)
}
private isItemMatchWithItemRD(itemRD$: Observable<RemoteData<Item>>, itemCheck: Item): Observable<boolean> {
return itemRD$.pipe(
getSucceededRemoteData(),
map((itemRD: RemoteData<Item>) => itemRD.payload),
map((item: Item) => item.uuid === itemCheck.uuid)
);
}
} }

View File

@@ -23,7 +23,7 @@ import { RelationshipType } from '../../../../../core/shared/item-relationships/
import { RelationshipService } from '../../../../../core/data/relationship.service'; import { RelationshipService } from '../../../../../core/data/relationship.service';
import { Item } from '../../../../../core/shared/item.model'; import { Item } from '../../../../../core/shared/item.model';
import { RelationshipOptions } from '../../models/relationship-options.model'; import { RelationshipOptions } from '../../models/relationship-options.model';
import { combineLatest as observableCombineLatest } from 'rxjs'; import { Relationship } from '../../../../../core/shared/item-relationships/relationship.model';
@Component({ @Component({
selector: 'ds-dynamic-lookup-relation-modal', selector: 'ds-dynamic-lookup-relation-modal',
@@ -146,18 +146,15 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy
select(selectableObject: SearchResult<Item>) { select(selectableObject: SearchResult<Item>) {
const relationshipType$: Observable<RelationshipType> = this.itemRD$.pipe( this.subscription = this.itemRD$
.pipe(
getSucceededRemoteData(), getSucceededRemoteData(),
take(1),
switchMap((itemRD: RemoteData<Item>) => { switchMap((itemRD: RemoteData<Item>) => {
const type1: string = itemRD.payload.firstMetadataValue('relationship.type'); const type1: string = itemRD.payload.firstMetadataValue('relationship.type');
const type2: string = selectableObject.indexableObject.firstMetadataValue('relationship.type'); const type2: string = selectableObject.indexableObject.firstMetadataValue('relationship.type');
return this.relationshipTypeService.getRelationshipTypeByLabelAndTypes(this.relationship.relationshipType, type1, type2); return this.relationshipTypeService.getRelationshipTypeByLabelAndTypes(this.relationship.relationshipType, type1, type2).pipe(
})); switchMap((type: RelationshipType) => {
this.subscription = observableCombineLatest(relationshipType$, this.itemRD$)
.pipe(
take(1),
switchMap(([type, itemRD]: [RelationshipType, RemoteData<Item>]) => {
const isSwitched = type.rightLabel === this.relationship.relationshipType; const isSwitched = type.rightLabel === this.relationship.relationshipType;
let result; let result;
if (isSwitched) { if (isSwitched) {
@@ -167,17 +164,19 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy
} }
return result; return result;
}) })
);
})
) )
.subscribe(); .subscribe();
} }
deselect(selectableObject: SearchResult<Item>) { deselect(selectableObject: SearchResult<Item>) {
this.itemRD$.pipe( const subscription = this.itemRD$.pipe(
getSucceededRemoteData(), getSucceededRemoteData(),
switchMap((itemRD: RemoteData<Item>) => this.relationshipService.getItemRelationshipsByLabel(itemRD.payload, this.relationship.relationshipType)), switchMap((itemRD: RemoteData<Item>) => this.relationshipService.getRelationshipByItemsAndLabel(itemRD.payload, selectableObject.indexableObject, this.relationship.relationshipType)),
switchMap((relationship: Relationship) => this.relationshipService.deleteRelationship(relationship.id)),
// map((items: Item[]) => items.find((item: Item) => ))
).subscribe(); ).subscribe();
} }