Skip to content

Commit

Permalink
added comparison data download requests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsingal committed Dec 21, 2023
1 parent 8979cec commit e9504d3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
2 changes: 1 addition & 1 deletion client/src/components/search/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const Search: React.FC<SearchProps> = ({
<div
className={classnames('bg-white', {
'w-fit min-w-[260px]': size === 'base',
'w-44': size === 'sm',
'flex w-auto max-w-[160px]': size === 'sm',
})}
>
<div className="flex items-center justify-center w-full">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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');
Expand All @@ -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(
<Mode extends ComparisonMode>(table: TableType<ImpactRowType<Mode>>) => {
Expand Down
35 changes: 33 additions & 2 deletions client/src/hooks/impact/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,43 @@ export const useImpactData = <T = ImpactDataApiResponse<false>>(
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;
};

0 comments on commit e9504d3

Please sign in to comment.