From 9200986d36b88f549ed40606246d4ef08c93f18f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 8 Jul 2022 16:52:45 -0700 Subject: Improve reports handling --- modern/src/reports/ChartReportPage.js | 8 +++-- modern/src/reports/EventReportPage.js | 40 +++++++++++++------------ modern/src/reports/RouteReportPage.js | 38 +++++++++++++----------- modern/src/reports/StatisticsPage.js | 2 +- modern/src/reports/StopReportPage.js | 38 +++++++++++++----------- modern/src/reports/SummaryReportPage.js | 42 +++++++++++++++------------ modern/src/reports/TripReportPage.js | 38 +++++++++++++----------- modern/src/reports/components/ReportFilter.js | 12 ++++---- 8 files changed, 119 insertions(+), 99 deletions(-) diff --git a/modern/src/reports/ChartReportPage.js b/modern/src/reports/ChartReportPage.js index 00edf7b6..4605a5b9 100644 --- a/modern/src/reports/ChartReportPage.js +++ b/modern/src/reports/ChartReportPage.js @@ -37,9 +37,11 @@ const ChartReportPage = () => { const maxValue = Math.max(...values); const valueRange = maxValue - minValue; - const handleSubmit = useCatch(async ({ deviceId, from, to, mail, headers }) => { - const query = new URLSearchParams({ deviceId, from, to, mail }); - const response = await fetch(`/api/reports/route?${query.toString()}`, { headers }); + const handleSubmit = useCatch(async ({ deviceId, from, to }) => { + const query = new URLSearchParams({ deviceId, from, to }); + const response = await fetch(`/api/reports/route?${query.toString()}`, { + headers: { Accept: 'application/json' }, + }); if (response.ok) { const positions = await response.json(); const formattedPositions = positions.map((position) => { diff --git a/modern/src/reports/EventReportPage.js b/modern/src/reports/EventReportPage.js index 8416af71..1362934b 100644 --- a/modern/src/reports/EventReportPage.js +++ b/modern/src/reports/EventReportPage.js @@ -56,26 +56,30 @@ const EventReportPage = () => { const [items, setItems] = useState([]); const [loading, setLoading] = useState(false); - const handleSubmit = useCatch(async ({ deviceId, from, to, mail, headers }) => { - setLoading(true); - try { - 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) { - 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())); - } - } - } else { + const handleSubmit = useCatch(async ({ deviceId, from, to, type }) => { + const query = new URLSearchParams({ deviceId, from, to }); + eventTypes.forEach((it) => query.append('type', it)); + if (type === 'export') { + window.location.assign(`/api/reports/events/xlsx?${query.toString()}`); + } else if (type === 'mail') { + const response = await fetch(`/api/reports/events/mail?${query.toString()}`); + if (!response.ok) { throw Error(await response.text()); } - } finally { - setLoading(false); + } else { + setLoading(true); + try { + const response = await fetch(`/api/reports/events?${query.toString()}`, { + headers: { Accept: 'application/json' }, + }); + if (response.ok) { + setItems(await response.json()); + } else { + throw Error(await response.text()); + } + } finally { + setLoading(false); + } } }); diff --git a/modern/src/reports/RouteReportPage.js b/modern/src/reports/RouteReportPage.js index 1c24a15b..c901cf66 100644 --- a/modern/src/reports/RouteReportPage.js +++ b/modern/src/reports/RouteReportPage.js @@ -30,25 +30,29 @@ const RouteReportPage = () => { const [loading, setLoading] = useState(false); const [selectedItem, setSelectedItem] = useState(null); - const handleSubmit = useCatch(async ({ deviceId, from, to, mail, headers }) => { - setLoading(true); - try { - const query = new URLSearchParams({ deviceId, from, to, mail }); - const response = await fetch(`/api/reports/route?${query.toString()}`, { headers }); - if (response.ok) { - 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())); - } - } - } else { + const handleSubmit = useCatch(async ({ deviceId, from, to, type }) => { + const query = new URLSearchParams({ deviceId, from, to }); + if (type === 'export') { + window.location.assign(`/api/reports/route/xlsx?${query.toString()}`); + } else if (type === 'mail') { + const response = await fetch(`/api/reports/route/mail?${query.toString()}`); + if (!response.ok) { throw Error(await response.text()); } - } finally { - setLoading(false); + } else { + setLoading(true); + try { + const response = await fetch(`/api/reports/route?${query.toString()}`, { + headers: { Accept: 'application/json' }, + }); + if (response.ok) { + setItems(await response.json()); + } else { + throw Error(await response.text()); + } + } finally { + setLoading(false); + } } }); diff --git a/modern/src/reports/StatisticsPage.js b/modern/src/reports/StatisticsPage.js index eb6130c7..254a912d 100644 --- a/modern/src/reports/StatisticsPage.js +++ b/modern/src/reports/StatisticsPage.js @@ -39,7 +39,7 @@ const StatisticsPage = () => { setLoading(true); try { const query = new URLSearchParams({ from, to }); - const response = await fetch(`/api/statistics?${query.toString()}`, { Accept: 'application/json' }); + const response = await fetch(`/api/statistics?${query.toString()}`); if (response.ok) { setItems(await response.json()); } else { diff --git a/modern/src/reports/StopReportPage.js b/modern/src/reports/StopReportPage.js index bacbe928..5d6e837d 100644 --- a/modern/src/reports/StopReportPage.js +++ b/modern/src/reports/StopReportPage.js @@ -46,25 +46,29 @@ const StopReportPage = () => { const [loading, setLoading] = useState(false); const [selectedItem, setSelectedItem] = useState(null); - const handleSubmit = useCatch(async ({ deviceId, from, to, mail, headers }) => { - setLoading(true); - try { - const query = new URLSearchParams({ deviceId, from, to, mail }); - const response = await fetch(`/api/reports/stops?${query.toString()}`, { headers }); - if (response.ok) { - 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())); - } - } - } else { + const handleSubmit = useCatch(async ({ deviceId, from, to, type }) => { + const query = new URLSearchParams({ deviceId, from, to }); + if (type === 'export') { + window.location.assign(`/api/reports/stops/xlsx?${query.toString()}`); + } else if (type === 'mail') { + const response = await fetch(`/api/reports/stops/mail?${query.toString()}`); + if (!response.ok) { throw Error(await response.text()); } - } finally { - setLoading(false); + } else { + setLoading(true); + try { + const response = await fetch(`/api/reports/stops?${query.toString()}`, { + headers: { Accept: 'application/json' }, + }); + if (response.ok) { + setItems(await response.json()); + } else { + throw Error(await response.text()); + } + } finally { + setLoading(false); + } } }); diff --git a/modern/src/reports/SummaryReportPage.js b/modern/src/reports/SummaryReportPage.js index f6ec0691..b8ec9283 100644 --- a/modern/src/reports/SummaryReportPage.js +++ b/modern/src/reports/SummaryReportPage.js @@ -45,27 +45,31 @@ const SummaryReportPage = () => { const [items, setItems] = useState([]); const [loading, setLoading] = useState(false); - const handleSubmit = useCatch(async ({ deviceIds, groupIds, from, to, mail, headers }) => { - setLoading(true); - try { - const query = new URLSearchParams({ from, to, daily, mail }); - deviceIds.forEach((deviceId) => query.append('deviceId', deviceId)); - groupIds.forEach((groupId) => query.append('groupId', groupId)); - const response = await fetch(`/api/reports/summary?${query.toString()}`, { headers }); - if (response.ok) { - 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())); - } - } - } else { + const handleSubmit = useCatch(async ({ deviceIds, groupIds, from, to, type }) => { + const query = new URLSearchParams({ from, to, daily }); + deviceIds.forEach((deviceId) => query.append('deviceId', deviceId)); + groupIds.forEach((groupId) => query.append('groupId', groupId)); + if (type === 'export') { + window.location.assign(`/api/reports/summary/xlsx?${query.toString()}`); + } else if (type === 'mail') { + const response = await fetch(`/api/reports/summary/mail?${query.toString()}`); + if (!response.ok) { throw Error(await response.text()); } - } finally { - setLoading(false); + } else { + setLoading(true); + try { + const response = await fetch(`/api/reports/summary?${query.toString()}`, { + headers: { Accept: 'application/json' }, + }); + if (response.ok) { + setItems(await response.json()); + } else { + throw Error(await response.text()); + } + } finally { + setLoading(false); + } } }); diff --git a/modern/src/reports/TripReportPage.js b/modern/src/reports/TripReportPage.js index 6e79128f..23455404 100644 --- a/modern/src/reports/TripReportPage.js +++ b/modern/src/reports/TripReportPage.js @@ -87,25 +87,29 @@ const TripReportPage = () => { } }, [selectedItem]); - const handleSubmit = useCatch(async ({ deviceId, from, to, mail, headers }) => { - setLoading(true); - try { - const query = new URLSearchParams({ deviceId, from, to, mail }); - const response = await fetch(`/api/reports/trips?${query.toString()}`, { headers }); - if (response.ok) { - 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())); - } - } - } else { + const handleSubmit = useCatch(async ({ deviceId, from, to, type }) => { + const query = new URLSearchParams({ deviceId, from, to }); + if (type === 'export') { + window.location.assign(`/api/reports/trips/xlsx?${query.toString()}`); + } else if (type === 'mail') { + const response = await fetch(`/api/reports/trips/mail?${query.toString()}`); + if (!response.ok) { throw Error(await response.text()); } - } finally { - setLoading(false); + } else { + setLoading(true); + try { + const response = await fetch(`/api/reports/trips?${query.toString()}`, { + headers: { Accept: 'application/json' }, + }); + if (response.ok) { + setItems(await response.json()); + } else { + throw Error(await response.text()); + } + } finally { + setLoading(false); + } } }); diff --git a/modern/src/reports/components/ReportFilter.js b/modern/src/reports/components/ReportFilter.js index 100dab90..07c406d6 100644 --- a/modern/src/reports/components/ReportFilter.js +++ b/modern/src/reports/components/ReportFilter.js @@ -24,7 +24,7 @@ const ReportFilter = ({ children, handleSubmit, showOnly, ignoreDevice, multiDev const disabled = !ignoreDevice && !deviceId && !deviceIds.length && !groupIds.length; - const handleClick = (mail, json) => { + const handleClick = (type) => { let selectedFrom; let selectedTo; switch (period) { @@ -58,15 +58,13 @@ const ReportFilter = ({ children, handleSubmit, showOnly, ignoreDevice, multiDev break; } - const accept = json ? 'application/json' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; handleSubmit({ deviceId, deviceIds, groupIds, from: selectedFrom.toISOString(), to: selectedTo.toISOString(), - mail, - headers: { Accept: accept }, + type, }); }; @@ -145,7 +143,7 @@ const ReportFilter = ({ children, handleSubmit, showOnly, ignoreDevice, multiDev {children}
{!showOnly && (