@@ -12,33 +12,35 @@ type DownloadButtonProps = {
12
12
}
13
13
14
14
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 } )
20
19
} )
21
20
. then ( ( response ) => {
22
21
return response . blob ( )
23
22
} )
24
23
. then ( ( blob ) => {
25
24
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 ( )
31
30
32
- document . body . removeChild ( link )
31
+ document . body . removeChild ( temporaryLink )
33
32
window . URL . revokeObjectURL ( downloadUrl )
34
33
} )
34
+ . catch ( ( error ) => {
35
+ throw new Error ( 'Error downloading file. ' + error . message )
36
+ } )
35
37
}
36
38
37
39
return (
38
40
< Tooltip title = "Download" className = "rustic-shift-to-right-by-8" >
39
41
{ props . getAuthHeaders ? (
40
42
< IconButton
41
- onClick = { handleAuthenticatedDownload }
43
+ onClick = { ( ) => handleAuthenticatedDownload ( props . getAuthHeaders ! ) }
42
44
data-cy = "download-button"
43
45
>
44
46
< Icon name = "download" />
0 commit comments