-
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added missing check on existing element
- Loading branch information
Showing
1 changed file
with
37 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
}); | ||
}; | ||
} | ||
}; |