Skip to content

Commit

Permalink
119941: Fixed DynamicLookupRelationModalComponent not working on the …
Browse files Browse the repository at this point in the history
…edit item relationships tab
  • Loading branch information
alexandrevryghem committed Jan 13, 2025
1 parent 4193482 commit e5e6d82
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, EventEmitter, Inject, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { Component, ComponentRef, EventEmitter, Inject, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { LinkService } from '../../../../core/cache/builders/link.service';
import { ObjectUpdatesService } from '../../../../core/data/object-updates/object-updates.service';
Expand Down Expand Up @@ -215,7 +215,7 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy {
this.modalRef = this.modalService.open(ThemedDynamicLookupRelationModalComponent, {
size: 'lg'
});
const modalComp: DynamicLookupRelationModalComponent = this.modalRef.componentInstance.compRef;
const modalComp: ThemedDynamicLookupRelationModalComponent = this.modalRef.componentInstance;
modalComp.repeatable = true;
modalComp.isEditRelationship = true;
modalComp.listId = this.listId;
Expand All @@ -232,112 +232,114 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy {
modalComp.collection = collection;
});

modalComp.select = (...selectableObjects: SearchResult<Item>[]) => {
selectableObjects.forEach((searchResult) => {
const relatedItem: Item = searchResult.indexableObject;
this.subs.push(modalComp.compRef$.pipe(
hasValueOperator(),
map((compRef: ComponentRef<DynamicLookupRelationModalComponent>) => compRef.instance),
).subscribe((modalComponent: DynamicLookupRelationModalComponent) => {
modalComponent.select = (...selectableObjects: SearchResult<Item>[]) => {
selectableObjects.forEach((searchResult) => {
const relatedItem: Item = searchResult.indexableObject;

const foundIndex = modalComp.toRemove.findIndex( el => el.uuid === relatedItem.uuid);
const foundIndex = modalComponent.toRemove.findIndex(el => el.uuid === relatedItem.uuid);

if (foundIndex !== -1) {
modalComp.toRemove.splice(foundIndex,1);
} else {
if (foundIndex !== -1) {
modalComponent.toRemove.splice(foundIndex, 1);
} else {

this.getRelationFromId(relatedItem)
.subscribe((relationship: Relationship) => {
if (!relationship ) {
modalComp.toAdd.push(searchResult);
} else {
const foundIndexRemove = modalComp.toRemove.findIndex( el => el.indexableObject.uuid === relatedItem.uuid);
if (foundIndexRemove !== -1) {
modalComp.toRemove.splice(foundIndexRemove,1);
this.getRelationFromId(relatedItem)
.subscribe((relationship: Relationship) => {
if (!relationship) {
modalComponent.toAdd.push(searchResult);
} else {
const foundIndexRemove = modalComponent.toRemove.findIndex(el => el.indexableObject.uuid === relatedItem.uuid);
if (foundIndexRemove !== -1) {
modalComponent.toRemove.splice(foundIndexRemove, 1);
}
}
}

this.loading$.next(true);
// emit the last page again to trigger a fieldupdates refresh
this.relationshipsRd$.next(this.relationshipsRd$.getValue());
});
}
});
};
modalComp.deselect = (...selectableObjects: SearchResult<Item>[]) => {
selectableObjects.forEach((searchResult) => {
const relatedItem: Item = searchResult.indexableObject;

const foundIndex = modalComp.toAdd.findIndex( el => el.indexableObject.uuid === relatedItem.uuid);

if (foundIndex !== -1) {
modalComp.toAdd.splice(foundIndex,1);
} else {
modalComp.toRemove.push(searchResult);
}
});
};



modalComp.submitEv = () => {

const subscriptions = [];

modalComp.toAdd.forEach((searchResult: SearchResult<Item>) => {
const relatedItem = searchResult.indexableObject;
subscriptions.push(this.relationshipService.getNameVariant(this.listId, relatedItem.uuid).pipe(
map((nameVariant) => {
const update = {
uuid: this.relationshipType.id + '-' + searchResult.indexableObject.uuid,
nameVariant,
type: this.relationshipType,
relatedItem,
} as RelationshipIdentifiable;
this.objectUpdatesService.saveAddFieldUpdate(this.url, update);
return update;
})
));
});

modalComp.toRemove.forEach( (searchResult) => {
subscriptions.push(this.relationshipService.getNameVariant(this.listId, searchResult.indexableObjectuuid).pipe(
switchMap((nameVariant) => {
return this.getRelationFromId(searchResult.indexableObject).pipe(
map( (relationship: Relationship) => {
const update = {
uuid: relationship.id,
nameVariant,
type: this.relationshipType,
relationship,
} as RelationshipIdentifiable;
this.objectUpdatesService.saveRemoveFieldUpdate(this.url,update);
return update;
})
);
})
));
});

observableCombineLatest(subscriptions).subscribe( (res) => {
// Wait until the states changes since there are multiple items
setTimeout( () => {
this.submit.emit();
},1000);

modalComp.isPending = true;
});
};

this.loading$.next(true);
// emit the last page again to trigger a fieldupdates refresh
this.relationshipsRd$.next(this.relationshipsRd$.getValue());
});
}
});
};

modalComp.discardEv = () => {
modalComp.toAdd.forEach( (searchResult) => {
this.selectableListService.deselectSingle(this.listId,searchResult);
});
modalComponent.deselect = (...selectableObjects: SearchResult<Item>[]) => {
selectableObjects.forEach((searchResult) => {
const relatedItem: Item = searchResult.indexableObject;

modalComp.toRemove.forEach( (searchResult) => {
this.selectableListService.selectSingle(this.listId,searchResult);
});
const foundIndex = modalComponent.toAdd.findIndex(el => el.indexableObject.uuid === relatedItem.uuid);

modalComp.toAdd = [];
modalComp.toRemove = [];
};
if (foundIndex !== -1) {
modalComponent.toAdd.splice(foundIndex, 1);
} else {
modalComponent.toRemove.push(searchResult);
}
});
};

modalComponent.submitEv = () => {
const subscriptions = [];

modalComponent.toAdd.forEach((searchResult: SearchResult<Item>) => {
const relatedItem = searchResult.indexableObject;
subscriptions.push(this.relationshipService.getNameVariant(this.listId, relatedItem.uuid).pipe(
map((nameVariant) => {
const update = {
uuid: this.relationshipType.id + '-' + searchResult.indexableObject.uuid,
nameVariant,
type: this.relationshipType,
relatedItem,
} as RelationshipIdentifiable;
this.objectUpdatesService.saveAddFieldUpdate(this.url, update);
return update;
})
));
});

modalComponent.toRemove.forEach((searchResult) => {
subscriptions.push(this.relationshipService.getNameVariant(this.listId, searchResult.indexableObjectuuid).pipe(
switchMap((nameVariant) => {
return this.getRelationFromId(searchResult.indexableObject).pipe(
map((relationship: Relationship) => {
const update = {
uuid: relationship.id,
nameVariant,
type: this.relationshipType,
relationship,
} as RelationshipIdentifiable;
this.objectUpdatesService.saveRemoveFieldUpdate(this.url, update);
return update;
})
);
})
));
});

observableCombineLatest(subscriptions).subscribe((res) => {
// Wait until the states changes since there are multiple items
setTimeout(() => {
this.submit.emit();
}, 1000);

modalComponent.isPending = true;
});
};

modalComponent.discardEv = () => {
modalComponent.toAdd.forEach((searchResult) => {
this.selectableListService.deselectSingle(this.listId, searchResult);
});

modalComponent.toRemove.forEach((searchResult) => {
this.selectableListService.selectSingle(this.listId, searchResult);
});

modalComponent.toAdd = [];
modalComponent.toRemove = [];
};
}));

this.relatedEntityType$
.pipe(take(1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export class DsDynamicFormControlContainerComponent extends DynamicFormControlCo

this.submissionService.dispatchSave(this.model.submissionId);

const modalComp = this.modalRef.componentInstance;
const modalComp: ThemedDynamicLookupRelationModalComponent = this.modalRef.componentInstance;

if (hasValue(this.model.value) && !this.model.readOnly) {
if (typeof this.model.value === 'string') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, NgZone, OnDestroy, OnInit } from '@angular/core';
import { Component, Input, NgZone, OnDestroy, OnInit } from '@angular/core';
import { combineLatest as observableCombineLatest, Observable, Subscription, BehaviorSubject } from 'rxjs';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { hasValue, isNotEmpty } from '../../../../empty.util';
Expand Down Expand Up @@ -28,6 +28,7 @@ import { RemoteDataBuildService } from '../../../../../core/cache/builders/remot
import { getAllSucceededRemoteDataPayload } from '../../../../../core/shared/operators';
import { followLink } from '../../../../utils/follow-link-config.model';
import { RelationshipType } from '../../../../../core/shared/item-relationships/relationship-type.model';
import { Collection } from '../../../../../core/shared/collection.model';

@Component({
selector: 'ds-dynamic-lookup-relation-modal',
Expand All @@ -49,32 +50,32 @@ export class DynamicLookupRelationModalComponent implements OnInit, OnDestroy {
/**
* The label to use to display i18n messages (describing the type of relationship)
*/
label: string;
@Input() label: string;

/**
* Options for searching related items
*/
relationshipOptions: RelationshipOptions;
@Input() relationshipOptions: RelationshipOptions;

/**
* The ID of the list to add/remove selected items to/from
*/
listId: string;
@Input() listId: string;

/**
* The item we're adding relationships to
*/
item;
@Input() item: Item;

/**
* The collection we're submitting an item to
*/
collection;
@Input() collection: Collection;

/**
* Is the selection repeatable?
*/
repeatable: boolean;
@Input() repeatable: boolean;

/**
* The list of selected items
Expand All @@ -84,22 +85,23 @@ export class DynamicLookupRelationModalComponent implements OnInit, OnDestroy {
/**
* The context to display lists
*/
context: Context;
@Input() context: Context;

/**
* The metadata-fields describing these relationships
*/
metadataFields: string;
@Input() metadataFields: string;

query: string;
@Input() query: string;

/**
* A map of subscriptions within this component
*/
subMap: {
[uuid: string]: Subscription
} = {};
submissionId: string;

@Input() submissionId: string;

/**
* A list of the available external sources configured for this relationship
Expand All @@ -119,12 +121,12 @@ export class DynamicLookupRelationModalComponent implements OnInit, OnDestroy {
/**
* The type of relationship
*/
relationshipType: RelationshipType;
@Input() relationshipType: RelationshipType;

/**
* Checks if relationship is left
*/
currentItemIsLeftItem$: Observable<boolean>;
@Input() currentItemIsLeftItem$: Observable<boolean>;

/**
* Relationship is left
Expand All @@ -134,22 +136,22 @@ export class DynamicLookupRelationModalComponent implements OnInit, OnDestroy {
/**
* Checks if modal is being used by edit relationship page
*/
isEditRelationship = false;
@Input() isEditRelationship = false;

/**
* Maintain the list of the related items to be added
*/
toAdd = [];
@Input() toAdd = [];

/**
* Maintain the list of the related items to be removed
*/
toRemove = [];
@Input() toRemove = [];

/**
* Disable buttons while the submit button is pressed
*/
isPending = false;
@Input() isPending = false;

constructor(
public modal: NgbActiveModal,
Expand Down
Loading

0 comments on commit e5e6d82

Please sign in to comment.