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/EventReportPage.js | 40 +++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'modern/src/reports/EventReportPage.js') 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); + } } }); -- cgit v1.2.3