From a4bf1a64c78579d5869b2bb9db8b9d846a809912 Mon Sep 17 00:00:00 2001 From: Marie Verdonck Date: Wed, 16 Oct 2019 12:37:23 +0200 Subject: [PATCH] 65600: Pagination for subcommunities works; not yet for collections since it needs authorisation --- .../community-list-adapter.ts | 89 ++++++++++++------- .../community-list.component.html | 8 +- .../community-list.component.ts | 29 +++++- src/app/core/data/community-data.service.ts | 16 ++++ 4 files changed, 102 insertions(+), 40 deletions(-) diff --git a/src/app/community-list-page/community-list-adapter.ts b/src/app/community-list-page/community-list-adapter.ts index d7ed317f8b..541a5d0189 100644 --- a/src/app/community-list-page/community-list-adapter.ts +++ b/src/app/community-list-page/community-list-adapter.ts @@ -4,14 +4,15 @@ 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'; -import {catchError, filter, map, switchMap, take, tap} from 'rxjs/operators'; +import {catchError, defaultIfEmpty, filter, map, switchMap, take, tap} from 'rxjs/operators'; import {Community} from '../core/shared/community.model'; import {Collection} from '../core/shared/collection.model'; -import {hasValue, isNotEmpty} from '../shared/empty.util'; +import {hasValue, isEmpty, 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 {CollectionDataService} from '../core/data/collection-data.service'; export interface FlatNode { isExpandable: boolean; @@ -23,6 +24,8 @@ export interface FlatNode { payload: Community | Collection; isShowMoreNode: boolean; route?: string; + currentCommunityPage?: number; + currentCollectionPage?: number; } export const combineAndFlatten = (obsList: Array>): Observable => @@ -66,37 +69,42 @@ export const showMoreFlatNode = ( @Injectable() export class CommunityListAdapter { - payload$: Array>>; + payloads$: Array>>; - config: PaginationComponentOptions; - sortConfig: SortOptions; + topCommunitiesConfig: PaginationComponentOptions; + topCommunitiesSortConfig: SortOptions; - constructor(private cds: CommunityDataService) { - this.config = new PaginationComponentOptions(); - this.config.id = 'top-level-pagination'; - this.config.pageSize = 5; - this.config.currentPage = 1; - this.sortConfig = new SortOptions('dc.title', SortDirection.ASC); + maxSubCommunitiesPerPage: number; + + constructor(private communityDataService: CommunityDataService, private collectionDataService: CollectionDataService) { + this.topCommunitiesConfig = new PaginationComponentOptions(); + this.topCommunitiesConfig.id = 'top-level-pagination'; + this.topCommunitiesConfig.pageSize = 10; + this.topCommunitiesConfig.currentPage = 1; + this.topCommunitiesSortConfig = new SortOptions('dc.title', SortDirection.ASC); this.initTopCommunityList() + + this.maxSubCommunitiesPerPage = 3; } private initTopCommunityList(): void { - this.payload$ = [this.cds.findTop({ - currentPage: this.config.currentPage, - elementsPerPage: this.config.pageSize, - sort: {field: this.sortConfig.field, direction: this.sortConfig.direction} + this.payloads$ = [this.communityDataService.findTop({ + currentPage: this.topCommunitiesConfig.currentPage, + elementsPerPage: this.topCommunitiesConfig.pageSize, + sort: {field: this.topCommunitiesSortConfig.field, direction: this.topCommunitiesSortConfig.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} + this.topCommunitiesConfig.currentPage = this.topCommunitiesConfig.currentPage + 1; + this.payloads$ = [...this.payloads$, this.communityDataService.findTop({ + currentPage: this.topCommunitiesConfig.currentPage, + elementsPerPage: this.topCommunitiesConfig.pageSize, + sort: {field: this.topCommunitiesSortConfig.field, direction: this.topCommunitiesSortConfig.direction} }).pipe( take(1), map((results) => results.payload), @@ -104,7 +112,7 @@ export class CommunityListAdapter { } loadCommunities(expandedNodes: FlatNode[]): Observable { - const res = this.payload$.map((payload) => { + const res = this.payloads$.map((payload) => { return payload.pipe( take(1), switchMap((result: PaginatedList) => { @@ -121,12 +129,16 @@ export class CommunityListAdapter { parent: FlatNode, expandedNodes: FlatNode[]): Observable { if (isNotEmpty(listOfPaginatedCommunities.page)) { - const isNotAllCommunities = (listOfPaginatedCommunities.totalElements > (listOfPaginatedCommunities.elementsPerPage * this.config.currentPage)); + let currentPage = this.topCommunitiesConfig.currentPage; + if (isNotEmpty(parent)) { + currentPage = expandedNodes.find((node: FlatNode) => node.id === parent.id).currentCommunityPage; + } + const isNotAllCommunities = (listOfPaginatedCommunities.totalElements > (listOfPaginatedCommunities.elementsPerPage * currentPage)); let obsList = listOfPaginatedCommunities.page - .map((community: Community) => - this.transformCommunity(community, level, parent, expandedNodes)); - - if (isNotAllCommunities && listOfPaginatedCommunities.currentPage > this.config.currentPage) { + .map((community: Community) => { + return this.transformCommunity(community, level, parent, expandedNodes) + }); + if (isNotAllCommunities && listOfPaginatedCommunities.currentPage > currentPage) { obsList = [...obsList, this.addPossibleShowMoreComunityFlatNode(level, parent)]; } @@ -147,14 +159,25 @@ export class CommunityListAdapter { let obsList = [observableOf([communityFlatNode])]; if (isExpanded) { - const subCommunityNodes$ = community.subcommunities.pipe( - filter((rd: RemoteData>) => rd.hasSucceeded), - take(1), - switchMap((rd: RemoteData>) => - this.transformListOfCommunities(rd.payload, level + 1, communityFlatNode, expandedNodes)) - ); + const currentPage = expandedNodes.find((node: FlatNode) => node.id === community.id).currentCommunityPage; + let subcoms$ = []; + for (let i = 1; i <= currentPage ; i++) { + const p = this.communityDataService.findSubCommunitiesPerParentCommunity(community.uuid,{elementsPerPage: this.maxSubCommunitiesPerPage, currentPage: i}) + .pipe( + filter((rd: RemoteData>) => rd.hasSucceeded), + take(1), + switchMap((rd: RemoteData>) => + this.transformListOfCommunities(rd.payload, level + 1, communityFlatNode, expandedNodes)) - obsList = [...obsList, subCommunityNodes$]; + ); + subcoms$ = [...subcoms$, p]; + } + + obsList = [...obsList, combineAndFlatten(subcoms$)]; + + // need to be authorized (logged in) to receive collections this way + // const cols = this.collectionDataService.getAuthorizedCollectionByCommunity(community.uuid,{elementsPerPage: 2}); + // cols.pipe(take(1)).subscribe((val) => console.log('cols:', val)); const collectionNodes$ = community.collections.pipe( filter((rd: RemoteData>) => rd.hasSucceeded), diff --git a/src/app/community-list-page/community-list/community-list.component.html b/src/app/community-list-page/community-list/community-list.component.html index 5613c09415..77e9e1ee40 100644 --- a/src/app/community-list-page/community-list/community-list.component.html +++ b/src/app/community-list-page/community-list/community-list.component.html @@ -4,10 +4,10 @@
+ (click)="getNextPage(node)"> @@ -26,7 +26,7 @@
- {{node.name}} + {{node.name}} {{node.id}}
@@ -51,7 +51,7 @@
- {{node.name}} + {{node.name}} {{node.id}}
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 7c551342e8..c45e5098ab 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 @@ -2,6 +2,9 @@ import {Component, OnInit} from '@angular/core'; import {CommunityListAdapter, FlatNode} from '../community-list-adapter'; import {CommunityListDatasource} from '../community-list-datasource'; import {FlatTreeControl} from '@angular/cdk/tree'; +import {Collection} from '../../core/shared/collection.model'; +import {Community} from '../../core/shared/community.model'; +import {isEmpty} from "../../shared/empty.util"; @Component({ selector: 'ds-community-list', @@ -42,13 +45,33 @@ export class CommunityListComponent implements OnInit { } else { this.expandedNodes.push(node); node.isExpanded = true; + if (isEmpty(node.currentCollectionPage)) { + node.currentCollectionPage = 1; + } + if (isEmpty(node.currentCommunityPage)) { + node.currentCommunityPage = 1; + } } this.dataSource.loadCommunities(this.expandedNodes); } - getNextPage(): void { - this.communityListAdapter.getNextPageTopCommunities(); - this.dataSource.loadCommunities(this.expandedNodes); + getNextPage(node: FlatNode): void { + if (node.parent != null) { + if (node.parent.isExpandable) { + if (node.payload instanceof Collection) { + const parentNodeInExpandedNodes = this.expandedNodes.find((node2:FlatNode) => node.parent.id === node2.id); + parentNodeInExpandedNodes.currentCollectionPage++; + } + if (node.payload instanceof Community) { + const parentNodeInExpandedNodes = this.expandedNodes.find((node2:FlatNode) => node.parent.id === node2.id); + parentNodeInExpandedNodes.currentCommunityPage++; + } + } + this.dataSource.loadCommunities(this.expandedNodes); + } else { + this.communityListAdapter.getNextPageTopCommunities(); + this.dataSource.loadCommunities(this.expandedNodes); + } } } diff --git a/src/app/core/data/community-data.service.ts b/src/app/core/data/community-data.service.ts index cc55fe6869..f29c3c8833 100644 --- a/src/app/core/data/community-data.service.ts +++ b/src/app/core/data/community-data.service.ts @@ -23,6 +23,7 @@ import { DSOChangeAnalyzer } from './dso-change-analyzer.service'; export class CommunityDataService extends ComColDataService { protected linkPath = 'communities'; protected topLinkPath = 'communities/search/top'; + protected subcommunitiesLinkPath = 'communities/search/subCommunities'; protected cds = this; constructor( @@ -55,4 +56,19 @@ export class CommunityDataService extends ComColDataService { return this.rdbService.buildList(hrefObs) as Observable>>; } + + findSubCommunitiesPerParentCommunity(parentCommunityUUID: string, options: FindAllOptions = {}): Observable>> { + const hrefObs = this.getFindAllHref(options, this.subcommunitiesLinkPath + '?parent=' + parentCommunityUUID); + console.log('subcomurl', hrefObs.pipe(take(1)).subscribe((val) => console.log('subcomurl', val))); + + hrefObs.pipe( + filter((href: string) => hasValue(href)), + take(1)) + .subscribe((href: string) => { + const request = new FindAllRequest(this.requestService.generateRequestId(), href, options); + this.requestService.configure(request); + }); + + return this.rdbService.buildList(hrefObs) as Observable>>; + } }