Skip to content

Commit 76bbb79

Browse files
authored
Fix download request as csv (#7190)
1 parent 14643c0 commit 76bbb79

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

clients/admin-ui/src/features/privacy-requests/dashboard/PrivacyRequestsDashboard.tsx

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@ import {
77
Pagination,
88
Skeleton,
99
Spin,
10-
useMessage,
1110
} from "fidesui";
1211
import React, { useEffect, useMemo } from "react";
1312

1413
import { BulkActionsDropdown } from "~/features/common/BulkActionsDropdown";
1514
import { useSelection } from "~/features/common/hooks/useSelection";
1615
import { ResultsSelectedCount } from "~/features/common/ResultsSelectedCount";
17-
import {
18-
useLazyDownloadPrivacyRequestCsvV2Query,
19-
useSearchPrivacyRequestsQuery,
20-
} from "~/features/privacy-requests/privacy-requests.slice";
16+
import { useSearchPrivacyRequestsQuery } from "~/features/privacy-requests/privacy-requests.slice";
2117
import { PrivacyRequestResponse } from "~/types/api";
2218

2319
import { useAntPagination } from "../../common/pagination/useAntPagination";
2420
import { DuplicateRequestsButton } from "./DuplicateRequestsButton";
21+
import useDownloadPrivacyRequestReport from "./hooks/useDownloadPrivacyRequestReport";
2522
import { usePrivacyRequestBulkActions } from "./hooks/usePrivacyRequestBulkActions";
2623
import usePrivacyRequestsFilters from "./hooks/usePrivacyRequestsFilters";
2724
import { ListItem } from "./list-item/ListItem";
@@ -34,8 +31,6 @@ export const PrivacyRequestsDashboard = () => {
3431
pagination,
3532
});
3633

37-
const messageApi = useMessage();
38-
3934
const { data, isLoading, isFetching, refetch } =
4035
useSearchPrivacyRequestsQuery({
4136
...filterQueryParams,
@@ -70,23 +65,8 @@ export const PrivacyRequestsDashboard = () => {
7065
clearSelectedIds();
7166
}, [requests, clearSelectedIds]);
7267

73-
const [downloadReport] = useLazyDownloadPrivacyRequestCsvV2Query();
74-
75-
const handleExport = async () => {
76-
let messageStr;
77-
try {
78-
await downloadReport(filterQueryParams);
79-
} catch (error) {
80-
if (error instanceof Error) {
81-
messageStr = error.message;
82-
} else {
83-
messageStr = "Unknown error occurred";
84-
}
85-
}
86-
if (messageStr) {
87-
messageApi.error(messageStr, 5000);
88-
}
89-
};
68+
const { downloadReport, isDownloadingReport } =
69+
useDownloadPrivacyRequestReport();
9070

9171
const { bulkActionMenuItems } = usePrivacyRequestBulkActions({
9272
requests,
@@ -143,7 +123,8 @@ export const PrivacyRequestsDashboard = () => {
143123
aria-label="Export report"
144124
data-testid="export-btn"
145125
icon={<Icons.Download />}
146-
onClick={handleExport}
126+
onClick={() => downloadReport(filterQueryParams)}
127+
loading={isDownloadingReport}
147128
/>
148129
</Flex>
149130
</Flex>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { useMessage } from "fidesui";
2+
3+
import { getErrorMessage } from "~/features/common/helpers";
4+
5+
import {
6+
SearchFilterParams,
7+
useLazyDownloadPrivacyRequestCsvV2Query,
8+
} from "../../privacy-requests.slice";
9+
10+
const useDownloadPrivacyRequestReport = () => {
11+
const messageApi = useMessage();
12+
13+
const [download, { isFetching }] = useLazyDownloadPrivacyRequestCsvV2Query();
14+
15+
const downloadReport = async (args: SearchFilterParams) => {
16+
const result = await download(args);
17+
if (result.isError) {
18+
const message = getErrorMessage(
19+
result.error,
20+
"A problem occurred while generating your privacy request report. Please try again.",
21+
);
22+
messageApi.error(message);
23+
} else {
24+
const a = document.createElement("a");
25+
const csvBlob = new Blob([result.data], { type: "text/csv" });
26+
const csvUrl = window.URL.createObjectURL(csvBlob);
27+
a.href = csvUrl;
28+
a.download = `privacy-request-report.csv`;
29+
a.click();
30+
a.remove();
31+
window.URL.revokeObjectURL(csvUrl);
32+
messageApi.success("Successfully downloaded privacy request report");
33+
}
34+
};
35+
36+
return { downloadReport, isDownloadingReport: isFetching };
37+
};
38+
39+
export default useDownloadPrivacyRequestReport;

clients/admin-ui/src/features/privacy-requests/privacy-requests.slice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ interface DateRangeParams {
116116
to?: string | null;
117117
}
118118

119-
interface SearchFilterParams
119+
export interface SearchFilterParams
120120
extends Partial<PrivacyRequestFilter>,
121121
Partial<DateRangeParams> {}
122122

0 commit comments

Comments
 (0)