Skip to content

Commit

Permalink
Replace export-to-csv with json2csv
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Mar 8, 2022
1 parent 7c60dd4 commit 2db1947
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 38 deletions.
88 changes: 66 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"chart.js": "^3.7.1",
"chartjs-adapter-moment": "^1.0.0",
"chartjs-plugin-datalabels": "^2.0.0",
"export-to-csv": "^0.2.1",
"export-to-csv-fix-source-map": "^0.2.1",
"json2csv": "^5.0.7",
"moment": "^2.29.1",
"prop-types": "^15.7.2",
"rc-table": "^7.17.1",
Expand Down
29 changes: 15 additions & 14 deletions src/views/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import SotaChart from '../components/SotaChart'
import { FacebookShareButton, TwitterShareButton, FacebookIcon, TwitterIcon } from 'react-share'
import moment from 'moment'
import { ExportToCsv } from 'export-to-csv'
import { parse } from 'json2csv'

library.add(faEdit)

Expand Down Expand Up @@ -239,19 +239,20 @@ class Task extends React.Component {
}

handleCsvExport () {
const options = {
filename: this.state.item.name,
fieldSeparator: ',',
quoteStrings: '"',
decimalSeparator: '.',
showLabels: true,
showTitle: false,
useTextFile: false,
useBom: true,
useKeysAsHeaders: true
}
const csvExporter = new ExportToCsv(options)
csvExporter.generateCsv(this.state.resultsJson)
const fields = Object.keys(this.state.resultsJson[0])
const opts = { fields }
const csv = parse(this.state.resultsJson, opts)

const element = document.createElement('a')
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(csv))
element.setAttribute('download', this.state.item.name)

element.style.display = 'none'
document.body.appendChild(element)

element.click()

document.body.removeChild(element)
}

render () {
Expand Down

0 comments on commit 2db1947

Please sign in to comment.