65528: last coll/subcom undefined fix (wait for remoteData.succeeded)

This commit is contained in:
Marie Verdonck
2019-10-11 11:06:25 +02:00
parent 514e9a98ed
commit 8e0280cb5a
2 changed files with 23 additions and 27 deletions

View File

@@ -1,9 +1,12 @@
import {CommunityListService} from './CommunityListService';
import {CollectionViewer, DataSource} from '@angular/cdk/typings/collections';
import {BehaviorSubject, Observable, of} from 'rxjs';
import {catchError, finalize, map, take, tap} from 'rxjs/operators';
import {catchError, filter, finalize, map, take, tap} from 'rxjs/operators';
import {Community} from '../core/shared/community.model';
import {Collection} from '../core/shared/collection.model';
import {hasValue, isEmpty} from '../shared/empty.util';
import {RemoteData} from '../core/data/remote-data';
import {PaginatedList} from '../core/data/paginated-list';
export interface FlatNode {
expandable: boolean;
@@ -39,15 +42,13 @@ export class CommunityListDataSource implements DataSource<FlatNode> {
this.communityListService.getCommunityList()
.pipe(
filter((rd: RemoteData<PaginatedList<Community>>) => rd.hasSucceeded),
take(1),
catchError(() => of([])),
finalize(() => this.loadingSubject.next(false)),
map((result: Community[]) => {
const flatNodes = this.transformListOfCommunities(result, 0, [], null, expandedNodes);
return flatNodes;
})
)
.subscribe((flatNodes) => {
.subscribe((result) => {
const communities = result.payload.page;
const flatNodes = this.transformListOfCommunities(communities, -1, [], null, expandedNodes);
this.communityListSubject.next(flatNodes)
});
};
@@ -58,10 +59,10 @@ export class CommunityListDataSource implements DataSource<FlatNode> {
parent: FlatNode,
expandedNodes: FlatNode[]): FlatNode[] {
level++;
if (undefined !== listOfCommunities) {
if (hasValue(listOfCommunities)) {
for (const community of listOfCommunities) {
let expanded = false;
if (expandedNodes != null) {
if (hasValue(expandedNodes)) {
const expandedNodesFound = expandedNodes.filter((node) => (node.handle === community.handle));
expanded = (expandedNodesFound.length > 0);
}
@@ -76,19 +77,21 @@ export class CommunityListDataSource implements DataSource<FlatNode> {
}
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);
}
filter((rd: RemoteData<PaginatedList<Community>>) => rd.hasSucceeded),
take(1),)
.subscribe((results) => {
subcoms = results.payload.page;
if (!isEmpty(subcoms)) {
this.transformListOfCommunities(subcoms, level, flatNodes, communityFlatNode, expandedNodes);
}
});
let coll: Collection[] = [];
community.collections.pipe(
tap((v) => console.log('col tap ' ,v)),
filter((rd: RemoteData<PaginatedList<Collection>>) => rd.hasSucceeded),
take(1),
)
.subscribe((results) => {

View File

@@ -1,10 +1,11 @@
import {Injectable} from '@angular/core';
import {Observable, of} from 'rxjs';
import {Observable} 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 {take} from 'rxjs/operators';
import {Community} from '../core/shared/community.model';
import {RemoteData} from '../core/data/remote-data';
import {PaginatedList} from '../core/data/paginated-list';
@Injectable()
export class CommunityListService {
@@ -20,22 +21,14 @@ export class CommunityListService {
this.config.pageSize = 100;
this.config.currentPage = 1;
this.sortConfig = new SortOptions('dc.title', SortDirection.ASC);
this.initTopCommunityList()
}
private initTopCommunityList(): void {
this.cds.findTop({
public getCommunityList(): Observable<RemoteData<PaginatedList<Community>>> {
return this.cds.findTop({
currentPage: this.config.currentPage,
elementsPerPage: this.config.pageSize,
sort: { field: this.sortConfig.field, direction: this.sortConfig.direction }
}).pipe(take(1)).subscribe((results) => {
this.communities = results.payload.page;
console.log('ping', this.communities);
});
}
public getCommunityList(): Observable<Community[]> {
return of(this.communities);
}
}