80233: Use correct relationship direction

This commit is contained in:
Bruno Roemers
2021-06-22 16:06:49 +02:00
parent d9a8b8f3fd
commit 45ad6b6f0b

View File

@@ -79,6 +79,17 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy {
*/ */
@Input() relationshipType: RelationshipType; @Input() relationshipType: RelationshipType;
/**
* Observable that emits the left and right item type of {@link relationshipType} simultaneously.
*/
private relationshipLeftAndRightType$: Observable<[ItemType, ItemType]>;
/**
* Observable that emits true if {@link itemType} is on the left-hand side of {@link relationshipType},
* false if it is on the right-hand side and undefined in the rare case that it is on neither side.
*/
private currentItemIsLeftItem$: Observable<boolean>;
private relatedEntityType$: Observable<ItemType>; private relatedEntityType$: Observable<ItemType>;
/** /**
@@ -326,18 +337,19 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy {
} }
ngOnInit(): void { ngOnInit(): void {
// store the left and right type of the relationship in a single observable
this.relationshipLeftAndRightType$ = observableCombineLatest([
this.relationshipType.leftType,
this.relationshipType.rightType,
].map((type) => type.pipe(
getFirstSucceededRemoteData(),
getRemoteDataPayload(),
))) as Observable<[ItemType, ItemType]>;
this.relatedEntityType$ = this.relatedEntityType$ = this.relationshipLeftAndRightType$.pipe(
observableCombineLatest([ map((relatedTypes: ItemType[]) => relatedTypes.find((relatedType) => relatedType.uuid !== this.itemType.uuid)),
this.relationshipType.leftType, hasValueOperator()
this.relationshipType.rightType, );
].map((type) => type.pipe(
getFirstSucceededRemoteData(),
getRemoteDataPayload(),
))).pipe(
map((relatedTypes: ItemType[]) => relatedTypes.find((relatedType) => relatedType.uuid !== this.itemType.uuid)),
hasValueOperator()
);
this.relatedEntityType$.pipe( this.relatedEntityType$.pipe(
take(1) take(1)
@@ -345,47 +357,69 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy {
(relatedEntityType) => this.listId = `edit-relationship-${this.itemType.id}-${relatedEntityType.id}` (relatedEntityType) => this.listId = `edit-relationship-${this.itemType.id}-${relatedEntityType.id}`
); );
this.currentItemIsLeftItem$ = this.relationshipLeftAndRightType$.pipe(
map(([leftType, rightType]: [ItemType, ItemType]) => {
if (leftType.id === this.itemType.id) {
return true;
}
if (rightType.id === this.itemType.id) {
return false;
}
// should never happen...
return undefined;
})
);
// initialize the pagination options // initialize the pagination options
this.paginationConfig = new PaginationComponentOptions(); this.paginationConfig = new PaginationComponentOptions();
this.paginationConfig.id = `er${this.relationshipType.id}`; this.paginationConfig.id = `er${this.relationshipType.id}`;
this.paginationConfig.pageSize = 5; this.paginationConfig.pageSize = 5;
this.paginationConfig.currentPage = 1; this.paginationConfig.currentPage = 1;
// get the pagination params from the route
const currentPagination$ = this.paginationService.getCurrentPagination(
this.paginationConfig.id,
this.paginationConfig
).pipe(
tap(() => this.loading$.next(true))
);
this.subs.push( this.subs.push(
// get the pagination params from the route observableCombineLatest([
this.paginationService.getCurrentPagination( currentPagination$,
this.paginationConfig.id, this.currentItemIsLeftItem$,
this.paginationConfig ]).pipe(
).pipe( switchMap(([currentPagination, currentItemIsLeftItem]: [PaginationComponentOptions, boolean]) =>
tap(() => {
this.loading$.next(true);
}),
switchMap((currentPagination: PaginationComponentOptions) =>
// get the relationships for the current item, relationshiptype and page // get the relationships for the current item, relationshiptype and page
this.relationshipService.getItemRelationshipsByLabel( this.relationshipService.getItemRelationshipsByLabel(
this.item, this.item,
this.relationshipType.rightwardType, currentItemIsLeftItem ? this.relationshipType.leftwardType : this.relationshipType.rightwardType,
{ {
elementsPerPage: currentPagination.pageSize, elementsPerPage: currentPagination.pageSize,
currentPage: currentPagination.currentPage currentPage: currentPagination.currentPage,
}, },
false, false,
true, true,
followLink('leftItem'), followLink('leftItem'),
followLink('rightItem') followLink('rightItem'),
)) )),
).subscribe((rd: RemoteData<PaginatedList<Relationship>>) => { ).subscribe((rd: RemoteData<PaginatedList<Relationship>>) => {
this.relationshipsRd$.next(rd); this.relationshipsRd$.next(rd);
})); })
);
// keep isLastPage$ up to date based on relationshipsRd$ // keep isLastPage$ up to date based on relationshipsRd$
this.subs.push(this.relationshipsRd$.pipe( this.subs.push(this.relationshipsRd$.pipe(
hasValueOperator(),
getAllSucceededRemoteData() getAllSucceededRemoteData()
).subscribe((rd: RemoteData<PaginatedList<Relationship>>) => { ).subscribe((rd: RemoteData<PaginatedList<Relationship>>) => {
this.isLastPage$.next(hasNoValue(rd.payload._links.next)); this.isLastPage$.next(hasNoValue(rd.payload._links.next));
})); }));
this.subs.push(this.relationshipsRd$.pipe( this.subs.push(this.relationshipsRd$.pipe(
hasValueOperator(),
getAllSucceededRemoteData(), getAllSucceededRemoteData(),
switchMap((rd: RemoteData<PaginatedList<Relationship>>) => switchMap((rd: RemoteData<PaginatedList<Relationship>>) =>
// emit each relationship in the page separately // emit each relationship in the page separately