Skip to content

Commit

Permalink
fix: added missing check on existing element
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jan 17, 2024
1 parent 8827b52 commit 7ee6e39
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions phpmyfaq/admin/assets/src/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @author Jan Harms <[email protected]>
* @copyright 2022-2024 phpMyFAQ Team
* @copyright 2024 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-01-14
Expand All @@ -17,42 +16,44 @@
import { pushErrorNotification } from './utils';

export const handleSessions = () => {
const firstHour = document.getElementById('firstHour');
const lastHour = document.getElementById('lastHour');
const exportSessions = document.getElementById('exportSessions');
const csrf = document.getElementById('csrf');
const firstHour = document.getElementById('firstHour');
const lastHour = document.getElementById('lastHour');
const exportSessions = document.getElementById('exportSessions');
const csrf = document.getElementById('csrf');

if (exportSessions) {
exportSessions.addEventListener('click', async (event) => {
event.preventDefault();
event.preventDefault();

try {
const response = await fetch('./api/session/export', {
method: 'POST',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({
csrf: csrf.value,
firstHour: firstHour.value,
lastHour: lastHour.value
})
});
if (response.ok) {
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'sessions_' + firstHour.value + '--' + lastHour.value + '.csv';
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(url);
} else {
const jsonResponse = response.json();
pushErrorNotification(jsonResponse.error);
}
} catch (error) {
console.error(error.message);
try {
const response = await fetch('./api/session/export', {
method: 'POST',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
body: JSON.stringify({
csrf: csrf.value,
firstHour: firstHour.value,
lastHour: lastHour.value,
}),
});
if (response.ok) {
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'sessions_' + firstHour.value + '--' + lastHour.value + '.csv';
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(url);
} else {
const jsonResponse = response.json();
pushErrorNotification(jsonResponse.error);
}
} catch (error) {
console.error(error.message);
}
});
};
}
};

0 comments on commit 7ee6e39

Please sign in to comment.