diff --git a/src/app/core/data/object-updates/object-updates.service.ts b/src/app/core/data/object-updates/object-updates.service.ts index 88c7c0e453..8574e38771 100644 --- a/src/app/core/data/object-updates/object-updates.service.ts +++ b/src/app/core/data/object-updates/object-updates.service.ts @@ -141,6 +141,7 @@ export class ObjectUpdatesService { map((objectEntry) => { const fieldUpdates: FieldUpdates = {}; for (const object of initialFields) { + console.log(object.uuid,objectEntry.fieldUpdates); let fieldUpdate = objectEntry.fieldUpdates[object.uuid]; if (isEmpty(fieldUpdate)) { fieldUpdate = { field: object, changeType: undefined }; @@ -151,6 +152,25 @@ export class ObjectUpdatesService { })); } + /** + * Method that combines the state's updates (excluding updates that aren't part of the initialFields) with + * the initial values (when there's no update) to create a FieldUpdates object + * @param url The URL of the page for which the FieldUpdates should be requested + * @param initialFields The initial values of the fields + */ + getFieldUpdatesAll(url: string, initialFields: Identifiable[]): Observable { + const objectUpdates = this.getObjectEntry(url); + return objectUpdates.pipe( + hasValueOperator(), + map((objectEntry) => { + console.log(objectEntry,initialFields); + const fieldUpdates: FieldUpdates = {}; + for (const key of Object.keys(objectEntry.fieldUpdates)) { + fieldUpdates[key] = objectEntry.fieldUpdates[key]; + } + return fieldUpdates; + })); + } /** * Method to check if a specific field is currently editable in the store * @param url The URL of the page on which the field resides diff --git a/src/app/item-page/edit-item-page/item-relationships/edit-relationship-list/edit-relationship-list.component.ts b/src/app/item-page/edit-item-page/item-relationships/edit-relationship-list/edit-relationship-list.component.ts index fff3d9fd71..2870c56024 100644 --- a/src/app/item-page/edit-item-page/item-relationships/edit-relationship-list/edit-relationship-list.component.ts +++ b/src/app/item-page/edit-item-page/item-relationships/edit-relationship-list/edit-relationship-list.component.ts @@ -249,7 +249,8 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy { modalComp.submit = this.submit; modalComp.reinstate = this.reinstate; modalComp.discard = this.discard; - + modalComp.toAdd = []; + modalComp.toRemove = []; this.item.owningCollection.pipe( getFirstSucceededRemoteDataPayload() @@ -259,44 +260,115 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy { modalComp.select = (...selectableObjects: SearchResult[]) => { selectableObjects.forEach((searchResult) => { const relatedItem: Item = searchResult.indexableObject; + + + let foundIndex = modalComp.toRemove.findIndex( el => el.uuid == relatedItem.uuid); + console.log("select",foundIndex); + if(foundIndex !== -1) { + modalComp.toRemove.splice(foundIndex,1); + } else { + this.getIsRelatedItem(relatedItem) .subscribe((isRelated: boolean) => { + if (!isRelated) { - this.relationshipService.getNameVariant(this.listId, relatedItem.uuid) - .subscribe((nameVariant) => { - const update = { - uuid: this.relationshipType.id + '-' + relatedItem.uuid, - nameVariant, - type: this.relationshipType, - relatedItem, - } as RelationshipIdentifiable; - this.objectUpdatesService.saveAddFieldUpdate(this.url, update); - }); + + + modalComp.toAdd.push(relatedItem); + + // this.relationshipService.getNameVariant(this.listId, relatedItem.uuid) + // .subscribe((nameVariant) => { + // const update = { + // uuid: this.relationshipType.id + '-' + relatedItem.uuid, + // nameVariant, + // type: this.relationshipType, + // relatedItem, + // } as RelationshipIdentifiable; + // this.objectUpdatesService.saveAddFieldUpdate(this.url, update); + // }); } this.loading$.next(true); // emit the last page again to trigger a fieldupdates refresh this.relationshipsRd$.next(this.relationshipsRd$.getValue()); }); + + } }); }; modalComp.deselect = (...selectableObjects: SearchResult[]) => { selectableObjects.forEach((searchResult) => { const relatedItem: Item = searchResult.indexableObject; - this.objectUpdatesService.removeSingleFieldUpdate(this.url, this.relationshipType.id + '-' + relatedItem.uuid); - this.getFieldUpdatesForRelatedItem(relatedItem) - .subscribe((identifiables) => - identifiables.forEach((identifiable) => - this.objectUpdatesService.saveRemoveFieldUpdate(this.url, identifiable) - ) - ); + + let foundIndex = modalComp.toAdd.findIndex( el => el.uuid == relatedItem.uuid); + console.log("deselect",foundIndex); + + if(foundIndex !== -1) { + modalComp.toAdd.splice(foundIndex,1); + } else { + modalComp.toRemove.push(relatedItem); + } + + + // this.objectUpdatesService.removeSingleFieldUpdate(this.url, this.relationshipType.id + '-' + relatedItem.uuid); + // this.getFieldUpdatesForRelatedItem(relatedItem) + // .subscribe((identifiables) => + // identifiables.forEach((identifiable) => + // this.objectUpdatesService.saveRemoveFieldUpdate(this.url, identifiable) + // ) + // ); }); - this.loading$.next(true); + // this.loading$.next(true); // emit the last page again to trigger a fieldupdates refresh - this.relationshipsRd$.next(this.relationshipsRd$.getValue()); + // this.relationshipsRd$.next(this.relationshipsRd$.getValue()); }; + + // TODO: JOIN SUBSCRIPTION BEFORE SUBMIT + modalComp.submitEv = () => { + modalComp.toAdd.forEach((relatedItem)=>{ + this.relationshipService.getNameVariant(this.listId, relatedItem.uuid) + .subscribe((nameVariant) => { + const update = { + uuid: this.relationshipType.id + '-' + relatedItem.uuid, + nameVariant, + type: this.relationshipType, + relatedItem, + } as RelationshipIdentifiable; + this.objectUpdatesService.saveAddFieldUpdate(this.url, update); + }); + }); + + modalComp.toRemove.forEach((relatedItem)=>{ + this.relationshipService.getNameVariant(this.listId, relatedItem.uuid) + .subscribe((nameVariant) => { + const update = { + uuid: this.relationshipType.id + '-' + relatedItem.uuid, + nameVariant, + type: this.relationshipType, + relatedItem, + } as RelationshipIdentifiable; + this.objectUpdatesService.saveRemoveFieldUpdate(this.url,update); + + }); + + // this.objectUpdatesService.removeSingleFieldUpdate(this.url, this.relationshipType.id + '-' + relatedItem.uuid); + // this.getFieldUpdatesForRelatedItem(relatedItem) + // .subscribe((identifiables) => + // identifiables.forEach((identifiable) => + // this.objectUpdatesService.saveRemoveFieldUpdate(this.url, identifiable) + // ) + // ); + }); + + setTimeout(()=>{ + this.submit.emit(); + this.modalRef.close(); + },3000); + + } + this.relatedEntityType$ .pipe(take(1)) .subscribe((relatedEntityType) => { diff --git a/src/app/item-page/edit-item-page/item-relationships/item-relationships.component.ts b/src/app/item-page/edit-item-page/item-relationships/item-relationships.component.ts index 80b908ac06..629e250acc 100644 --- a/src/app/item-page/edit-item-page/item-relationships/item-relationships.component.ts +++ b/src/app/item-page/edit-item-page/item-relationships/item-relationships.component.ts @@ -108,6 +108,10 @@ export class ItemRelationshipsComponent extends AbstractItemUpdateComponent { */ public submit(): void { + this.relationshipService.getItemRelationshipsArray(this.item).pipe( + take(1) + ).subscribe(res=>console.log(res)); + // Get all the relationships that should be removed const removedRelationshipIDs$: Observable = this.relationshipService.getItemRelationshipsArray(this.item).pipe( startWith([]), @@ -115,12 +119,14 @@ export class ItemRelationshipsComponent extends AbstractItemUpdateComponent { Object.assign(new Relationship(), relationship, { uuid: relationship.id }) )), switchMap((relationships: Relationship[]) => { - return this.objectUpdatesService.getFieldUpdatesExclusive(this.url, relationships) as Observable; + return this.objectUpdatesService.getFieldUpdatesAll(this.url, relationships) as Observable; }), - map((fieldUpdates: FieldUpdates) => - Object.values(fieldUpdates) + map((fieldUpdates: FieldUpdates) =>{ + console.log(fieldUpdates); + return Object.values(fieldUpdates) .filter((fieldUpdate: FieldUpdate) => fieldUpdate.changeType === FieldChangeType.REMOVE) .map((fieldUpdate: FieldUpdate) => fieldUpdate.field as DeleteRelationship) + } ), ); @@ -156,6 +162,7 @@ export class ItemRelationshipsComponent extends AbstractItemUpdateComponent { } deleteRelationships(deleteRelationshipIDs: DeleteRelationship[]): Observable[]> { + console.log(deleteRelationshipIDs); return observableZip(...deleteRelationshipIDs.map((deleteRelationship) => { let copyVirtualMetadata: string; if (deleteRelationship.keepLeftVirtualMetadata && deleteRelationship.keepRightVirtualMetadata) { diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.html b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.html index c6d6ebfe88..0023ce86ef 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.html +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.html @@ -79,7 +79,8 @@ class="fas fa-undo-alt">  {{"item.edit.metadata.reinstate-button" | translate}} -