From e9504d380e25373fc5bf119a18d3d022539981db Mon Sep 17 00:00:00 2001 From: David Inga Date: Thu, 21 Dec 2023 12:48:50 +0100 Subject: [PATCH] added comparison data download requests --- client/src/components/search/component.tsx | 2 +- .../analysis-table/component.tsx | 54 +++++++++++++++++-- client/src/hooks/impact/index.ts | 35 +++++++++++- 3 files changed, 84 insertions(+), 7 deletions(-) diff --git a/client/src/components/search/component.tsx b/client/src/components/search/component.tsx index c8434bd0e..b34fb221e 100644 --- a/client/src/components/search/component.tsx +++ b/client/src/components/search/component.tsx @@ -65,7 +65,7 @@ export const Search: React.FC = ({
diff --git a/client/src/containers/analysis-visualization/analysis-table/component.tsx b/client/src/containers/analysis-visualization/analysis-table/component.tsx index ca1e13a35..afab742a1 100644 --- a/client/src/containers/analysis-visualization/analysis-table/component.tsx +++ b/client/src/containers/analysis-visualization/analysis-table/component.tsx @@ -11,7 +11,12 @@ import { useAppSelector } from 'store/hooks'; import { filtersForTabularAPI } from 'store/features/analysis/selector'; import { scenarios } from 'store/features/analysis/scenarios'; import { useIndicators } from 'hooks/indicators'; -import { useImpactData, useDownloadImpactData } from 'hooks/impact'; +import { + useImpactData, + useDownloadImpactData, + useDownloadImpactActualVsScenarioData, + useDownloadImpactScenarioVsScenarioData, +} from 'hooks/impact'; import { useImpactComparison, useImpactScenarioComparison } from 'hooks/impact/comparison'; import AnalysisDynamicMetadata from 'containers/analysis-visualization/analysis-dynamic-metadata'; import { Button } from 'components/button'; @@ -71,6 +76,20 @@ const AnalysisTable = () => { onError: handleResponseError, }); + const downloadActualVsScenarioData = useDownloadImpactActualVsScenarioData({ + onSuccess: () => { + toast.success('Data was downloaded successfully'); + }, + onError: handleResponseError, + }); + + const downloadScenarioVsScenarioData = useDownloadImpactScenarioVsScenarioData({ + onSuccess: () => { + toast.success('Data was downloaded successfully'); + }, + onError: handleResponseError, + }); + const filters = useAppSelector(filtersForTabularAPI); const useIsComparison = useCallback( @@ -187,8 +206,27 @@ const AnalysisTable = () => { }, [impactTable]); const handleDownloadData = useCallback(async () => { - // do not pass pagination params to download data endpoint - const csv = await downloadImpactData.mutateAsync(omit(params, 'page[number]', 'page[size]')); + let csv = null; + // actual vs scenario + if (!currentScenario && scenarioToCompare) { + csv = await downloadActualVsScenarioData.mutateAsync({ + ...omit(params, 'page[number]', 'page[size]'), + comparedScenarioId: scenarioToCompare, + }); + } + // scenario vs scenario + else if (currentScenario && scenarioToCompare) { + csv = await downloadScenarioVsScenarioData.mutateAsync({ + ...omit(params, 'page[number]', 'page[size]'), + baseScenarioId: currentScenario, + comparedScenarioId: scenarioToCompare, + }); + } + // no scenario or comparison + else { + csv = await downloadImpactData.mutateAsync(omit(params, 'page[number]', 'page[size]')); + } + if (csv) { const url = window.URL.createObjectURL(new Blob([csv])); const link = document.createElement('a'); @@ -198,7 +236,15 @@ const AnalysisTable = () => { link.click(); document.body.removeChild(link); } - }, [downloadImpactData, params]); + // do not pass pagination params to download data endpoint + }, [ + currentScenario, + downloadActualVsScenarioData, + downloadImpactData, + downloadScenarioVsScenarioData, + params, + scenarioToCompare, + ]); const handleExpandedChange = useCallback( (table: TableType>) => { diff --git a/client/src/hooks/impact/index.ts b/client/src/hooks/impact/index.ts index 1f43bdd74..bdcec9747 100644 --- a/client/src/hooks/impact/index.ts +++ b/client/src/hooks/impact/index.ts @@ -65,12 +65,43 @@ export const useImpactData = >( return query; }; +const requestDownload = (params) => + apiRawService.get('/impact/table/report', { params }).then((response) => response.data); + export const useDownloadImpactData = (options) => { - const requestDownload = (params) => - apiRawService.get('/impact/table/report', { params }).then((response) => response.data); const mutation = useMutation(requestDownload, { mutationKey: ['download-impact-data'], ...options, }); return mutation; }; + +const requestActualVsScenarioDownload = (params) => + apiRawService + .get('/impact/compare/scenario/vs/actual/report', { + params, + }) + .then((response) => response.data); + +export const useDownloadImpactActualVsScenarioData = (options) => { + const mutation = useMutation(requestActualVsScenarioDownload, { + mutationKey: ['download-actual-vs-scenario-data'], + ...options, + }); + return mutation; +}; + +const requestScenarioVsScenarioDownload = (params) => + apiRawService + .get('/impact/compare/scenario/vs/scenario/report', { + params, + }) + .then((response) => response.data); + +export const useDownloadImpactScenarioVsScenarioData = (options) => { + const mutation = useMutation(requestScenarioVsScenarioDownload, { + mutationKey: ['download-scenario-vs-scenario-data'], + ...options, + }); + return mutation; +};