Skip to content

Commit 8bc1d1b

Browse files
committed
fix: update based on suggestions
1 parent 3cf215a commit 8bc1d1b

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/components/multipart/downloadButton.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,35 @@ type DownloadButtonProps = {
1212
}
1313

1414
export default function DownloadButton(props: DownloadButtonProps) {
15-
function handleAuthenticatedDownload() {
16-
props.getAuthHeaders!()
17-
.then((headersObj: Record<string, any>) => {
18-
const headers = headersObj.headers
19-
return fetch(props.url, { headers })
15+
function handleAuthenticatedDownload(getHeadersFn: GetAuthHeaders) {
16+
getHeadersFn()
17+
.then((headersObj) => {
18+
return fetch(props.url, { ...headersObj })
2019
})
2120
.then((response) => {
2221
return response.blob()
2322
})
2423
.then((blob) => {
2524
const downloadUrl = window.URL.createObjectURL(blob)
26-
const link = document.createElement('a')
27-
link.href = downloadUrl
28-
link.download = props.fileName
29-
document.body.appendChild(link)
30-
link.click()
25+
const temporaryLink = document.createElement('a')
26+
temporaryLink.href = downloadUrl
27+
temporaryLink.download = props.fileName
28+
document.body.appendChild(temporaryLink)
29+
temporaryLink.click()
3130

32-
document.body.removeChild(link)
31+
document.body.removeChild(temporaryLink)
3332
window.URL.revokeObjectURL(downloadUrl)
3433
})
34+
.catch((error) => {
35+
throw new Error('Error downloading file. ' + error.message)
36+
})
3537
}
3638

3739
return (
3840
<Tooltip title="Download" className="rustic-shift-to-right-by-8">
3941
{props.getAuthHeaders ? (
4042
<IconButton
41-
onClick={handleAuthenticatedDownload}
43+
onClick={() => handleAuthenticatedDownload(props.getAuthHeaders!)}
4244
data-cy="download-button"
4345
>
4446
<Icon name="download" />

0 commit comments

Comments
 (0)