From d23c80eadc2617e34bdc5a8900d90f231aca813c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-=C3=89tienne=20Lord?= Date: Wed, 4 Dec 2024 10:22:10 -0500 Subject: [PATCH] refactor(integration): resolving some comments (more complex one) --- .../catalog-library-tool.component.ts | 141 ++++++++++-------- 1 file changed, 80 insertions(+), 61 deletions(-) diff --git a/packages/integration/src/lib/catalog/catalog-library-tool/catalog-library-tool.component.ts b/packages/integration/src/lib/catalog/catalog-library-tool/catalog-library-tool.component.ts index c1581f819..290a2f145 100644 --- a/packages/integration/src/lib/catalog/catalog-library-tool/catalog-library-tool.component.ts +++ b/packages/integration/src/lib/catalog/catalog-library-tool/catalog-library-tool.component.ts @@ -26,7 +26,7 @@ import { import { TranslateModule } from '@ngx-translate/core'; import { Observable, Subscription, combineLatest, forkJoin } from 'rxjs'; -import { map, switchMap, take } from 'rxjs/operators'; +import { concatAll, map, switchMap, take, toArray } from 'rxjs/operators'; import { ToolState } from '../../tool/tool.state'; import { CatalogState } from '../catalog.state'; @@ -207,73 +207,92 @@ export class CatalogLibraryToolComponent implements OnInit, OnDestroy { ]); } - async getCatalogList(): Promise { + private layersInfoFromContexts(): Observable { + return this.contextService.getLocalContexts().pipe( + switchMap((contextsList) => + forkJoin( + contextsList.ours.map((context) => + this.contextService.getLocalContext(context.uri) + ) + ) + ), + concatAll(), + map((detailedContext) => { + return detailedContext.layers.map((layer) => + getInfoFromSourceOptions( + layer.sourceOptions, + detailedContext.title ?? detailedContext.uri + ) + ); + }), + concatAll(), + toArray() + ); + } + + private listExportFromCatalogs(): Observable { let rank = 1; - let finalListExportOutputs: ListExport[] = []; - this.generatelist$$ = - this.getCatalogsAndItemsAndDetailedContexts().subscribe( - ([catalogsWithItems, detailedContexts]) => { - const layerInfosFromDetailedContexts: InfoFromSourceOptions[] = []; - detailedContexts.forEach((detailedContext) => - detailedContext.layers.forEach((layer) => - layerInfosFromDetailedContexts.push( - getInfoFromSourceOptions( - layer.sourceOptions, - detailedContext.title ?? detailedContext.uri - ) - ) + const finalListExportOutputs: ListExport[] = []; + return this.store.entities$.pipe( + switchMap((catalogs) => + combineLatest( + catalogs.map((catalog) => + this.catalogService.loadCatalogItems(catalog).pipe( + map((items) => { + return { catalog, items }; + }) ) - ); - - catalogsWithItems.forEach((catalogAndItems) => { - const catalog = catalogAndItems.catalog; - const loadedCatalogItems = catalogAndItems.items; + ) + ) + ), + map((catalogsWithItems) => { + catalogsWithItems.forEach((catalogAndItems) => { + const catalog = catalogAndItems.catalog; + const loadedCatalogItems = catalogAndItems.items; - const catalogListExports = loadedCatalogItems.reduce( - (catalogListExports, item) => { - if (item.type === CatalogItemType.Group) { - const group = item as CatalogItemGroup; - group.items.forEach((layer: CatalogItemLayer) => { - catalogListExports.push( - this.catalogItemLayerToCatalogOutput( - layer, - rank, - group.title, - catalog.title - ) - ); - rank++; - }); - } else { - const layer = item as CatalogItemLayer; + const catalogListExports = loadedCatalogItems.reduce( + (catalogListExports, item) => { + if (item.type === CatalogItemType.Group) { + const group = item as CatalogItemGroup; + group.items.forEach((layer: CatalogItemLayer) => { catalogListExports.push( - this.catalogItemLayerToCatalogOutput( - layer, - rank, - '', - catalog.title - ) + this.formatLayer(layer, rank, group.title, catalog.title) ); rank++; - } - return catalogListExports; - }, - [] as ListExport[] - ); - finalListExportOutputs.push(...catalogListExports); - }); - - finalListExportOutputs = this.matchLayersWithLayersContext( - finalListExportOutputs, - layerInfosFromDetailedContexts + }); + } else { + const layer = item as CatalogItemLayer; + catalogListExports.push( + this.formatLayer(layer, rank, '', catalog.title) + ); + rank++; + } + return catalogListExports; + }, + [] as ListExport[] ); + finalListExportOutputs.push(...catalogListExports); + }); + return finalListExportOutputs; + }) + ); + } - this.exportExcel(finalListExportOutputs); - } + async getCatalogList(): Promise { + this.generatelist$$ = combineLatest([ + this.layersInfoFromContexts(), + this.listExportFromCatalogs() + ]).subscribe(([layersInfoFromContexts, listExportFromCatalogs]) => { + const listExport = this.matchLayersWithLayersFromContext( + listExportFromCatalogs, + layersInfoFromContexts ); + + this.exportExcel(listExport); + }); } - private catalogItemLayerToCatalogOutput( + private formatLayer( layer: CatalogItemLayer, rank: number, groupTitle: string, @@ -308,11 +327,11 @@ export class CatalogLibraryToolComponent implements OnInit, OnDestroy { * @returns An altered list, with layer/context association */ - private matchLayersWithLayersContext( - catalogOutputs: ListExport[], + private matchLayersWithLayersFromContext( + listExport: ListExport[], layerInfosFromDetailedContexts: InfoFromSourceOptions[] ): ListExport[] { - catalogOutputs.forEach((catalogOutput) => { + listExport.map((catalogOutput) => { const matchingLayersFromContext = layerInfosFromDetailedContexts .filter( (l) => @@ -323,7 +342,7 @@ export class CatalogLibraryToolComponent implements OnInit, OnDestroy { .map((f) => f.context); catalogOutput.context = matchingLayersFromContext.join(','); }); - return catalogOutputs; + return listExport; } /**