From 604eabcd97c6e7ddb42316a5a1fb0abc24e8dc7f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 14 Nov 2020 23:10:22 -0800 Subject: Support excel reports --- modern/src/reports/EventReportPage.js | 23 +++++++++++++++-------- modern/src/reports/ReplayPage.js | 6 +++--- modern/src/reports/ReportFilter.js | 23 ++++++++++++++++------- modern/src/reports/ReportLayoutPage.js | 6 +----- modern/src/reports/RouteReportPage.js | 18 +++++++++++++----- modern/src/reports/StopReportPage.js | 18 +++++++++++++----- modern/src/reports/SummaryReportPage.js | 18 +++++++++++++----- modern/src/reports/TripReportPage.js | 18 +++++++++++++----- 8 files changed, 87 insertions(+), 43 deletions(-) (limited to 'modern/src') diff --git a/modern/src/reports/EventReportPage.js b/modern/src/reports/EventReportPage.js index b11933a..13e081e 100644 --- a/modern/src/reports/EventReportPage.js +++ b/modern/src/reports/EventReportPage.js @@ -6,16 +6,23 @@ import { formatPosition } from '../common/formatter'; import ReportFilter from './ReportFilter'; import ReportLayoutPage from './ReportLayoutPage'; -const ReportFilterForm = ({ onResult }) => { +const ReportFilterForm = ({ setItems }) => { - const [eventType, setEventType] = useState(['allEvents']); + const [eventTypes, setEventTypes] = useState(['allEvents']); - const handleSubmit = async (deviceId, from, to) => { - const query = new URLSearchParams({ deviceId, from, to }); - eventType.forEach(it => query.append('type', it)); - const response = await fetch(`/api/reports/events?${query.toString()}`, { headers: { Accept: 'application/json' } }); + const handleSubmit = async (deviceId, from, to, mail, headers) => { + const query = new URLSearchParams({ deviceId, from, to, mail }); + eventTypes.forEach(it => query.append('type', it)); + const response = await fetch(`/api/reports/events?${query.toString()}`, { headers }); if (response.ok) { - onResult(await response.json()); + const contentType = response.headers.get('content-type'); + if (contentType) { + if (contentType === 'application/json') { + setItems(await response.json()); + } else { + window.location.assign(window.URL.createObjectURL(await response.blob())); + } + } } }; @@ -23,7 +30,7 @@ const ReportFilterForm = ({ onResult }) => { {t('reportEventTypes')} - setEventTypes(e.target.value)} multiple> {t('eventAll')} {t('eventDeviceOnline')} {t('eventDeviceUnknown')} diff --git a/modern/src/reports/ReplayPage.js b/modern/src/reports/ReplayPage.js index 5c27cb5..dfa9999 100644 --- a/modern/src/reports/ReplayPage.js +++ b/modern/src/reports/ReplayPage.js @@ -46,9 +46,9 @@ const ReplayPage = () => { const [positions, setPositions] = useState([]); const [index, setIndex] = useState(0); - const handleSubmit = async (deviceId, from, to) => { + const handleSubmit = async (deviceId, from, to, _, headers) => { const query = new URLSearchParams({ deviceId, from, to }); - const response = await fetch(`/api/positions?${query.toString()}`, { headers: { 'Accept': 'application/json' } }); + const response = await fetch(`/api/positions?${query.toString()}`, { headers }); if (response.ok) { setIndex(0); setPositions(await response.json()); @@ -88,7 +88,7 @@ const ReplayPage = () => { - ; + diff --git a/modern/src/reports/ReportFilter.js b/modern/src/reports/ReportFilter.js index 9bc88bb..164dbf9 100644 --- a/modern/src/reports/ReportFilter.js +++ b/modern/src/reports/ReportFilter.js @@ -1,17 +1,17 @@ import React, { useState } from 'react'; -import { FormControl, InputLabel, Select, MenuItem, Button, TextField } from '@material-ui/core'; +import { FormControl, InputLabel, Select, MenuItem, Button, TextField, ButtonGroup } from '@material-ui/core'; import t from '../common/localization'; import { useSelector } from 'react-redux'; import moment from 'moment'; -const ReportFilter = ({ children, handleSubmit }) => { +const ReportFilter = ({ children, handleSubmit, showOnly }) => { const devices = useSelector(state => Object.values(state.devices.items)); const [deviceId, setDeviceId] = useState(); const [period, setPeriod] = useState('today'); const [from, setFrom] = useState(moment().subtract(1, 'hour')); const [to, setTo] = useState(moment()); - const handleShow = () => { + const handleClick = (mail, json) => { let selectedFrom; let selectedTo; switch (period) { @@ -45,7 +45,14 @@ const ReportFilter = ({ children, handleSubmit }) => { break; } - handleSubmit(deviceId, selectedFrom.toISOString(), selectedTo.toISOString()); + const accept = json ? 'application/json' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; + handleSubmit( + deviceId, + selectedFrom.toISOString(), + selectedTo.toISOString(), + mail, + { Accept: accept } + ); } return ( @@ -92,9 +99,11 @@ const ReportFilter = ({ children, handleSubmit }) => { )} {children} - + + + {!showOnly && } + {!showOnly && } + ); diff --git a/modern/src/reports/ReportLayoutPage.js b/modern/src/reports/ReportLayoutPage.js index 296b7c6..ddc7325 100644 --- a/modern/src/reports/ReportLayoutPage.js +++ b/modern/src/reports/ReportLayoutPage.js @@ -20,10 +20,6 @@ const useStyles = makeStyles(theme => ({ const ReportLayoutPage = ({ reportFilterForm:ReportFilterForm, setItems, children }) => { const classes = useStyles(); - - const onResult = (data) => { - setItems(data); - } return (
@@ -31,7 +27,7 @@ const ReportLayoutPage = ({ reportFilterForm:ReportFilterForm, setItems, childre - + diff --git a/modern/src/reports/RouteReportPage.js b/modern/src/reports/RouteReportPage.js index 7ff5d40..43d310b 100644 --- a/modern/src/reports/RouteReportPage.js +++ b/modern/src/reports/RouteReportPage.js @@ -5,15 +5,23 @@ import { formatPosition } from '../common/formatter'; import ReportFilter from './ReportFilter'; import ReportLayoutPage from './ReportLayoutPage'; -const ReportFilterForm = ({ onResult }) => { +const ReportFilterForm = ({ setItems }) => { - const handleSubmit = async (deviceId, from, to) => { - const query = new URLSearchParams({ deviceId, from, to }); - const response = await fetch(`/api/reports/route?${query.toString()}`, { headers: { Accept: 'application/json' } }); + const handleSubmit = async (deviceId, from, to, mail, headers) => { + const query = new URLSearchParams({ deviceId, from, to, mail }); + const response = await fetch(`/api/reports/route?${query.toString()}`, { headers }); if (response.ok) { - onResult(await response.json()); + const contentType = response.headers.get('content-type'); + if (contentType) { + if (contentType === 'application/json') { + setItems(await response.json()); + } else { + window.location.assign(window.URL.createObjectURL(await response.blob())); + } + } } } + return ; }; diff --git a/modern/src/reports/StopReportPage.js b/modern/src/reports/StopReportPage.js index ec4c199..77d3dfa 100644 --- a/modern/src/reports/StopReportPage.js +++ b/modern/src/reports/StopReportPage.js @@ -6,15 +6,23 @@ import ReportFilter from './ReportFilter'; import ReportLayoutPage from './ReportLayoutPage'; import { useAttributePreference } from '../common/preferences'; -const ReportFilterForm = ({ onResult }) => { +const ReportFilterForm = ({ setItems }) => { - const handleSubmit = async (deviceId, from, to) => { - const query = new URLSearchParams({ deviceId, from, to }); - const response = await fetch(`/api/reports/stops?${query.toString()}`, { headers: { Accept: 'application/json' } }); + const handleSubmit = async (deviceId, from, to, mail, headers) => { + const query = new URLSearchParams({ deviceId, from, to, mail }); + const response = await fetch(`/api/reports/stops?${query.toString()}`, { headers }); if (response.ok) { - onResult(await response.json()); + const contentType = response.headers.get('content-type'); + if (contentType) { + if (contentType === 'application/json') { + setItems(await response.json()); + } else { + window.location.assign(window.URL.createObjectURL(await response.blob())); + } + } } } + return ; }; diff --git a/modern/src/reports/SummaryReportPage.js b/modern/src/reports/SummaryReportPage.js index 4cb37aa..dcd3323 100644 --- a/modern/src/reports/SummaryReportPage.js +++ b/modern/src/reports/SummaryReportPage.js @@ -6,17 +6,25 @@ import ReportFilter from './ReportFilter'; import ReportLayoutPage from './ReportLayoutPage'; import { useAttributePreference } from '../common/preferences'; -const ReportFilterForm = ({ onResult }) => { +const ReportFilterForm = ({ setItems }) => { const [daily, setDaily] = useState(false); - const handleSubmit = async (deviceId, from, to) => { - const query = new URLSearchParams({ deviceId, from, to, daily }); - const response = await fetch(`/api/reports/summary?${query.toString()}`, { headers: { Accept: 'application/json' } }); + const handleSubmit = async (deviceId, from, to, mail, headers) => { + const query = new URLSearchParams({ deviceId, from, to, daily, mail }); + const response = await fetch(`/api/reports/summary?${query.toString()}`, { headers }); if (response.ok) { - onResult(await response.json()); + const contentType = response.headers.get('content-type'); + if (contentType) { + if (contentType === 'application/json') { + setItems(await response.json()); + } else { + window.location.assign(window.URL.createObjectURL(await response.blob())); + } + } } } + return ( { +const ReportFilterForm = ({ setItems }) => { - const handleSubmit = async (deviceId, from, to) => { - const query = new URLSearchParams({ deviceId, from, to }); - const response = await fetch(`/api/reports/trips?${query.toString()}`, { headers: { Accept: 'application/json' } }); + const handleSubmit = async (deviceId, from, to, mail, headers) => { + const query = new URLSearchParams({ deviceId, from, to, mail }); + const response = await fetch(`/api/reports/trips?${query.toString()}`, { headers }); if (response.ok) { - onResult(await response.json()); + const contentType = response.headers.get('content-type'); + if (contentType) { + if (contentType === 'application/json') { + setItems(await response.json()); + } else { + window.location.assign(window.URL.createObjectURL(await response.blob())); + } + } } } + return ; } -- cgit v1.2.3