utils/download: take a string instead of AxiosResponse

environments/review-theme-edit-1forjd/deployments/1789
Alex Gleason 2022-12-17 20:15:03 -06:00
rodzic 69a9748b3d
commit b15871aaa8
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
3 zmienionych plików z 10 dodań i 12 usunięć

Wyświetl plik

@ -21,22 +21,22 @@ const Dashboard: React.FC = () => {
const account = useOwnAccount(); const account = useOwnAccount();
const handleSubscribersClick: React.MouseEventHandler = e => { const handleSubscribersClick: React.MouseEventHandler = e => {
dispatch(getSubscribersCsv()).then((response) => { dispatch(getSubscribersCsv()).then(({ data }) => {
download(response, 'subscribers.csv'); download(data, 'subscribers.csv');
}).catch(() => {}); }).catch(() => {});
e.preventDefault(); e.preventDefault();
}; };
const handleUnsubscribersClick: React.MouseEventHandler = e => { const handleUnsubscribersClick: React.MouseEventHandler = e => {
dispatch(getUnsubscribersCsv()).then((response) => { dispatch(getUnsubscribersCsv()).then(({ data }) => {
download(response, 'unsubscribers.csv'); download(data, 'unsubscribers.csv');
}).catch(() => {}); }).catch(() => {});
e.preventDefault(); e.preventDefault();
}; };
const handleCombinedClick: React.MouseEventHandler = e => { const handleCombinedClick: React.MouseEventHandler = e => {
dispatch(getCombinedCsv()).then((response) => { dispatch(getCombinedCsv()).then(({ data }) => {
download(response, 'combined.csv'); download(data, 'combined.csv');
}).catch(() => {}); }).catch(() => {});
e.preventDefault(); e.preventDefault();
}; };

Wyświetl plik

@ -102,8 +102,8 @@ const EventHeader: React.FC<IEventHeader> = ({ status }) => {
}; };
const handleExportClick = () => { const handleExportClick = () => {
dispatch(fetchEventIcs(status.id)).then((response) => { dispatch(fetchEventIcs(status.id)).then(({ data }) => {
download(response, 'calendar.ics'); download(data, 'calendar.ics');
}).catch(() => {}); }).catch(() => {});
}; };

Wyświetl plik

@ -1,9 +1,7 @@
import type { AxiosResponse } from 'axios';
/** Download the file from the response instead of opening it in a tab. */ /** Download the file from the response instead of opening it in a tab. */
// https://stackoverflow.com/a/53230807 // https://stackoverflow.com/a/53230807
export const download = (response: AxiosResponse, filename: string) => { export const download = (data: string, filename: string): void => {
const url = URL.createObjectURL(new Blob([response.data])); const url = URL.createObjectURL(new Blob([data]));
const link = document.createElement('a'); const link = document.createElement('a');
link.href = url; link.href = url;
link.setAttribute('download', filename); link.setAttribute('download', filename);