New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFR] Add Export feature #2034
Merged
Merged
[RFR] Add Export feature #2034
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
988487d
Add Export button
fzaninotto b5250a1
Add CSV conversion and download
fzaninotto 06f1904
Extract download logic from button
fzaninotto 893dddd
Allows exporter override from List
fzaninotto dccfad2
Allow exporter to override parser
fzaninotto 7fd8307
tweak exporter syntax
fzaninotto 489f621
syntax tweaks
fzaninotto 6a623ea
Provide a helper for fetching related records
fzaninotto 7a960bb
revert crudGetMany changes
fzaninotto 63c50ec
Make example exporter more concise
fzaninotto 9af29f2
remove unused import
fzaninotto 38c6bd3
do not pass static functions as parameter of exporter
fzaninotto a9c36e4
Add documentation
fzaninotto 767e5e7
document maxResults
fzaninotto 435017c
linting
fzaninotto eefd29f
Add missing exporter PropType
fzaninotto 7f1c9cf
Fix typo
fzaninotto 8aea01a
Doc tweaks
fzaninotto File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import GetApp from '@material-ui/icons/GetApp'; | ||
import { crudGetAll, CRUD_GET_MANY, GET_MANY } from 'ra-core'; | ||
import { unparse as convertToCSV } from 'papaparse/papaparse.min'; | ||
|
||
import Button from './Button'; | ||
import downloadCSV from '../util/downloadCSV'; | ||
|
||
const sanitizeRestProps = ({ | ||
basePath, | ||
crudGetAll, | ||
dispatch, | ||
exporter, | ||
filter, | ||
maxResults, | ||
resource, | ||
sort, | ||
...rest | ||
}) => rest; | ||
|
||
/** | ||
* Helper function for calling the data provider with GET_MANY | ||
* via redux and saga, and getting a Promise in return | ||
* | ||
* @example | ||
* fetchRelatedRecords(records, 'post_id', 'posts').then(posts => | ||
* posts.map(record => ({ | ||
* ...record, | ||
* post_title: posts[record.post_id].title, | ||
* })); | ||
*/ | ||
const fetchRelatedRecords = dispatch => (data, field, resource) => | ||
new Promise((resolve, reject) => { | ||
// find unique keys | ||
const ids = [...new Set(data.map(record => record[field]))]; | ||
dispatch({ | ||
type: CRUD_GET_MANY, | ||
payload: { ids }, | ||
meta: { | ||
resource, | ||
fetch: GET_MANY, | ||
onSuccess: { | ||
callback: ({ payload: { data } }) => { | ||
resolve( | ||
data.reduce((acc, post) => { | ||
acc[post.id] = post; | ||
return acc; | ||
}, {}) | ||
); | ||
}, | ||
}, | ||
onFailure: { | ||
notification: { | ||
body: 'ra.notification.http_error', | ||
level: 'warning', | ||
}, | ||
callback: ({ error }) => reject(error), | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
class ExportButton extends Component { | ||
static propTypes = { | ||
basePath: PropTypes.string, | ||
dispatch: PropTypes.func, | ||
exporter: PropTypes.func, | ||
filter: PropTypes.object, | ||
label: PropTypes.string, | ||
maxResults: PropTypes.number.isRequired, | ||
resource: PropTypes.string.isRequired, | ||
sort: PropTypes.object, | ||
}; | ||
|
||
handleClick = () => { | ||
const { | ||
dispatch, | ||
exporter, | ||
filter, | ||
maxResults, | ||
sort, | ||
resource, | ||
} = this.props; | ||
dispatch( | ||
crudGetAll( | ||
resource, | ||
sort, | ||
filter, | ||
maxResults, | ||
({ payload: { data } }) => | ||
exporter | ||
? exporter( | ||
data, | ||
fetchRelatedRecords(dispatch), | ||
dispatch | ||
) | ||
: downloadCSV(resource)(convertToCSV(data)) | ||
) | ||
); | ||
}; | ||
|
||
render() { | ||
const { label, ...rest } = this.props; | ||
|
||
return ( | ||
<Button | ||
onClick={this.handleClick} | ||
label={label} | ||
{...sanitizeRestProps(rest)} | ||
> | ||
<GetApp /> | ||
</Button> | ||
); | ||
} | ||
} | ||
|
||
ExportButton.defaultProps = { | ||
label: 'ra.action.export', | ||
maxResults: 1000, | ||
}; | ||
|
||
export default connect()(ExportButton); // inject redux dispatch |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
spottedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on purpose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh ?