diff options
Diffstat (limited to 'modern/src/reports/SummaryReportPage.js')
-rw-r--r-- | modern/src/reports/SummaryReportPage.js | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/modern/src/reports/SummaryReportPage.js b/modern/src/reports/SummaryReportPage.js index 2199d49..4413895 100644 --- a/modern/src/reports/SummaryReportPage.js +++ b/modern/src/reports/SummaryReportPage.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useRef } from 'react'; import { DataGrid } from '@material-ui/data-grid'; import { Grid, FormControlLabel, Checkbox } from '@material-ui/core'; import { useTheme } from '@material-ui/core/styles'; @@ -13,6 +13,8 @@ import { useTranslation } from '../LocalizationProvider'; const Filter = ({ setItems }) => { const t = useTranslation(); + const inputElement = useRef(); + const [data, setData] = useState({url: '', filename: ''}); const [daily, setDaily] = useState(false); const handleSubmit = async (deviceId, from, to, mail, headers) => { @@ -26,21 +28,47 @@ const Filter = ({ setItems }) => { if (contentType === 'application/json') { setItems(await response.json()); } else { - window.location.assign(window.URL.createObjectURL(await response.blob())); + // Copied from /web/app/view/ReportController.js (but without Ext) + const disposition = response.headers.get('content-disposition'); + const filename = disposition.slice(disposition.indexOf('=') + 1, disposition.length); + const blob = new Blob([await response.blob()], { type: contentType }); + if (typeof window.navigator.msSaveBlob !== 'undefined') { + // IE workaround + window.navigator.msSaveBlob(blob, filename); + } else { + const url = window.URL || window.webkitURL; + const downloadUrl = url.createObjectURL(blob); + if (filename) { + setData({url: downloadUrl, filename: filename}); + setTimeout(() => { + inputElement.current.click(); + }, 100); + } + setTimeout(() => { + url.revokeObjectURL(downloadUrl); + }, 100); + } } } } }; return ( - <ReportFilter handleSubmit={handleSubmit}> - <Grid item xs={12} sm={6}> - <FormControlLabel - control={<Checkbox checked={daily} onChange={(e) => setDaily(e.target.checked)} />} - label={t('reportDaily')} + <> + <a style={{display: 'none'}} + href={data.url} + download={data.filename} + ref={inputElement} /> - </Grid> - </ReportFilter> + <ReportFilter handleSubmit={handleSubmit}> + <Grid item xs={12} sm={6}> + <FormControlLabel + control={<Checkbox checked={daily} onChange={(e) => setDaily(e.target.checked)} />} + label={t('reportDaily')} + /> + </Grid> + </ReportFilter> + </> ); }; |