65528: ComCol flat tree with actual backend comm/coll

> TODO FIX: Last subcom and coll are undefined
This commit is contained in:
Marie Verdonck
2019-10-10 17:59:09 +02:00
parent 69e8221867
commit 2ece89db62
3 changed files with 110 additions and 61 deletions

View File

@@ -1,27 +1,31 @@
import {CommunityForList, CommunityListService} from './CommunityListService';
import {CommunityListService} from './CommunityListService';
import {CollectionViewer, DataSource} from '@angular/cdk/typings/collections';
import {BehaviorSubject, Observable, of} from 'rxjs';
import {catchError, finalize, map, take} from 'rxjs/operators';
import {catchError, finalize, map, take, tap} from 'rxjs/operators';
import {Community} from '../core/shared/community.model';
import {Collection} from '../core/shared/collection.model';
export interface CommunityFlatNode {
export interface FlatNode {
expandable: boolean;
name: string;
handle: string;
level: number;
isExpanded?: boolean;
parent?: CommunityFlatNode;
community: CommunityForList;
parent?: FlatNode;
community: Community;
}
export class CommunityListDataSource implements DataSource<CommunityFlatNode> {
export class CommunityListDataSource implements DataSource<FlatNode> {
private communityListSubject = new BehaviorSubject<CommunityFlatNode[]>([]);
private communityListSubject = new BehaviorSubject<FlatNode[]>([]);
private loadingSubject = new BehaviorSubject<boolean>(false);
public loading$ = this.loadingSubject.asObservable();
constructor(private communityListService: CommunityListService) {}
constructor(private communityListService: CommunityListService) {
}
connect(collectionViewer: CollectionViewer): Observable<CommunityFlatNode[]> {
connect(collectionViewer: CollectionViewer): Observable<FlatNode[]> {
return this.communityListSubject.asObservable();
}
@@ -30,7 +34,7 @@ export class CommunityListDataSource implements DataSource<CommunityFlatNode> {
this.loadingSubject.complete();
}
loadCommunities(expandedNodes: CommunityFlatNode[]) {
loadCommunities(expandedNodes: FlatNode[]) {
this.loadingSubject.next(true);
this.communityListService.getCommunityList()
@@ -38,45 +42,74 @@ export class CommunityListDataSource implements DataSource<CommunityFlatNode> {
take(1),
catchError(() => of([])),
finalize(() => this.loadingSubject.next(false)),
map((result: CommunityForList[]) => {
const communityFlatNodes: CommunityFlatNode[] = [];
const level = 0;
return this.transformListOfCommunities(result, level, communityFlatNodes, null, expandedNodes);
map((result: Community[]) => {
const flatNodes = this.transformListOfCommunities(result, 0, [], null, expandedNodes);
return flatNodes;
})
)
.subscribe((communityFlatNode) => {
this.communityListSubject.next(communityFlatNode)
.subscribe((flatNodes) => {
this.communityListSubject.next(flatNodes)
});
};
transformListOfCommunities(listOfCommunities: CommunityForList[],
transformListOfCommunities(listOfCommunities: Community[],
level: number,
communityFlatNodes: CommunityFlatNode[],
parent: CommunityFlatNode,
expandedNodes: CommunityFlatNode[]): CommunityFlatNode[] {
flatNodes: FlatNode[],
parent: FlatNode,
expandedNodes: FlatNode[]): FlatNode[] {
level++;
for (const community of listOfCommunities) {
const hasSubComs = ((!!community.subcoms && community.subcoms.length > 0));
let expanded = false;
if (expandedNodes != null) {
const expandedNodesFound = expandedNodes.filter((node) => (node.name === community.name));
expanded = (expandedNodesFound.length > 0);
}
console.log(community.name + 'is expanded: ' + expanded);
const communityFlatNode: CommunityFlatNode = {
expandable: hasSubComs,
name: community.name,
level: level,
isExpanded: expanded,
community: community,
parent: parent
}
communityFlatNodes.push(communityFlatNode);
if (hasSubComs && communityFlatNode.isExpanded) {
this.transformListOfCommunities(community.subcoms, level, communityFlatNodes, communityFlatNode, expandedNodes);
if (undefined !== listOfCommunities) {
for (const community of listOfCommunities) {
let expanded = false;
if (expandedNodes != null) {
const expandedNodesFound = expandedNodes.filter((node) => (node.handle === community.handle));
expanded = (expandedNodesFound.length > 0);
}
const communityFlatNode: FlatNode = {
expandable: true,
name: community.name,
handle: community.handle,
level: level,
isExpanded: expanded,
parent: parent,
community: community,
}
flatNodes.push(communityFlatNode);
if (expanded) {
// TODO FIX: Retrieves last subcom or coll as undefined
let subcoms: Community[] = [];
community.subcommunities.pipe(
tap((v) => console.log('subcom tap', v)),
take(1)).subscribe((results) => {
subcoms = results.payload.page;
if (subcoms.length > 0) {
this.transformListOfCommunities(subcoms, level, flatNodes, communityFlatNode, expandedNodes);
}
});
let coll: Collection[] = [];
community.collections.pipe(
tap((v) => console.log('col tap ' ,v)),
take(1),
)
.subscribe((results) => {
coll = results.payload.page;
for (const collection of coll) {
const collectionFlatNode: FlatNode = {
expandable: false,
name: collection.name,
handle: collection.handle,
level: level,
isExpanded: false,
parent: parent,
community: community,
}
flatNodes.push(collectionFlatNode);
}
});
}
}
}
return communityFlatNodes;
return flatNodes;
}
}