mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
62849: fixed delete/add relationships
This commit is contained in:
@@ -3,7 +3,7 @@ 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, 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 { DeleteRequest, GetRequest, PostRequest, RestRequest } from './request.models';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
@@ -44,10 +44,10 @@ export class RelationshipService {
|
||||
|
||||
/**
|
||||
* Send a delete request for a relationship by ID
|
||||
* @param uuid
|
||||
* @param id
|
||||
*/
|
||||
deleteRelationship(uuid: string): Observable<RestResponse> {
|
||||
return this.getRelationshipEndpoint(uuid).pipe(
|
||||
deleteRelationship(id: string): Observable<RestResponse> {
|
||||
return this.getRelationshipEndpoint(id).pipe(
|
||||
isNotEmptyOperator(),
|
||||
distinctUntilChanged(),
|
||||
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 item’s unique relationship types
|
||||
* The array doesn't contain any duplicate labels
|
||||
@@ -248,4 +231,34 @@ export class RelationshipService {
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -23,7 +23,7 @@ import { RelationshipType } from '../../../../../core/shared/item-relationships/
|
||||
import { RelationshipService } from '../../../../../core/data/relationship.service';
|
||||
import { Item } from '../../../../../core/shared/item.model';
|
||||
import { RelationshipOptions } from '../../models/relationship-options.model';
|
||||
import { combineLatest as observableCombineLatest } from 'rxjs';
|
||||
import { Relationship } from '../../../../../core/shared/item-relationships/relationship.model';
|
||||
|
||||
@Component({
|
||||
selector: 'ds-dynamic-lookup-relation-modal',
|
||||
@@ -146,18 +146,15 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy
|
||||
|
||||
|
||||
select(selectableObject: SearchResult<Item>) {
|
||||
const relationshipType$: Observable<RelationshipType> = this.itemRD$.pipe(
|
||||
this.subscription = this.itemRD$
|
||||
.pipe(
|
||||
getSucceededRemoteData(),
|
||||
take(1),
|
||||
switchMap((itemRD: RemoteData<Item>) => {
|
||||
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);
|
||||
}));
|
||||
|
||||
this.subscription = observableCombineLatest(relationshipType$, this.itemRD$)
|
||||
.pipe(
|
||||
take(1),
|
||||
switchMap(([type, itemRD]: [RelationshipType, RemoteData<Item>]) => {
|
||||
return this.relationshipTypeService.getRelationshipTypeByLabelAndTypes(this.relationship.relationshipType, type1, type2).pipe(
|
||||
switchMap((type: RelationshipType) => {
|
||||
const isSwitched = type.rightLabel === this.relationship.relationshipType;
|
||||
let result;
|
||||
if (isSwitched) {
|
||||
@@ -167,17 +164,19 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy
|
||||
}
|
||||
return result;
|
||||
})
|
||||
);
|
||||
|
||||
})
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
|
||||
deselect(selectableObject: SearchResult<Item>) {
|
||||
this.itemRD$.pipe(
|
||||
const subscription = this.itemRD$.pipe(
|
||||
getSucceededRemoteData(),
|
||||
switchMap((itemRD: RemoteData<Item>) => this.relationshipService.getItemRelationshipsByLabel(itemRD.payload, this.relationship.relationshipType)),
|
||||
|
||||
// map((items: Item[]) => items.find((item: Item) => ))
|
||||
switchMap((itemRD: RemoteData<Item>) => this.relationshipService.getRelationshipByItemsAndLabel(itemRD.payload, selectableObject.indexableObject, this.relationship.relationshipType)),
|
||||
switchMap((relationship: Relationship) => this.relationshipService.deleteRelationship(relationship.id)),
|
||||
).subscribe();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user