From edab12f8dedacfbaa4635eee078be8aad4cb23c4 Mon Sep 17 00:00:00 2001 From: trevlenb2 Date: Wed, 14 Feb 2024 20:06:40 +0200 Subject: [PATCH] minor fixes to download csv --- .../planSimulation/components/Simulation.tsx | 3 - .../SimulationMapView/SimulationMapView.tsx | 10 ++- .../modals/DownloadSimulationResultsModal.tsx | 68 ++++++++++++------- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/features/planSimulation/components/Simulation.tsx b/src/features/planSimulation/components/Simulation.tsx index 185d1c29..d2ade1ab 100644 --- a/src/features/planSimulation/components/Simulation.tsx +++ b/src/features/planSimulation/components/Simulation.tsx @@ -1643,9 +1643,6 @@ const Simulation = () => { - void; @@ -1957,10 +1958,15 @@ const SimulationMapView = ({ }) .filter((meta: any) => stats[userDefinedLayer.key] && stats[userDefinedLayer.key][meta]) .map((meta: any) => { + let num: any = stats[userDefinedLayer.key][meta]; + let val = isNumeric(num) + ? (num as number) > 0 + ? Math.round(num as number).toLocaleString('en-US') + : num + : stats[userDefinedLayer.key][meta]; return (

- {meta}:{' '} - {stats[userDefinedLayer.key] ? stats[userDefinedLayer.key][meta] : ''} + {meta}: {stats[userDefinedLayer.key] ? val : ''}

); }); diff --git a/src/features/planSimulation/components/modals/DownloadSimulationResultsModal.tsx b/src/features/planSimulation/components/modals/DownloadSimulationResultsModal.tsx index 8e316a4f..2e70e217 100644 --- a/src/features/planSimulation/components/modals/DownloadSimulationResultsModal.tsx +++ b/src/features/planSimulation/components/modals/DownloadSimulationResultsModal.tsx @@ -1,6 +1,6 @@ import { Button, Col, Form, Modal, Row, Tab, Tabs } from 'react-bootstrap'; import React, { useEffect, useRef, useState } from 'react'; -import { PlanningLocationResponseTagged } from '../../providers/types'; +import { Metadata, PlanningLocationResponseTagged } from '../../providers/types'; import { useForm } from 'react-hook-form'; import { Feature, MultiPolygon, Point, Polygon, Properties } from '@turf/turf'; import { getFullHierarchyJSON } from '../../api'; @@ -162,57 +162,76 @@ const DownloadSimulationResultsModal = ({ inputData, closeHandler, hierarchyIden ): Promise => { let metaList: any = {}; let metaCols: string[] = []; - let rowHeader: string[] = ['Identifier', 'Type', 'Name', 'Geographic Level', 'Type', 'Parent', 'isResult']; + let rowHeader: string[] = ['Identifier', 'Type', 'Name', 'Geographic Level', 'Parent']; let csv = ''; - if (mapDataCurrent.parents) { + if (mapDataCurrent.features) { + let allParentArr: any = {}; let rows = Object.keys(mapDataCurrent.parents) .filter(key => mapDataCurrent.features[key] !== undefined || includeParentData) .map(key => mapDataCurrent.parents[key]) .map((feature: any) => { + let parentArr: { name: any | undefined; id: string | undefined; geographicLevel: string | undefined }[] = []; + + let checkKey = feature.identifier; + do { + if (mapDataCurrent.parents[checkKey]) { + parentArr.push({ + name: mapDataCurrent.parents[checkKey].properties?.name, + geographicLevel: mapDataCurrent.parents[checkKey].properties?.geographicLevel, + id: mapDataCurrent.parents[checkKey].identifier + }); + checkKey = mapDataCurrent.parents[checkKey].properties?.parent; + } + } while (checkKey && mapDataCurrent.parents[checkKey] !== null); + + allParentArr[feature.identifier] = parentArr; + let col = []; col.push(feature.identifier); col.push(feature.type); col.push(feature.properties?.name); col.push(feature.properties?.geographicLevel); - col.push(feature.geometry.type); col.push(feature.properties?.parent); - col.push( - mapDataCurrent.features[feature.identifier] !== null && - mapDataCurrent.features[feature.identifier] !== undefined - ); - let metaItem: any = {}; - if (feature.aggregates) { - Object.keys(feature.aggregates) - .map(key => { + if (feature.properties && feature.properties.metadata) { + feature.properties.metadata + .map((metaV: Metadata) => { return { - key: key, - value: feature.aggregates[key] + key: metaV.type, + value: metaV.value }; }) .forEach((meta: any) => { metaItem[meta.key] = meta.value; - let includes = mapDataCurrent.features[feature.identifier]?.properties?.metadata - ?.map((metaItem: any) => metaItem.type) - .includes(meta.key); - metaItem[meta.key.concat('-isAggregate')] = !(includes !== undefined && includes); - metaList[feature.identifier] = metaItem; if (!metaCols.includes(meta.key)) { metaCols.push(meta.key); } - if (!metaCols.includes(meta.key.concat('-isAggregate'))) { - metaCols.push(meta.key.concat('-isAggregate')); - } }); } - return col; }); + let maxParents = 0; + Object.keys(allParentArr).forEach( + parentArrKey => + (maxParents = allParentArr[parentArrKey].length > maxParents ? allParentArr[parentArrKey].length : maxParents) + ); + + for (let i = 0; i < maxParents - 1; i++) { + rowHeader.push('Parent Id'); + rowHeader.push('Parent Name'); + rowHeader.push('Parent Level'); + } + rows?.forEach(row => { + for (let i = maxParents - 1; i > 0; i--) { + row.push(allParentArr[row[0]][i].id); + row.push(allParentArr[row[0]][i].name); + row.push(allParentArr[row[0]][i].geographicLevel); + } metaCols.forEach(metaCol => { if (!rowHeader.includes(metaCol)) { rowHeader.push(metaCol); @@ -320,8 +339,7 @@ const DownloadSimulationResultsModal = ({ inputData, closeHandler, hierarchyIden {errors.filenameCSV.message} )} - {getIncludeParentSlider()} - {getFullHierarchySlider()} + {t('simulationPage.fieldDelimiter')}