-
Notifications
You must be signed in to change notification settings - Fork 12
/
DownloadButton.jsx
88 lines (80 loc) · 3.27 KB
/
DownloadButton.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import 'core-js/features/url-search-params';
import React from 'react';
import alertifyjs from 'alertifyjs';
import { MenuItem, Menu, Tooltip, Button } from '@mui/material';
import { GetApp as DownloadIcon } from '@mui/icons-material';
import { isArray, map, toUpper, includes, forEach, merge } from 'lodash';
import APIService from '../../services/APIService';
import { downloadObject, arrayToCSV, downloadFromURL, toFullAPIURL } from '../../common/utils';
const DownloadButton = ({formats, includeCSV, resource, filename, buttonFunc, queryParams, tooltip, iconStyle, tooltipPlacement}) => {
const fileName = filename || 'download';
const [fetchedResources, setFetchedResources] = React.useState([]);
const [anchorEl, setAnchorEl] = React.useState(null);
let downloadableFormats = isArray(formats) ? formats : ['json', 'zip'];
if(includeCSV && !includes(downloadableFormats, 'csv'))
downloadableFormats.splice(1, 0, 'csv')
const getQueryString = prefix => {
if(!queryParams)
return ''
const params = new URLSearchParams(queryParams)
return prefix + params.toString()
}
const onFormatClick = format => {
const name = `${fileName}.${format}`;
if(format === 'json') {
if(queryParams && isArray(resource))
fetchAndDownloadResources()
else
downloadJSON(resource)
}
if(format === 'csv')
downloadObject(arrayToCSV(isArray(resource) ? resource : [resource]), 'text/csv', name)
if(format === 'zip' && resource.url)
downloadFromURL(toFullAPIURL(resource.url) + '?format=zip' + getQueryString('&'), name)
setAnchorEl(null)
}
const fetchAndDownloadResources = () => {
alertifyjs.warning('Preparing Download. This might take a minute...')
forEach(resource, object => {
if(object.url)
APIService.new().overrideURL(object.url)
.get(null, null, queryParams)
.then(response => setFetchedResources(prev => [...prev, response.data]))
})
}
React.useEffect(
() => {
if(fetchedResources.length === resource.length) {
downloadJSON(fetchedResources)
setFetchedResources([])
}
},
[fetchedResources]
)
const downloadJSON = objects => downloadObject(
JSON.stringify(objects, null, 2), 'application/json', `${fileName}.json`
)
const tooltipTitle = tooltip || `Download ${map(downloadableFormats, toUpper).join(', ')}`;
return (
<React.Fragment>
<Tooltip arrow title={tooltipTitle} placement={tooltipPlacement}>
{
buttonFunc ? buttonFunc({onClick: event => setAnchorEl(event.currentTarget)}) :
<Button onClick={event => setAnchorEl(event.currentTarget)} style={{minWidth: 'unset', padding: '8px 11px', fontSize: '0.9375rem'}} color='secondary'>
<DownloadIcon fontSize='inherit' style={merge({marginTop: '3px'}, (iconStyle || {}))} />
</Button>
}
</Tooltip>
<Menu anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={() => setAnchorEl(null)}>
{
map(downloadableFormats, format => (
<MenuItem key={format} onClick={() => onFormatClick(format)} style={{fontSize: '0.8rem'}}>
{format.toUpperCase()}
</MenuItem>
))
}
</Menu>
</React.Fragment>
);
}
export default DownloadButton;