-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from bento-platform/fix/drs-download
fix: authorization for DRS object download
- Loading branch information
Showing
3 changed files
with
184 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useSelector } from "react-redux"; | ||
import React, { useCallback } from "react"; | ||
import { Button } from "antd"; | ||
import PropTypes from "prop-types"; | ||
|
||
const DownloadButton = ({ disabled, uri, children }) => { | ||
const { accessToken } = useSelector((state) => state.auth); | ||
|
||
const onClick = useCallback(() => { | ||
if (!uri) return; | ||
|
||
const form = document.createElement("form"); | ||
form.method = "post"; | ||
form.target = "_blank"; | ||
form.action = uri; | ||
form.innerHTML = `<input type="hidden" name="token" value="${accessToken}" />`; | ||
document.body.appendChild(form); | ||
try { | ||
form.submit(); | ||
} finally { | ||
// Even if submit raises for some reason, we still need to clean this up; it has a token in it! | ||
document.body.removeChild(form); | ||
} | ||
}, [uri, accessToken]); | ||
|
||
return ( | ||
<Button key="download" icon="download" disabled={disabled} onClick={onClick}> | ||
{children} | ||
</Button> | ||
); | ||
}; | ||
|
||
DownloadButton.defaultProps = { | ||
disabled: false, | ||
children: "Download", | ||
}; | ||
|
||
DownloadButton.propTypes = { | ||
disabled: PropTypes.bool, | ||
uri: PropTypes.string, | ||
children: PropTypes.oneOfType([PropTypes.object, PropTypes.string]), | ||
}; | ||
|
||
export default DownloadButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters