From 48d893e9757f580428ade83e368376f90ef688a4 Mon Sep 17 00:00:00 2001 From: Marie Verdonck Date: Tue, 15 Oct 2019 15:01:51 +0200 Subject: [PATCH] 65600: Pagination on top level communities by combineFlatten list of consecutive page payloads --- .../community-list-adapter.ts | 42 ++++++++++++------- .../community-list.component.ts | 8 ++-- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/app/community-list-page/community-list-adapter.ts b/src/app/community-list-page/community-list-adapter.ts index b5e5ef4ca5..d7ed317f8b 100644 --- a/src/app/community-list-page/community-list-adapter.ts +++ b/src/app/community-list-page/community-list-adapter.ts @@ -1,6 +1,6 @@ import {Injectable} from '@angular/core'; 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 {PaginationComponentOptions} from '../shared/pagination/pagination-component-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 {RemoteData} from '../core/data/remote-data'; import {PaginatedList} from '../core/data/paginated-list'; -import {getCommunityPageRoute} from "../+community-page/community-page-routing.module"; -import {getCollectionPageRoute} from "../+collection-page/collection-page-routing.module"; +import {getCommunityPageRoute} from '../+community-page/community-page-routing.module'; +import {getCollectionPageRoute} from '../+collection-page/collection-page-routing.module'; export interface FlatNode { isExpandable: boolean; @@ -66,7 +66,7 @@ export const showMoreFlatNode = ( @Injectable() export class CommunityListAdapter { - payload$: Observable>; + payload$: Array>>; config: PaginationComponentOptions; sortConfig: SortOptions; @@ -74,33 +74,46 @@ export class CommunityListAdapter { constructor(private cds: CommunityDataService) { this.config = new PaginationComponentOptions(); this.config.id = 'top-level-pagination'; - this.config.pageSize = 10; + this.config.pageSize = 5; this.config.currentPage = 1; this.sortConfig = new SortOptions('dc.title', SortDirection.ASC); this.initTopCommunityList() } private initTopCommunityList(): void { - this.payload$ = this.cds.findTop({ + 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), - ); + )]; + } + + 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 { - return this.payload$ - .pipe( + const res = this.payload$.map((payload) => { + return payload.pipe( take(1), switchMap((result: PaginatedList) => { return this.transformListOfCommunities(result, 0, null, expandedNodes); }), catchError(() => observableOf([])), - tap((results) => console.log('endload', results)), ); + }); + return combineAndFlatten(res); }; private transformListOfCommunities(listOfPaginatedCommunities: PaginatedList, @@ -108,12 +121,12 @@ export class CommunityListAdapter { parent: FlatNode, expandedNodes: FlatNode[]): Observable { if (isNotEmpty(listOfPaginatedCommunities.page)) { - const isNotAllCommunities = (listOfPaginatedCommunities.totalElements > listOfPaginatedCommunities.elementsPerPage); + const isNotAllCommunities = (listOfPaginatedCommunities.totalElements > (listOfPaginatedCommunities.elementsPerPage * this.config.currentPage)); let obsList = listOfPaginatedCommunities.page .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)]; } @@ -123,7 +136,7 @@ export class CommunityListAdapter { } } - private transformCommunity(community: Community, level: number, parent: FlatNode, expandedNodes: FlatNode[], isNotAllCommunities: boolean): Observable { + private transformCommunity(community: Community, level: number, parent: FlatNode, expandedNodes: FlatNode[]): Observable { let isExpanded = false; if (isNotEmpty(expandedNodes)) { isExpanded = hasValue(expandedNodes.find((node) => (node.id === community.id))); @@ -146,7 +159,6 @@ export class CommunityListAdapter { const collectionNodes$ = community.collections.pipe( filter((rd: RemoteData>) => rd.hasSucceeded), take(1), - tap((results) => console.log('collectionstap', results)), map((rd: RemoteData>) => { let nodes$ = rd.payload.page .map((collection: Collection) => toFlatNode(collection, level + 1, false, parent)); diff --git a/src/app/community-list-page/community-list/community-list.component.ts b/src/app/community-list-page/community-list/community-list.component.ts index 0a9d369cdf..7c551342e8 100644 --- a/src/app/community-list-page/community-list/community-list.component.ts +++ b/src/app/community-list-page/community-list/community-list.component.ts @@ -19,11 +19,11 @@ export class CommunityListComponent implements OnInit { dataSource: CommunityListDatasource; - constructor(private communityListService: CommunityListAdapter) { + constructor(private communityListAdapter: CommunityListAdapter) { } ngOnInit() { - this.dataSource = new CommunityListDatasource(this.communityListService); + this.dataSource = new CommunityListDatasource(this.communityListAdapter); this.dataSource.loadCommunities(null); } @@ -47,8 +47,8 @@ export class CommunityListComponent implements OnInit { } getNextPage(): void { - console.log('go to next page'); - // TODO + this.communityListAdapter.getNextPageTopCommunities(); + this.dataSource.loadCommunities(this.expandedNodes); } }