diff --git a/src/app/+item-page/simple/item-types/shared/item-relationships-utils.ts b/src/app/+item-page/simple/item-types/shared/item-relationships-utils.ts index c63900663d..36c605fdc1 100644 --- a/src/app/+item-page/simple/item-types/shared/item-relationships-utils.ts +++ b/src/app/+item-page/simple/item-types/shared/item-relationships-utils.ts @@ -7,13 +7,13 @@ import { hasNoValue, hasValue } from '../../../../shared/empty.util'; import { Observable } from 'rxjs/internal/Observable'; import { Relationship } from '../../../../core/shared/item-relationships/relationship.model'; import { RelationshipType } from '../../../../core/shared/item-relationships/relationship-type.model'; -import { distinctUntilChanged, filter, flatMap, map, switchMap, tap } from 'rxjs/operators'; -import { of as observableOf, zip as observableZip, combineLatest as observableCombineLatest } from 'rxjs'; -import { ItemDataService } from '../../../../core/data/item-data.service'; +import { distinctUntilChanged, filter, flatMap, map, switchMap } from 'rxjs/operators'; +import { zip as observableZip, combineLatest as observableCombineLatest } from 'rxjs'; import { Item } from '../../../../core/shared/item.model'; import { RemoteData } from '../../../../core/data/remote-data'; import { RelationshipService } from '../../../../core/data/relationship.service'; import { PaginatedList } from '../../../../core/data/paginated-list'; +import { of } from 'rxjs/internal/observable/of'; /** * Operator for comparing arrays using a mapping function @@ -100,6 +100,12 @@ export const relationsToItems = (thisId: string) => distinctUntilChanged(compareArraysUsingIds()), ); +/** + * Operator for turning a paginated list of relationships into a paginated list of the relevant items + * The result is wrapped in the original RemoteData and PaginatedList + * @param {string} thisId The item's id of which the relations belong to + * @returns {(source: Observable) => Observable} + */ export const paginatedRelationsToItems = (thisId: string) => (source: Observable>>): Observable>> => source.pipe( @@ -128,39 +134,27 @@ export const paginatedRelationsToItems = (thisId: string) => }) ); -/** - * Operator for turning a list of relationships and their relationship-types into a list of relevant items by relationship label - * @param thisId The item's id of which the relations belong to - * @param label The label of the relationship-type to filter on - * @param side Filter only on one side of the relationship (for example: child-parent relationships) - */ -export const getRelatedItemsByTypeLabel = (thisId: string, label: string) => - (source: Observable<[Relationship[], RelationshipType[]]>): Observable => - source.pipe( - filterRelationsByTypeLabel(label, thisId), - relationsToItems(thisId) - ); - /** * Operator for turning a list of relationships into a list of metadatarepresentations given the original metadata + * The result is wrapped in the original RemoteData and PaginatedList * @param parentId The id of the parent item * @param itemType The type of relation this list resembles (for creating representations) * @param metadata The list of original Metadatum objects - * @param ids The ItemDataService to use for fetching Items from the Rest API */ -export const relationsToRepresentations = (parentId: string, itemType: string, metadata: MetadataValue[], ids: ItemDataService) => - (source: Observable): Observable => +export const relationsToRepresentations = (parentId: string, itemType: string, metadata: MetadataValue[]) => + (source: Observable>>): Observable>> => source.pipe( - flatMap((rels: Relationship[]) => + flatMap((relRD: RemoteData>) => observableZip( ...metadata .map((metadatum: any) => Object.assign(new MetadataValue(), metadatum)) .map((metadatum: MetadataValue) => { if (metadatum.isVirtual) { - const matchingRels = rels.filter((rel: Relationship) => ('' + rel.id) === metadatum.virtualValue); + const matchingRels = relRD.payload.page.filter((rel: Relationship) => ('' + rel.id) === metadatum.virtualValue); if (matchingRels.length > 0) { const matchingRel = matchingRels[0]; return observableCombineLatest(matchingRel.leftItem, matchingRel.rightItem).pipe( + filter(([leftItem, rightItem]) => leftItem.hasSucceeded && rightItem.hasSucceeded), map(([leftItem, rightItem]) => { if (leftItem.payload.id === parentId) { return rightItem.payload; @@ -172,9 +166,12 @@ export const relationsToRepresentations = (parentId: string, itemType: string, m ); } } else { - return observableOf(Object.assign(new MetadatumRepresentation(itemType), metadatum)); + return of(Object.assign(new MetadatumRepresentation(itemType), metadatum)); } }) + ).pipe( + distinctUntilChanged(compareArraysUsingIds()), + map((representations: MetadataRepresentation[]) => Object.assign(relRD, { payload: { page: representations } })) ) ) ); diff --git a/src/app/+item-page/simple/item-types/shared/item.component.ts b/src/app/+item-page/simple/item-types/shared/item.component.ts index 18aa229611..79255f6d7c 100644 --- a/src/app/+item-page/simple/item-types/shared/item.component.ts +++ b/src/app/+item-page/simple/item-types/shared/item.component.ts @@ -1,57 +1,12 @@ import { Component, Inject } from '@angular/core'; -import { Observable , zip as observableZip, combineLatest as observableCombineLatest } from 'rxjs'; -import { filter, flatMap, map } from 'rxjs/operators'; +import { Observable } from 'rxjs'; import { PaginatedList } from '../../../../core/data/paginated-list'; import { RemoteData } from '../../../../core/data/remote-data'; -import { Relationship } from '../../../../core/shared/item-relationships/relationship.model'; import { Item } from '../../../../core/shared/item.model'; import { ITEM } from '../../../../shared/items/switcher/item-type-switcher.component'; import { MetadataRepresentation } from '../../../../core/shared/metadata-representation/metadata-representation.model'; -import { ItemMetadataRepresentation } from '../../../../core/shared/metadata-representation/item/item-metadata-representation.model'; -import { MetadatumRepresentation } from '../../../../core/shared/metadata-representation/metadatum/metadatum-representation.model'; -import { of } from 'rxjs/internal/observable/of'; -import { MetadataValue } from '../../../../core/shared/metadata.models'; import { RelationshipService } from '../../../../core/data/relationship.service'; - -/** - * Operator for turning a list of relationships into a list of metadatarepresentations given the original metadata - * @param thisId The id of the parent item - * @param itemType The type of relation this list resembles (for creating representations) - * @param metadata The list of original Metadatum objects - */ -export const relationsToRepresentations = (thisId: string, itemType: string, metadata: MetadataValue[]) => - (source: Observable>>): Observable>> => - source.pipe( - flatMap((relRD: RemoteData>) => - observableZip( - ...metadata - .map((metadatum: any) => Object.assign(new MetadataValue(), metadatum)) - .map((metadatum: MetadataValue) => { - if (metadatum.isVirtual) { - const matchingRels = relRD.payload.page.filter((rel: Relationship) => ('' + rel.id) === metadatum.virtualValue); - if (matchingRels.length > 0) { - const matchingRel = matchingRels[0]; - return observableCombineLatest(matchingRel.leftItem, matchingRel.rightItem).pipe( - filter(([leftItem, rightItem]) => leftItem.hasSucceeded && rightItem.hasSucceeded), - map(([leftItem, rightItem]) => { - if (leftItem.payload.id === thisId) { - return rightItem.payload; - } else if (rightItem.payload.id === thisId) { - return leftItem.payload; - } - }), - map((item: Item) => Object.assign(new ItemMetadataRepresentation(), item)) - ); - } - } else { - return of(Object.assign(new MetadatumRepresentation(itemType), metadatum)); - } - }) - ).pipe( - map((representations: MetadataRepresentation[]) => Object.assign(relRD, { payload: { page: representations } })) - ) - ) - ); +import { relationsToRepresentations } from './item-relationships-utils'; @Component({ selector: 'ds-item', diff --git a/themes/mantis/app/+item-page/simple/item-types/publication/publication.component.html b/themes/mantis/app/+item-page/simple/item-types/publication/publication.component.html index 6b6f484183..bb3d6f8bd0 100644 --- a/themes/mantis/app/+item-page/simple/item-types/publication/publication.component.html +++ b/themes/mantis/app/+item-page/simple/item-types/publication/publication.component.html @@ -62,19 +62,19 @@