Skip to content

Commit

Permalink
fix(download) (#337)
Browse files Browse the repository at this point in the history
* fix(download)

* fix(link): Link -> a outside react

* fix(router): Regex match files

* style(eslint fix)
  • Loading branch information
paulineribeyre authored Aug 9, 2018
1 parent de46a00 commit e071ecf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
16 changes: 8 additions & 8 deletions src/CoreMetadata/CoreMetadataHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ class CoreMetadataHeader extends Component {

render() {
if (this.props.metadata) {
// display the download button if the user can download this file
// display the download button if the user can download this file
const { user, projectAvail } = this.props;
const projectId = this.props.metadata.project_id;
const canDownload = canUserDownload(user, projectAvail, projectId);
let downloadButton = null;
if (canDownload) {
const downloadLink = `/user/data/download/${this.props.metadata.object_id}?expires_in=10&redirect`;

downloadButton = (
<button
onClick={this.props.onDownloadFile}
className='button-primary-orange'
>
{DOWNLOAD_BTN_CAPTION}
</button>);
<a href={downloadLink}>
<button className='button-primary-orange'>
{DOWNLOAD_BTN_CAPTION}
</button>
</a>);
}

const properties = `${this.props.metadata.data_format} | ${fileSizeTransform(this.props.metadata.file_size)} | ${this.props.metadata.object_id} | ${this.dateTransform(this.props.metadata.updated_datetime)}`;
Expand Down Expand Up @@ -89,7 +90,6 @@ class CoreMetadataHeader extends Component {
CoreMetadataHeader.propTypes = {
metadata: PropTypes.object,
error: PropTypes.string,
onDownloadFile: PropTypes.func.isRequired,
user: PropTypes.object.isRequired,
projectAvail: PropTypes.object.isRequired,
};
Expand Down
30 changes: 1 addition & 29 deletions src/CoreMetadata/reduxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { connect } from 'react-redux';
import CoreMetadataHeader from './CoreMetadataHeader';
import FileTypePicture from '../components/FileTypePicture';
import CoreMetadataTable from './CoreMetadataTable';
import { coreMetadataPath, userapiPath } from '../localconf';
import { coreMetadataPath } from '../localconf';
import { fetchWithCreds } from '../actions';

export const fetchCoreMetadata = objectId =>
Expand All @@ -28,33 +28,6 @@ export const fetchCoreMetadata = objectId =>
})
.then(msg => dispatch(msg));

const downloadFile = id => (dispatch) => {
const path = `${userapiPath}data/download/${id}?expires_in=10&redirect`;
const method = 'GET';
return fetchWithCreds({
path,
method,
})
.then(({ status, data }) => {
switch (status) {
case 200:
return {
type: 'DOWNLOAD_FILE',
file: data,
};
default:
return {
type: 'DOWNLOAD_FILE_ERROR',
error: data.error,
};
}
})
.then((msg) => {
dispatch(msg);
window.location.href = path; // redirect to download
});
};

export const ReduxCoreMetadataHeader = (() => {
const mapStateToProps = state => ({
metadata: state.coreMetadata.metadata,
Expand All @@ -64,7 +37,6 @@ export const ReduxCoreMetadataHeader = (() => {
});

const mapDispatchToProps = dispatch => ({
onDownloadFile: id => dispatch(downloadFile(id)),
});

return connect(mapStateToProps, mapDispatchToProps)(CoreMetadataHeader);
Expand Down
4 changes: 2 additions & 2 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ async function init() {
/>
<Route
exact
path='/files/:object_id'
path='/files/*'
component={
props => (<ProtectedContent
filter={() =>
store.dispatch(fetchCoreMetadata(props.match.params.object_id))
store.dispatch(fetchCoreMetadata(props.match.params[0]))
}
component={CoreMetadataPage}
{...props}
Expand Down

0 comments on commit e071ecf

Please sign in to comment.