mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +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 { 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 item’s unique relationship types
|
* Get an array of the labels of an item’s 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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -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,26 +146,26 @@ 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$
|
||||||
getSucceededRemoteData(),
|
|
||||||
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(
|
.pipe(
|
||||||
|
getSucceededRemoteData(),
|
||||||
take(1),
|
take(1),
|
||||||
switchMap(([type, itemRD]: [RelationshipType, RemoteData<Item>]) => {
|
switchMap((itemRD: RemoteData<Item>) => {
|
||||||
const isSwitched = type.rightLabel === this.relationship.relationshipType;
|
const type1: string = itemRD.payload.firstMetadataValue('relationship.type');
|
||||||
let result;
|
const type2: string = selectableObject.indexableObject.firstMetadataValue('relationship.type');
|
||||||
if (isSwitched) {
|
return this.relationshipTypeService.getRelationshipTypeByLabelAndTypes(this.relationship.relationshipType, type1, type2).pipe(
|
||||||
result = this.relationshipService.addRelationship(type.id, selectableObject.indexableObject, itemRD.payload);
|
switchMap((type: RelationshipType) => {
|
||||||
} else {
|
const isSwitched = type.rightLabel === this.relationship.relationshipType;
|
||||||
result = this.relationshipService.addRelationship(type.id, itemRD.payload, selectableObject.indexableObject);
|
let result;
|
||||||
}
|
if (isSwitched) {
|
||||||
return result;
|
result = this.relationshipService.addRelationship(type.id, selectableObject.indexableObject, itemRD.payload);
|
||||||
|
} else {
|
||||||
|
result = this.relationshipService.addRelationship(type.id, itemRD.payload, selectableObject.indexableObject);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.subscribe();
|
.subscribe();
|
||||||
@@ -173,11 +173,10 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy
|
|||||||
|
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user