64574: Further refactoring of item-component, mantis publication and JSDocs

This commit is contained in:
Kristof De Langhe
2019-08-27 11:36:32 +02:00
parent 4b55d7507c
commit bd54d47037
3 changed files with 24 additions and 72 deletions

View File

@@ -7,13 +7,13 @@ import { hasNoValue, hasValue } from '../../../../shared/empty.util';
import { Observable } from 'rxjs/internal/Observable'; import { Observable } from 'rxjs/internal/Observable';
import { Relationship } from '../../../../core/shared/item-relationships/relationship.model'; import { Relationship } from '../../../../core/shared/item-relationships/relationship.model';
import { RelationshipType } from '../../../../core/shared/item-relationships/relationship-type.model'; import { RelationshipType } from '../../../../core/shared/item-relationships/relationship-type.model';
import { distinctUntilChanged, filter, flatMap, map, switchMap, tap } from 'rxjs/operators'; import { distinctUntilChanged, filter, flatMap, map, switchMap } from 'rxjs/operators';
import { of as observableOf, zip as observableZip, combineLatest as observableCombineLatest } from 'rxjs'; import { zip as observableZip, combineLatest as observableCombineLatest } from 'rxjs';
import { ItemDataService } from '../../../../core/data/item-data.service';
import { Item } from '../../../../core/shared/item.model'; import { Item } from '../../../../core/shared/item.model';
import { RemoteData } from '../../../../core/data/remote-data'; import { RemoteData } from '../../../../core/data/remote-data';
import { RelationshipService } from '../../../../core/data/relationship.service'; import { RelationshipService } from '../../../../core/data/relationship.service';
import { PaginatedList } from '../../../../core/data/paginated-list'; import { PaginatedList } from '../../../../core/data/paginated-list';
import { of } from 'rxjs/internal/observable/of';
/** /**
* Operator for comparing arrays using a mapping function * Operator for comparing arrays using a mapping function
@@ -100,6 +100,12 @@ export const relationsToItems = (thisId: string) =>
distinctUntilChanged(compareArraysUsingIds()), 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<Relationship[]>) => Observable<Item[]>}
*/
export const paginatedRelationsToItems = (thisId: string) => export const paginatedRelationsToItems = (thisId: string) =>
(source: Observable<RemoteData<PaginatedList<Relationship>>>): Observable<RemoteData<PaginatedList<Item>>> => (source: Observable<RemoteData<PaginatedList<Relationship>>>): Observable<RemoteData<PaginatedList<Item>>> =>
source.pipe( 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<Item[]> =>
source.pipe(
filterRelationsByTypeLabel(label, thisId),
relationsToItems(thisId)
);
/** /**
* Operator for turning a list of relationships into a list of metadatarepresentations given the original metadata * 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 parentId The id of the parent item
* @param itemType The type of relation this list resembles (for creating representations) * @param itemType The type of relation this list resembles (for creating representations)
* @param metadata The list of original Metadatum objects * @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) => export const relationsToRepresentations = (parentId: string, itemType: string, metadata: MetadataValue[]) =>
(source: Observable<Relationship[]>): Observable<MetadataRepresentation[]> => (source: Observable<RemoteData<PaginatedList<Relationship>>>): Observable<RemoteData<PaginatedList<MetadataRepresentation>>> =>
source.pipe( source.pipe(
flatMap((rels: Relationship[]) => flatMap((relRD: RemoteData<PaginatedList<Relationship>>) =>
observableZip( observableZip(
...metadata ...metadata
.map((metadatum: any) => Object.assign(new MetadataValue(), metadatum)) .map((metadatum: any) => Object.assign(new MetadataValue(), metadatum))
.map((metadatum: MetadataValue) => { .map((metadatum: MetadataValue) => {
if (metadatum.isVirtual) { 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) { if (matchingRels.length > 0) {
const matchingRel = matchingRels[0]; const matchingRel = matchingRels[0];
return observableCombineLatest(matchingRel.leftItem, matchingRel.rightItem).pipe( return observableCombineLatest(matchingRel.leftItem, matchingRel.rightItem).pipe(
filter(([leftItem, rightItem]) => leftItem.hasSucceeded && rightItem.hasSucceeded),
map(([leftItem, rightItem]) => { map(([leftItem, rightItem]) => {
if (leftItem.payload.id === parentId) { if (leftItem.payload.id === parentId) {
return rightItem.payload; return rightItem.payload;
@@ -172,9 +166,12 @@ export const relationsToRepresentations = (parentId: string, itemType: string, m
); );
} }
} else { } 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 } }))
) )
) )
); );

View File

@@ -1,57 +1,12 @@
import { Component, Inject } from '@angular/core'; import { Component, Inject } from '@angular/core';
import { Observable , zip as observableZip, combineLatest as observableCombineLatest } from 'rxjs'; import { Observable } from 'rxjs';
import { filter, flatMap, map } from 'rxjs/operators';
import { PaginatedList } from '../../../../core/data/paginated-list'; import { PaginatedList } from '../../../../core/data/paginated-list';
import { RemoteData } from '../../../../core/data/remote-data'; 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 '../../../../core/shared/item.model';
import { ITEM } from '../../../../shared/items/switcher/item-type-switcher.component'; import { ITEM } from '../../../../shared/items/switcher/item-type-switcher.component';
import { MetadataRepresentation } from '../../../../core/shared/metadata-representation/metadata-representation.model'; 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'; import { RelationshipService } from '../../../../core/data/relationship.service';
import { relationsToRepresentations } from './item-relationships-utils';
/**
* 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<RemoteData<PaginatedList<Relationship>>>): Observable<RemoteData<PaginatedList<MetadataRepresentation>>> =>
source.pipe(
flatMap((relRD: RemoteData<PaginatedList<Relationship>>) =>
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 } }))
)
)
);
@Component({ @Component({
selector: 'ds-item', selector: 'ds-item',

View File

@@ -62,19 +62,19 @@
<div class="row"> <div class="row">
<div class="col-12 col-md-4" *ngIf="projects$ | async"> <div class="col-12 col-md-4" *ngIf="projects$ | async">
<ds-related-items <ds-related-items
[items]="projects$ | async" [items]="(projects$ | async)?.payload?.page"
[label]="'relationships.isProjectOf' | translate"> [label]="'relationships.isProjectOf' | translate">
</ds-related-items> </ds-related-items>
</div> </div>
<div class="col-12 col-md-4" *ngIf="orgUnits$ | async"> <div class="col-12 col-md-4" *ngIf="orgUnits$ | async">
<ds-related-items <ds-related-items
[items]="orgUnits$ | async" [items]="(orgUnits$ | async)?.payload?.page"
[label]="'relationships.isOrgUnitOf' | translate"> [label]="'relationships.isOrgUnitOf' | translate">
</ds-related-items> </ds-related-items>
</div> </div>
<div class="col-12 col-md-4" *ngIf="journalIssues$ | async"> <div class="col-12 col-md-4" *ngIf="journalIssues$ | async">
<ds-related-items <ds-related-items
[items]="journalIssues$ | async" [items]="(journalIssues$ | async)?.payload?.page"
[label]="'relationships.isJournalIssueOf' | translate"> [label]="'relationships.isJournalIssueOf' | translate">
</ds-related-items> </ds-related-items>
</div> </div>