Skip to content
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

Zip CSV file downloads so there is only one file #680

Merged
merged 4 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"d3-time-format": "^2.0.3",
"express": "^4.14.0",
"glossary-panel": "^0.2.0",
"jszip": "^3.1.3",
"lodash.debounce": "^4.0.8",
"lodash.flatten": "^4.4.0",
"lodash.lowercase": "^4.3.0",
Expand Down
48 changes: 32 additions & 16 deletions src/components/DownloadDataBtn.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import lowerCase from 'lodash.lowercase'
import React from 'react'
import Zip from 'jszip'

import jsonToCsv from '../util/csv'
import { slugify } from '../util/text'


const downloadData = (fname, data) => {
const file = `${slugify(fname)}.csv`
const dataStr = jsonToCsv(data)

const triggerDataDownload = ({ content, filename, type }) => {
if (window.navigator.msSaveBlob) {
const blob = new Blob([dataStr], { type: 'text/csv' })
window.navigator.msSaveBlob(blob, file);
const blob = new Blob([content], { type })
window.navigator.msSaveBlob(blob, filename);
} else {
const a = document.createElement('a')
const body = document.querySelector('body')
a.download = file
a.href = `data:text/csv,${encodeURIComponent(dataStr)}`
a.download = filename
a.href = `data:${type},${encodeURIComponent(content)}`
body.appendChild(a)
a.click()
body.removeChild(a)
}
}

const downloadUrl = url => {
const triggerUrlDownload = url => {
const a = document.createElement('a')
const body = document.querySelector('body')
a.href = url
Expand All @@ -31,16 +30,33 @@ const downloadUrl = url => {
body.removeChild(a)
}

const DownloadDataBtn = ({ data, text }) => {
const DownloadDataBtn = ({ data, filename, text }) => {
if (!data || data.length === 0) return null

const clickHander = () => {
data.forEach(d => {
if (d.url) {
downloadUrl(d.url)
} else if (d.data.length > 0) {
downloadData(d.filename, d.data)
}
const first = data[0]
const dirname = filename || `${first.filename}`
const multipleFiles = data.length > 1
const zip = new Zip()

if (!multipleFiles && first.url) return triggerUrlDownload(first.url)

zip.file(`${dirname}/README.md`, `# ${lowerCase(dirname)}\n`)
data.forEach(d => (
zip.file(`${dirname}/${slugify(d.filename)}.csv`, jsonToCsv(d.data))
))


return zip.generateAsync({ type: 'base64' }).then(content => {
triggerDataDownload({
content,
filename: `${dirname}.zip`,
type: 'application/zip;base64',
})
}).catch(e => {
/* eslint-disable */
console.error('error from b64 zip', { err: e, args: { data, filename, text } })
/* eslint-enable */
})
}

Expand Down
1 change: 1 addition & 0 deletions src/components/NibrsCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const NibrsCard = ({ crime, data, place, since, title, until }) => {
)}
<DownloadDataBtn
data={download}
filename={`${place}-${crime}-${slugify(title)}-${since}-${until}`}
text='Download data'
/>
</div>
Expand Down