From 250508841169c1259aa2da1e328632c1810f43f7 Mon Sep 17 00:00:00 2001 From: Edoardo Sabadelli Date: Fri, 26 Jan 2024 16:09:38 +0100 Subject: [PATCH] fix: adjust download menu and request (DHIS2-16595) --- src/api/analytics.js | 25 +++- .../DownloadMenu/AdvancedSubMenu.js | 1 + src/components/DownloadMenu/DownloadMenu.js | 37 +++-- src/components/DownloadMenu/useDownload.js | 139 +++++++++++------- 4 files changed, 126 insertions(+), 76 deletions(-) diff --git a/src/api/analytics.js b/src/api/analytics.js index 66b8f7b837..2691270213 100644 --- a/src/api/analytics.js +++ b/src/api/analytics.js @@ -35,11 +35,12 @@ export const apiFetchAnalytics = async (dataEngine, visualization, options) => { return [new analyticsEngine.response(rawResponse)] } -export const apiFetchAnalyticsForOutlierTable = async ( - dataEngine, +export const getAnalyticsRequestForOutlierTable = ({ + analyticsEngine, visualization, - options -) => { + options, + forDownload = false, +}) => { const headersMap = getOutlierTableHeadersMap(options) const parameters = { @@ -59,7 +60,7 @@ export const apiFetchAnalyticsForOutlierTable = async ( columns.forEach(({ dimension, items }) => { parameters[dimension] = items.map(({ id }) => id).join(',') - headers.push(headersMap[dimension]) + headers.push(forDownload ? dimension : headersMap[dimension]) }) headers.push('value') @@ -84,9 +85,21 @@ export const apiFetchAnalyticsForOutlierTable = async ( parameters.sortOrder = sorting.direction } + return new analyticsEngine.request().withParameters(parameters) +} + +export const apiFetchAnalyticsForOutlierTable = async ( + dataEngine, + visualization, + options +) => { const analyticsEngine = Analytics.getAnalytics(dataEngine) - const req = new analyticsEngine.request().withParameters(parameters) + const req = getAnalyticsRequestForOutlierTable({ + analyticsEngine, + visualization, + options, + }) const rawResponse = await analyticsEngine.aggregate.getOutliersData(req) diff --git a/src/components/DownloadMenu/AdvancedSubMenu.js b/src/components/DownloadMenu/AdvancedSubMenu.js index 0f536d3aaf..80f6b1dbb5 100644 --- a/src/components/DownloadMenu/AdvancedSubMenu.js +++ b/src/components/DownloadMenu/AdvancedSubMenu.js @@ -24,6 +24,7 @@ export const AdvancedSubMenu = ({ {visType === VIS_TYPE_PIVOT_TABLE ? ( - ) : ( + ) : visType !== VIS_TYPE_OUTLIER_TABLE ? ( - )} + ) : null} - + {visType !== VIS_TYPE_OUTLIER_TABLE && ( + + )} - + {visType !== VIS_TYPE_OUTLIER_TABLE && ( + + )} ) } diff --git a/src/components/DownloadMenu/useDownload.js b/src/components/DownloadMenu/useDownload.js index 07a32c8357..5d15aeb416 100644 --- a/src/components/DownloadMenu/useDownload.js +++ b/src/components/DownloadMenu/useDownload.js @@ -1,7 +1,8 @@ -import { Analytics } from '@dhis2/analytics' +import { Analytics, VIS_TYPE_OUTLIER_TABLE } from '@dhis2/analytics' import { useConfig, useDataEngine, useDataMutation } from '@dhis2/app-runtime' import { useCallback } from 'react' import { useSelector } from 'react-redux' +import { getAnalyticsRequestForOutlierTable } from '../../api/analytics.js' import { sGetChart } from '../../reducers/chart.js' import { sGetCurrent } from '../../reducers/current.js' import { @@ -106,62 +107,87 @@ const useDownload = (relativePeriodDate) => { let req = new analyticsEngine.request() let target = '_top' - switch (type) { - case DOWNLOAD_TYPE_TABLE: - req = req - .fromVisualization(visualization) - .withFormat(format) - .withTableLayout() - .withColumns(columns.join(';')) - .withRows(rows.join(';')) - - req = addCommonParameters(req, visualization, { - relativePeriodDate, - }) - - if (visualization.hideEmptyColumns) { - req = req.withHideEmptyColumns() - } - - if (visualization.hideEmptyRows) { - req = req.withHideEmptyRows() - } - - if (visualization.showHierarchy) { - req = req.withShowHierarchy() - } - - target = format === FILE_FORMAT_HTML_CSS ? '_blank' : '_top' - - break - case DOWNLOAD_TYPE_PLAIN: - req = req - .fromVisualization( - visualization, - path === 'dataValueSet' + if (visType === VIS_TYPE_OUTLIER_TABLE) { + // only DOWNLOAD_TYPE_PLAIN is enabled + // open JSON in new tab + target = [FILE_FORMAT_CSV, FILE_FORMAT_XLS].includes(format) + ? '_top' + : '_blank' + + req = getAnalyticsRequestForOutlierTable({ + analyticsEngine, + visualization, + options: { showHierarchy: visualization.showHierarchy }, + forDownload: true, + }) + + // TODO withRelativePeriodDate + + req = req + .withFormat(format) + .withOutputIdScheme(idScheme) + .withPath('outlierDetection') + } else { + switch (type) { + case DOWNLOAD_TYPE_TABLE: + req = req + .fromVisualization(visualization) + .withFormat(format) + .withTableLayout() + .withColumns(columns.join(';')) + .withRows(rows.join(';')) + + req = addCommonParameters(req, visualization, { + relativePeriodDate, + }) + + if (visualization.hideEmptyColumns) { + req = req.withHideEmptyColumns() + } + + if (visualization.hideEmptyRows) { + req = req.withHideEmptyRows() + } + + if (visualization.showHierarchy) { + req = req.withShowHierarchy() + } + + target = + format === FILE_FORMAT_HTML_CSS ? '_blank' : '_top' + + break + case DOWNLOAD_TYPE_PLAIN: + req = req + .fromVisualization( + visualization, + path === 'dataValueSet' + ) + .withFormat(format) + .withShowHierarchy(visualization.showHierarchy) + .withHierarchyMeta(visualization.showHierarchy) + .withIncludeMetadataDetails(true) + .withIncludeNumDen() + + req = addCommonParameters(req, visualization, { + relativePeriodDate, + }) + + if (path) { + req = req.withPath(path) + } + + if (idScheme) { + req = req.withOutputIdScheme(idScheme) + } + + target = [FILE_FORMAT_CSV, FILE_FORMAT_XLS].includes( + format ) - .withFormat(format) - .withShowHierarchy(visualization.showHierarchy) - .withHierarchyMeta(visualization.showHierarchy) - .withIncludeMetadataDetails(true) - .withIncludeNumDen() - - req = addCommonParameters(req, visualization, { - relativePeriodDate, - }) - - if (path) { - req = req.withPath(path) - } - - if (idScheme) { - req = req.withOutputIdScheme(idScheme) - } - - target = [FILE_FORMAT_CSV, FILE_FORMAT_XLS].includes(format) - ? '_top' - : '_blank' - break + ? '_top' + : '_blank' + break + } } const url = new URL( @@ -182,6 +208,7 @@ const useDownload = (relativePeriodDate) => { relativePeriodDate, rows, visualization, + visType, ] )