add support for paginated properties on dspace objects

This commit is contained in:
Art Lowel
2018-10-24 13:03:03 +02:00
committed by Kristof De Langhe
parent 8c124f227d
commit 9aff1b6954
7 changed files with 1591 additions and 9 deletions

View File

@@ -0,0 +1,59 @@
import { RemoteDataBuildService } from './remote-data-build.service';
import { Item } from '../../shared/item.model';
import { PaginatedList } from '../../data/paginated-list';
import { PageInfo } from '../../shared/page-info.model';
import { RemoteData } from '../../data/remote-data';
import { Observable } from 'rxjs/Observable';
const pageInfo = new PageInfo();
const array = [
Object.assign(new Item(), {
metadata: [
{
key: 'dc.title',
language: 'en_US',
value: 'Item nr 1'
}]
}),
Object.assign(new Item(), {
metadata: [
{
key: 'dc.title',
language: 'en_US',
value: 'Item nr 2'
}]
})
];
const paginatedList = new PaginatedList(pageInfo, array);
const arrayRD = new RemoteData(false, false, true, undefined, array);
const paginatedListRD = new RemoteData(false, false, true, undefined, paginatedList);
describe('RemoteDataBuildService', () => {
let service: RemoteDataBuildService;
beforeEach(() => {
service = new RemoteDataBuildService(undefined, undefined, undefined);
});
describe('when toPaginatedList is called', () => {
let expected: RemoteData<PaginatedList<Item>>;
beforeEach(() => {
expected = paginatedListRD;
});
it('should return the correct remoteData of a paginatedList when the input is a (remoteData of an) array', () => {
const result = (service as any).toPaginatedList(Observable.of(arrayRD), pageInfo);
result.subscribe((resultRD) => {
expect(resultRD).toEqual(expected);
});
});
it('should return the correct remoteData of a paginatedList when the input is a (remoteData of a) paginated list', () => {
const result = (service as any).toPaginatedList(Observable.of(paginatedListRD), pageInfo);
result.subscribe((resultRD) => {
expect(resultRD).toEqual(expected);
});
});
});
});

View File

@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { distinctUntilChanged, flatMap, map, startWith } from 'rxjs/operators'; import { distinctUntilChanged, filter, flatMap, map, startWith, switchMap } from 'rxjs/operators';
import { hasValue, hasValueOperator, isEmpty, isNotEmpty } from '../../../shared/empty.util'; import { hasValue, hasValueOperator, isEmpty, isNotEmpty } from '../../../shared/empty.util';
import { PaginatedList } from '../../data/paginated-list'; import { PaginatedList } from '../../data/paginated-list';
import { RemoteData } from '../../data/remote-data'; import { RemoteData } from '../../data/remote-data';
@@ -190,7 +190,7 @@ export class RemoteDataBuildService {
} }
if (hasValue(normalized[relationship].page)) { if (hasValue(normalized[relationship].page)) {
links[relationship] = this.aggregatePaginatedList(result, normalized[relationship].pageInfo); links[relationship] = this.toPaginatedList(result, normalized[relationship].pageInfo);
} else { } else {
links[relationship] = result; links[relationship] = result;
} }
@@ -254,8 +254,14 @@ export class RemoteDataBuildService {
}) })
} }
aggregatePaginatedList<T>(input: Observable<RemoteData<T[]>>, pageInfo: PageInfo): Observable<RemoteData<PaginatedList<T>>> { private toPaginatedList<T>(input: Observable<RemoteData<T[] | PaginatedList<T>>>, pageInfo: PageInfo): Observable<RemoteData<PaginatedList<T>>> {
return input.map((rd) => Object.assign(rd, {payload: new PaginatedList(pageInfo, rd.payload)})); return input.map((rd: RemoteData<T[] | PaginatedList<T>>) => {
if (Array.isArray(rd.payload)) {
return Object.assign(rd, { payload: new PaginatedList(pageInfo, rd.payload) })
} else {
return Object.assign(rd, { payload: new PaginatedList(pageInfo, rd.payload.page) });
}
});
} }
} }

View File

@@ -6,7 +6,6 @@ import { ObjectCacheService } from '../cache/object-cache.service';
import { GlobalConfig } from '../../../config/global-config.interface'; import { GlobalConfig } from '../../../config/global-config.interface';
import { GenericConstructor } from '../shared/generic-constructor'; import { GenericConstructor } from '../shared/generic-constructor';
import { PaginatedList } from './paginated-list'; import { PaginatedList } from './paginated-list';
import { NormalizedObject } from '../cache/models/normalized-object.model';
import { ResourceType } from '../shared/resource-type'; import { ResourceType } from '../shared/resource-type';
import { RESTURLCombiner } from '../url-combiner/rest-url-combiner'; import { RESTURLCombiner } from '../url-combiner/rest-url-combiner';
@@ -15,7 +14,7 @@ function isObjectLevel(halObj: any) {
} }
function isPaginatedResponse(halObj: any) { function isPaginatedResponse(halObj: any) {
return isNotEmpty(halObj.page) && hasValue(halObj._embedded); return hasValue(halObj.page) && hasValue(halObj._embedded);
} }
/* tslint:disable:max-classes-per-file */ /* tslint:disable:max-classes-per-file */
@@ -130,7 +129,7 @@ export abstract class BaseResponseParsingService {
} }
processPageInfo(payload: any): PageInfo { processPageInfo(payload: any): PageInfo {
if (isNotEmpty(payload.page)) { if (hasValue(payload.page)) {
const pageObj = Object.assign({}, payload.page, { _links: payload._links }); const pageObj = Object.assign({}, payload.page, { _links: payload._links });
const pageInfoObject = new DSpaceRESTv2Serializer(PageInfo).deserialize(pageObj); const pageInfoObject = new DSpaceRESTv2Serializer(PageInfo).deserialize(pageObj);
if (pageInfoObject.currentPage >= 0) { if (pageInfoObject.currentPage >= 0) {

View File

@@ -1,4 +1,5 @@
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { filter, map, startWith, tap } from 'rxjs/operators';
import { DSpaceObject } from './dspace-object.model'; import { DSpaceObject } from './dspace-object.model';
import { Collection } from './collection.model'; import { Collection } from './collection.model';

View File

@@ -59,7 +59,7 @@ export const toDSpaceObjectListRD = () =>
source.pipe( source.pipe(
map((rd: RemoteData<PaginatedList<SearchResult<T>>>) => { map((rd: RemoteData<PaginatedList<SearchResult<T>>>) => {
const dsoPage: T[] = rd.payload.page.map((searchResult: SearchResult<T>) => searchResult.dspaceObject); const dsoPage: T[] = rd.payload.page.map((searchResult: SearchResult<T>) => searchResult.dspaceObject);
const payload = Object.assign(rd.payload, { page: dsoPage }) as PaginatedList<T>; const payload = Object.assign(rd.payload, { page: dsoPage }) as any;
return Object.assign(rd, {payload: payload}); return Object.assign(rd, {payload: payload});
}) })
); );

View File

@@ -3,6 +3,7 @@ import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { Subscription } from 'rxjs/Subscription'; import { Subscription } from 'rxjs/Subscription';
import { hasValue } from '../empty.util';
@Component({ @Component({
selector: 'ds-loading', selector: 'ds-loading',
@@ -28,7 +29,7 @@ export class LoadingComponent implements OnDestroy, OnInit {
} }
ngOnDestroy() { ngOnDestroy() {
if (this.subscription !== undefined) { if (hasValue(this.subscription)) {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
} }
} }

1516
yarn.lock

File diff suppressed because it is too large Load Diff