Skip to content

Commit 7ee6e39

Browse files
committed
fix: added missing check on existing element
1 parent 8827b52 commit 7ee6e39

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

phpmyfaq/admin/assets/src/sessions.js

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
* obtain one at https://mozilla.org/MPL/2.0/.
77
*
88
* @package phpMyFAQ
9-
* @author Thorsten Rinne <[email protected]>
109
* @author Jan Harms <[email protected]>
11-
* @copyright 2022-2024 phpMyFAQ Team
10+
* @copyright 2024 phpMyFAQ Team
1211
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
1312
* @link https://www.phpmyfaq.de
1413
* @since 2024-01-14
@@ -17,42 +16,44 @@
1716
import { pushErrorNotification } from './utils';
1817

1918
export const handleSessions = () => {
20-
const firstHour = document.getElementById('firstHour');
21-
const lastHour = document.getElementById('lastHour');
22-
const exportSessions = document.getElementById('exportSessions');
23-
const csrf = document.getElementById('csrf');
19+
const firstHour = document.getElementById('firstHour');
20+
const lastHour = document.getElementById('lastHour');
21+
const exportSessions = document.getElementById('exportSessions');
22+
const csrf = document.getElementById('csrf');
2423

24+
if (exportSessions) {
2525
exportSessions.addEventListener('click', async (event) => {
26-
event.preventDefault();
26+
event.preventDefault();
2727

28-
try {
29-
const response = await fetch('./api/session/export', {
30-
method: 'POST',
31-
headers: {
32-
Accept: 'application/json, text/plain, */*',
33-
'Content-Type': 'application/json'
34-
},
35-
body: JSON.stringify({
36-
csrf: csrf.value,
37-
firstHour: firstHour.value,
38-
lastHour: lastHour.value
39-
})
40-
});
41-
if (response.ok) {
42-
const blob = await response.blob();
43-
const url = URL.createObjectURL(blob);
44-
const link = document.createElement('a');
45-
link.href = url;
46-
link.download = 'sessions_' + firstHour.value + '--' + lastHour.value + '.csv';
47-
document.body.appendChild(link);
48-
link.click();
49-
URL.revokeObjectURL(url);
50-
} else {
51-
const jsonResponse = response.json();
52-
pushErrorNotification(jsonResponse.error);
53-
}
54-
} catch (error) {
55-
console.error(error.message);
28+
try {
29+
const response = await fetch('./api/session/export', {
30+
method: 'POST',
31+
headers: {
32+
Accept: 'application/json, text/plain, */*',
33+
'Content-Type': 'application/json',
34+
},
35+
body: JSON.stringify({
36+
csrf: csrf.value,
37+
firstHour: firstHour.value,
38+
lastHour: lastHour.value,
39+
}),
40+
});
41+
if (response.ok) {
42+
const blob = await response.blob();
43+
const url = URL.createObjectURL(blob);
44+
const link = document.createElement('a');
45+
link.href = url;
46+
link.download = 'sessions_' + firstHour.value + '--' + lastHour.value + '.csv';
47+
document.body.appendChild(link);
48+
link.click();
49+
URL.revokeObjectURL(url);
50+
} else {
51+
const jsonResponse = response.json();
52+
pushErrorNotification(jsonResponse.error);
5653
}
54+
} catch (error) {
55+
console.error(error.message);
56+
}
5757
});
58-
};
58+
}
59+
};

0 commit comments

Comments
 (0)