65600: Pagination on top level communities by combineFlatten list of consecutive page payloads

This commit is contained in:
Marie Verdonck
2019-10-15 15:01:51 +02:00
parent 1400af3a5e
commit 48d893e975
2 changed files with 31 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {combineLatest as observableCombineLatest} from 'rxjs/internal/observable/combineLatest'; import {combineLatest as observableCombineLatest} from 'rxjs/internal/observable/combineLatest';
import {Observable, of, of as observableOf} from 'rxjs'; import {merge, Observable, of, of as observableOf} from 'rxjs';
import {CommunityDataService} from '../core/data/community-data.service'; import {CommunityDataService} from '../core/data/community-data.service';
import {PaginationComponentOptions} from '../shared/pagination/pagination-component-options.model'; import {PaginationComponentOptions} from '../shared/pagination/pagination-component-options.model';
import {SortDirection, SortOptions} from '../core/cache/models/sort-options.model'; import {SortDirection, SortOptions} from '../core/cache/models/sort-options.model';
@@ -10,8 +10,8 @@ import {Collection} from '../core/shared/collection.model';
import {hasValue, isNotEmpty} from '../shared/empty.util'; import {hasValue, isNotEmpty} from '../shared/empty.util';
import {RemoteData} from '../core/data/remote-data'; import {RemoteData} from '../core/data/remote-data';
import {PaginatedList} from '../core/data/paginated-list'; import {PaginatedList} from '../core/data/paginated-list';
import {getCommunityPageRoute} from "../+community-page/community-page-routing.module"; import {getCommunityPageRoute} from '../+community-page/community-page-routing.module';
import {getCollectionPageRoute} from "../+collection-page/collection-page-routing.module"; import {getCollectionPageRoute} from '../+collection-page/collection-page-routing.module';
export interface FlatNode { export interface FlatNode {
isExpandable: boolean; isExpandable: boolean;
@@ -66,7 +66,7 @@ export const showMoreFlatNode = (
@Injectable() @Injectable()
export class CommunityListAdapter { export class CommunityListAdapter {
payload$: Observable<PaginatedList<Community>>; payload$: Array<Observable<PaginatedList<Community>>>;
config: PaginationComponentOptions; config: PaginationComponentOptions;
sortConfig: SortOptions; sortConfig: SortOptions;
@@ -74,33 +74,46 @@ export class CommunityListAdapter {
constructor(private cds: CommunityDataService) { constructor(private cds: CommunityDataService) {
this.config = new PaginationComponentOptions(); this.config = new PaginationComponentOptions();
this.config.id = 'top-level-pagination'; this.config.id = 'top-level-pagination';
this.config.pageSize = 10; this.config.pageSize = 5;
this.config.currentPage = 1; this.config.currentPage = 1;
this.sortConfig = new SortOptions('dc.title', SortDirection.ASC); this.sortConfig = new SortOptions('dc.title', SortDirection.ASC);
this.initTopCommunityList() this.initTopCommunityList()
} }
private initTopCommunityList(): void { private initTopCommunityList(): void {
this.payload$ = this.cds.findTop({ this.payload$ = [this.cds.findTop({
currentPage: this.config.currentPage, currentPage: this.config.currentPage,
elementsPerPage: this.config.pageSize, elementsPerPage: this.config.pageSize,
sort: {field: this.sortConfig.field, direction: this.sortConfig.direction} sort: {field: this.sortConfig.field, direction: this.sortConfig.direction}
}).pipe( }).pipe(
take(1), take(1),
map((results) => results.payload), map((results) => results.payload),
); )];
}
getNextPageTopCommunities(): void {
this.config.currentPage = this.config.currentPage + 1;
this.payload$ = [...this.payload$, this.cds.findTop({
currentPage: this.config.currentPage,
elementsPerPage: this.config.pageSize,
sort: {field: this.sortConfig.field, direction: this.sortConfig.direction}
}).pipe(
take(1),
map((results) => results.payload),
)];
} }
loadCommunities(expandedNodes: FlatNode[]): Observable<FlatNode[]> { loadCommunities(expandedNodes: FlatNode[]): Observable<FlatNode[]> {
return this.payload$ const res = this.payload$.map((payload) => {
.pipe( return payload.pipe(
take(1), take(1),
switchMap((result: PaginatedList<Community>) => { switchMap((result: PaginatedList<Community>) => {
return this.transformListOfCommunities(result, 0, null, expandedNodes); return this.transformListOfCommunities(result, 0, null, expandedNodes);
}), }),
catchError(() => observableOf([])), catchError(() => observableOf([])),
tap((results) => console.log('endload', results)),
); );
});
return combineAndFlatten(res);
}; };
private transformListOfCommunities(listOfPaginatedCommunities: PaginatedList<Community>, private transformListOfCommunities(listOfPaginatedCommunities: PaginatedList<Community>,
@@ -108,12 +121,12 @@ export class CommunityListAdapter {
parent: FlatNode, parent: FlatNode,
expandedNodes: FlatNode[]): Observable<FlatNode[]> { expandedNodes: FlatNode[]): Observable<FlatNode[]> {
if (isNotEmpty(listOfPaginatedCommunities.page)) { if (isNotEmpty(listOfPaginatedCommunities.page)) {
const isNotAllCommunities = (listOfPaginatedCommunities.totalElements > listOfPaginatedCommunities.elementsPerPage); const isNotAllCommunities = (listOfPaginatedCommunities.totalElements > (listOfPaginatedCommunities.elementsPerPage * this.config.currentPage));
let obsList = listOfPaginatedCommunities.page let obsList = listOfPaginatedCommunities.page
.map((community: Community) => .map((community: Community) =>
this.transformCommunity(community, level, parent, expandedNodes, isNotAllCommunities)); this.transformCommunity(community, level, parent, expandedNodes));
if (isNotAllCommunities) { if (isNotAllCommunities && listOfPaginatedCommunities.currentPage > this.config.currentPage) {
obsList = [...obsList, this.addPossibleShowMoreComunityFlatNode(level, parent)]; obsList = [...obsList, this.addPossibleShowMoreComunityFlatNode(level, parent)];
} }
@@ -123,7 +136,7 @@ export class CommunityListAdapter {
} }
} }
private transformCommunity(community: Community, level: number, parent: FlatNode, expandedNodes: FlatNode[], isNotAllCommunities: boolean): Observable<FlatNode[]> { private transformCommunity(community: Community, level: number, parent: FlatNode, expandedNodes: FlatNode[]): Observable<FlatNode[]> {
let isExpanded = false; let isExpanded = false;
if (isNotEmpty(expandedNodes)) { if (isNotEmpty(expandedNodes)) {
isExpanded = hasValue(expandedNodes.find((node) => (node.id === community.id))); isExpanded = hasValue(expandedNodes.find((node) => (node.id === community.id)));
@@ -146,7 +159,6 @@ export class CommunityListAdapter {
const collectionNodes$ = community.collections.pipe( const collectionNodes$ = community.collections.pipe(
filter((rd: RemoteData<PaginatedList<Collection>>) => rd.hasSucceeded), filter((rd: RemoteData<PaginatedList<Collection>>) => rd.hasSucceeded),
take(1), take(1),
tap((results) => console.log('collectionstap', results)),
map((rd: RemoteData<PaginatedList<Collection>>) => { map((rd: RemoteData<PaginatedList<Collection>>) => {
let nodes$ = rd.payload.page let nodes$ = rd.payload.page
.map((collection: Collection) => toFlatNode(collection, level + 1, false, parent)); .map((collection: Collection) => toFlatNode(collection, level + 1, false, parent));

View File

@@ -19,11 +19,11 @@ export class CommunityListComponent implements OnInit {
dataSource: CommunityListDatasource; dataSource: CommunityListDatasource;
constructor(private communityListService: CommunityListAdapter) { constructor(private communityListAdapter: CommunityListAdapter) {
} }
ngOnInit() { ngOnInit() {
this.dataSource = new CommunityListDatasource(this.communityListService); this.dataSource = new CommunityListDatasource(this.communityListAdapter);
this.dataSource.loadCommunities(null); this.dataSource.loadCommunities(null);
} }
@@ -47,8 +47,8 @@ export class CommunityListComponent implements OnInit {
} }
getNextPage(): void { getNextPage(): void {
console.log('go to next page'); this.communityListAdapter.getNextPageTopCommunities();
// TODO this.dataSource.loadCommunities(this.expandedNodes);
} }
} }