Skip to content

Commit

Permalink
Merge branch 'w2p-113560_edit-item-add-relationships-one-by-one_contr…
Browse files Browse the repository at this point in the history
…ibute-7.6' into w2p-113560_edit-item-add-relationships-one-by-one_contribute-7_x
  • Loading branch information
alexandrevryghem committed May 28, 2024
2 parents 14dbced + 557dfba commit de381af
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/app/core/data/relationship-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,18 @@ export class RelationshipDataService extends IdentifiableDataService<Relationshi
);
});

return this.searchBy(
const searchRD$: Observable<RemoteData<PaginatedList<Relationship>>> = this.searchBy(
'byItemsAndType',
{
searchParams: searchParams
},
) as Observable<RemoteData<PaginatedList<Relationship>>>;

arrayOfItemIds.forEach((itemId: string) => {
this.addDependency(searchRD$, this.itemService.getIDHrefObs(encodeURIComponent(itemId)));
});

return searchRD$;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ describe('EditItemRelationshipsService', () => {

expect(itemService.invalidateByHref).toHaveBeenCalledWith(currentItem.self);
expect(itemService.invalidateByHref).toHaveBeenCalledWith(relationshipItem1.self);
expect(itemService.invalidateByHref).toHaveBeenCalledWith(relationshipItem2.self);

expect(notificationsService.success).toHaveBeenCalledTimes(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ export class EditItemRelationshipsService {
// process each update one by one, while waiting for the previous to finish
concatMap((update: FieldUpdate) => {
if (update.changeType === FieldChangeType.REMOVE) {
return this.deleteRelationship(update.field as DeleteRelationship).pipe(take(1));
return this.deleteRelationship(update.field as DeleteRelationship).pipe(
take(1),
switchMap((deleteRD: RemoteData<NoContent>) => {
if (deleteRD.hasSucceeded) {
this.itemService.invalidateByHref((update.field as DeleteRelationship).relatedItem._links.self.href);
}
return [deleteRD];
}),
);
} else if (update.changeType === FieldChangeType.ADD) {
return this.addRelationship(update.field as RelationshipIdentifiable).pipe(
take(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from '../../../../core/shared/item-relationships/relationship-type.model';
import {
getAllSucceededRemoteData,
getFirstCompletedRemoteData,
getFirstSucceededRemoteData,
getFirstSucceededRemoteDataPayload,
getRemoteDataPayload,
Expand Down Expand Up @@ -305,16 +306,16 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy {
modalComp.submitEv = () => {
modalComp.isPending = true;
const isLeft = this.currentItemIsLeftItem$.getValue();
const addOperations = modalComp.toAdd.map((searchResult: any) => ({ type: 'add', searchResult }));
const removeOperations = modalComp.toRemove.map((searchResult: any) => ({ type: 'remove', searchResult }));
const addOperations = modalComp.toAdd.map((searchResult: ItemSearchResult) => ({ type: 'add', searchResult }));
const removeOperations = modalComp.toRemove.map((searchResult: ItemSearchResult) => ({ type: 'remove', searchResult }));
observableFrom([...addOperations, ...removeOperations]).pipe(
concatMap(({ type, searchResult }: { type: string, searchResult: any }) => {
concatMap(({ type, searchResult }: { type: string, searchResult: ItemSearchResult }) => {
const relatedItem: Item = searchResult.indexableObject;
if (type === 'add') {
const relatedItem = searchResult.indexableObject;
return this.relationshipService.getNameVariant(this.listId, relatedItem.uuid).pipe(
map((nameVariant) => {
const update = {
uuid: this.relationshipType.id + '-' + searchResult.indexableObject.uuid,
uuid: `${this.relationshipType.id}-${relatedItem.uuid}`,
nameVariant,
type: this.relationshipType,
originalIsLeft: isLeft,
Expand All @@ -327,7 +328,7 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy {
take(1)
);
} else if (type === 'remove') {
return this.relationshipService.getNameVariant(this.listId, searchResult.indexableObjectuuid).pipe(
return this.relationshipService.getNameVariant(this.listId, relatedItem.uuid).pipe(
switchMap((nameVariant) => {
return this.getRelationFromId(searchResult.indexableObject).pipe(
map( (relationship: Relationship) => {
Expand All @@ -337,6 +338,7 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy {
type: this.relationshipType,
originalIsLeft: isLeft,
originalItem: this.item,
relatedItem,
relationship,
} as RelationshipIdentifiable;
this.objectUpdatesService.saveRemoveFieldUpdate(this.url,update);
Expand Down Expand Up @@ -508,7 +510,22 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy {
map((isLeftItem: boolean) => [relationship, isLeftItem])
)
),
map(([relationship, isLeftItem]: [Relationship, boolean]) => {
switchMap(([relationship, isLeftItem]: [Relationship, boolean]) => {
if (isLeftItem) {
return relationship.rightItem.pipe(
getFirstCompletedRemoteData(),
getRemoteDataPayload(),
map((relatedItem: Item) => [relationship, isLeftItem, relatedItem]),
);
} else {
return relationship.leftItem.pipe(
getFirstCompletedRemoteData(),
getRemoteDataPayload(),
map((relatedItem: Item) => [relationship, isLeftItem, relatedItem]),
);
}
}),
map(([relationship, isLeftItem, relatedItem]: [Relationship, boolean, Item]) => {
// turn it into a RelationshipIdentifiable, an
const nameVariant =
isLeftItem ? relationship.rightwardValue : relationship.leftwardValue;
Expand All @@ -518,6 +535,7 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy {
relationship,
originalIsLeft: isLeftItem,
originalItem: this.item,
relatedItem: relatedItem,
nameVariant,
} as RelationshipIdentifiable;
}),
Expand Down

0 comments on commit de381af

Please sign in to comment.