From 7f7c56cbb4ecf1d3f3657283cbe7de2637504fff Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 10 Jan 2022 02:19:29 -0600 Subject: Copied code from old UI in order to fix downloads in mobile app --- modern/src/reports/EventReportPage.js | 85 ++++++++++++++++++++++----------- modern/src/reports/RouteReportPage.js | 37 ++++++++++++-- modern/src/reports/StopReportPage.js | 37 ++++++++++++-- modern/src/reports/SummaryReportPage.js | 46 ++++++++++++++---- modern/src/reports/TripReportPage.js | 37 ++++++++++++-- 5 files changed, 196 insertions(+), 46 deletions(-) (limited to 'modern/src') diff --git a/modern/src/reports/EventReportPage.js b/modern/src/reports/EventReportPage.js index ade1b45..8e81039 100644 --- a/modern/src/reports/EventReportPage.js +++ b/modern/src/reports/EventReportPage.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, FormControl, InputLabel, Select, MenuItem, Typography @@ -14,6 +14,9 @@ import { useTranslation } from '../LocalizationProvider'; const Filter = ({ setItems }) => { const t = useTranslation(); + const inputElement = useRef(); + const [data, setData] = useState({url: '', filename: ''}); + const [eventTypes, setEventTypes] = useState([ 'deviceInactive', 'deviceMoving', @@ -53,7 +56,26 @@ const Filter = ({ setItems }) => { if (contentType === 'application/json') { items['events'] = 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; } } @@ -72,33 +94,40 @@ const Filter = ({ setItems }) => { }; return ( - - + <> + + + - {t('reportEventTypes')} - - - - + {t('reportEventTypes')} + + + + + ); }; diff --git a/modern/src/reports/RouteReportPage.js b/modern/src/reports/RouteReportPage.js index 361e7c0..55f6657 100644 --- a/modern/src/reports/RouteReportPage.js +++ b/modern/src/reports/RouteReportPage.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useRef } from 'react'; import { Paper } from '@material-ui/core'; import { DataGrid } from '@material-ui/data-grid'; import { useTheme } from '@material-ui/core/styles'; @@ -11,6 +11,9 @@ import { useAttributePreference, usePreference } from '../common/preferences'; import { useTranslation } from '../LocalizationProvider'; const Filter = ({ setItems }) => { + const inputElement = useRef(); + const [data, setData] = useState({url: '', filename: ''}); + const handleSubmit = async (deviceId, from, to, mail, headers) => { const query = new URLSearchParams({ deviceId, from, to, mail, @@ -22,13 +25,41 @@ 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 ; + return ( + <> + + + + ); }; diff --git a/modern/src/reports/StopReportPage.js b/modern/src/reports/StopReportPage.js index ab1f299..5012637 100644 --- a/modern/src/reports/StopReportPage.js +++ b/modern/src/reports/StopReportPage.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useRef } from 'react'; import { DataGrid } from '@material-ui/data-grid'; import { useTheme } from '@material-ui/core/styles'; import { @@ -10,6 +10,9 @@ import { useAttributePreference } from '../common/preferences'; import { useTranslation } from '../LocalizationProvider'; const Filter = ({ setItems }) => { + const inputElement = useRef(); + const [data, setData] = useState({url: '', filename: ''}); + const handleSubmit = async (deviceId, from, to, mail, headers) => { const query = new URLSearchParams({ deviceId, from, to, mail, @@ -21,13 +24,41 @@ 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 ; + return ( + <> + + + + ); }; const StopReportPage = () => { 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 ( - - - setDaily(e.target.checked)} />} - label={t('reportDaily')} + <> + - - + + + setDaily(e.target.checked)} />} + label={t('reportDaily')} + /> + + + ); }; diff --git a/modern/src/reports/TripReportPage.js b/modern/src/reports/TripReportPage.js index f2cffc2..74451bc 100644 --- a/modern/src/reports/TripReportPage.js +++ b/modern/src/reports/TripReportPage.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useRef } from 'react'; import { DataGrid } from '@material-ui/data-grid'; import { useTheme } from '@material-ui/core/styles'; import { @@ -10,6 +10,9 @@ import { useAttributePreference } from '../common/preferences'; import { useTranslation } from '../LocalizationProvider'; const Filter = ({ setItems }) => { + const inputElement = useRef(); + const [data, setData] = useState({url: '', filename: ''}); + const handleSubmit = async (deviceId, from, to, mail, headers) => { const query = new URLSearchParams({ deviceId, from, to, mail, @@ -21,13 +24,41 @@ 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 ; + return ( + <> + + + + ); }; const TripReportPage = () => { -- cgit v1.2.3