aboutsummaryrefslogtreecommitdiff
path: root/modern/src/reports/EventReportPage.js
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-07-08 16:52:45 -0700
committerAnton Tananaev <anton@traccar.org>2022-07-08 16:52:45 -0700
commit9200986d36b88f549ed40606246d4ef08c93f18f (patch)
tree1e1a2265ad4115af4af8b382bea7a956a89d4156 /modern/src/reports/EventReportPage.js
parentcbb5a320802031c102818f8221d853f8181b54f3 (diff)
downloadtrackermap-web-9200986d36b88f549ed40606246d4ef08c93f18f.tar.gz
trackermap-web-9200986d36b88f549ed40606246d4ef08c93f18f.tar.bz2
trackermap-web-9200986d36b88f549ed40606246d4ef08c93f18f.zip
Improve reports handling
Diffstat (limited to 'modern/src/reports/EventReportPage.js')
-rw-r--r--modern/src/reports/EventReportPage.js40
1 files changed, 22 insertions, 18 deletions
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);
+ }
}
});