mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-10 11:33:04 +00:00
add support for paginated properties on dspace objects
This commit is contained in:

committed by
Kristof De Langhe

parent
8c124f227d
commit
9aff1b6954
59
src/app/core/cache/builders/remote-data-build.service.spec.ts
vendored
Normal file
59
src/app/core/cache/builders/remote-data-build.service.spec.ts
vendored
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@@ -1,6 +1,6 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
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 { PaginatedList } from '../../data/paginated-list';
|
||||
import { RemoteData } from '../../data/remote-data';
|
||||
@@ -190,7 +190,7 @@ export class RemoteDataBuildService {
|
||||
}
|
||||
|
||||
if (hasValue(normalized[relationship].page)) {
|
||||
links[relationship] = this.aggregatePaginatedList(result, normalized[relationship].pageInfo);
|
||||
links[relationship] = this.toPaginatedList(result, normalized[relationship].pageInfo);
|
||||
} else {
|
||||
links[relationship] = result;
|
||||
}
|
||||
@@ -254,8 +254,14 @@ export class RemoteDataBuildService {
|
||||
})
|
||||
}
|
||||
|
||||
aggregatePaginatedList<T>(input: Observable<RemoteData<T[]>>, pageInfo: PageInfo): Observable<RemoteData<PaginatedList<T>>> {
|
||||
return input.map((rd) => Object.assign(rd, {payload: new PaginatedList(pageInfo, rd.payload)}));
|
||||
private toPaginatedList<T>(input: Observable<RemoteData<T[] | PaginatedList<T>>>, pageInfo: PageInfo): Observable<RemoteData<PaginatedList<T>>> {
|
||||
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) });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -6,7 +6,6 @@ import { ObjectCacheService } from '../cache/object-cache.service';
|
||||
import { GlobalConfig } from '../../../config/global-config.interface';
|
||||
import { GenericConstructor } from '../shared/generic-constructor';
|
||||
import { PaginatedList } from './paginated-list';
|
||||
import { NormalizedObject } from '../cache/models/normalized-object.model';
|
||||
import { ResourceType } from '../shared/resource-type';
|
||||
import { RESTURLCombiner } from '../url-combiner/rest-url-combiner';
|
||||
|
||||
@@ -15,7 +14,7 @@ function isObjectLevel(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 */
|
||||
@@ -130,7 +129,7 @@ export abstract class BaseResponseParsingService {
|
||||
}
|
||||
|
||||
processPageInfo(payload: any): PageInfo {
|
||||
if (isNotEmpty(payload.page)) {
|
||||
if (hasValue(payload.page)) {
|
||||
const pageObj = Object.assign({}, payload.page, { _links: payload._links });
|
||||
const pageInfoObject = new DSpaceRESTv2Serializer(PageInfo).deserialize(pageObj);
|
||||
if (pageInfoObject.currentPage >= 0) {
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { filter, map, startWith, tap } from 'rxjs/operators';
|
||||
|
||||
import { DSpaceObject } from './dspace-object.model';
|
||||
import { Collection } from './collection.model';
|
||||
|
@@ -59,7 +59,7 @@ export const toDSpaceObjectListRD = () =>
|
||||
source.pipe(
|
||||
map((rd: RemoteData<PaginatedList<SearchResult<T>>>) => {
|
||||
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});
|
||||
})
|
||||
);
|
||||
|
@@ -3,6 +3,7 @@ import { Component, Input, OnDestroy, OnInit } from '@angular/core';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
|
||||
import { Subscription } from 'rxjs/Subscription';
|
||||
import { hasValue } from '../empty.util';
|
||||
|
||||
@Component({
|
||||
selector: 'ds-loading',
|
||||
@@ -28,7 +29,7 @@ export class LoadingComponent implements OnDestroy, OnInit {
|
||||
}
|
||||
|
||||
ngOnDestroy() {
|
||||
if (this.subscription !== undefined) {
|
||||
if (hasValue(this.subscription)) {
|
||||
this.subscription.unsubscribe();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user