[CHANGE] Switch to framework fetch functions

This commit is contained in:
Peter Pfeufer 2025-08-08 19:29:55 +02:00
parent 18e9453fed
commit 0028310aa5
No known key found for this signature in database
3 changed files with 31 additions and 62 deletions

View File

@ -1,4 +1,4 @@
/* global notificationUpdateSettings */ /* global notificationUpdateSettings, fetchGet */
/** /**
* This script refreshed the notification icon in the top menu * This script refreshed the notification icon in the top menu
@ -19,22 +19,9 @@ $(() => {
* Update the notification icon in the top menu * Update the notification icon in the top menu
*/ */
const updateNotificationIcon = () => { const updateNotificationIcon = () => {
fetch(userNotificationCountViewUrl) fetchGet({url: userNotificationCountViewUrl})
.then((response) => { .then((data) => {
if (response.ok) { elementNotificationIcon.toggleClass('text-danger', data.unread_count > 0);
return response.json();
}
throw new Error('Something went wrong');
})
.then((responseJson) => {
const unreadCount = responseJson.unread_count;
if (unreadCount > 0) {
elementNotificationIcon.addClass('text-danger');
} else {
elementNotificationIcon.removeClass('text-danger');
}
}) })
.catch((error) => { .catch((error) => {
console.log(`Failed to load HTMl to render notifications item. Error: ${error.message}`); console.log(`Failed to load HTMl to render notifications item. Error: ${error.message}`);

View File

@ -8,30 +8,22 @@
</div> </div>
<script> <script>
const elemCard = document.getElementById("esi-alert"); const elemCard = document.getElementById('esi-alert');
const elemMessage = document.getElementById("esi-data"); const elemMessage = document.getElementById('esi-data');
const elemCode = document.getElementById("esi-code"); const elemCode = document.getElementById('esi-code');
fetch('{% url "authentication:esi_check" %}') fetchGet({url: '{% url "authentication:esi_check" %}'})
.then((response) => { .then((data) => {
if (response.ok) { console.log('ESI Check: ', JSON.stringify(data, null, 2));
return response.json();
}
throw new Error("Something went wrong");
})
.then((responseJson) => {
console.log("ESI Check: ", JSON.stringify(responseJson, null, 2));
const status = responseJson.status; if (data.status !== 200) {
if (status !== 200) { elemCode.textContent = data.status;
elemCode.textContent = status elemMessage.textContent = data.data.error;
elemMessage.textContent = responseJson.data.error;
new bootstrap.Collapse(elemCard, { new bootstrap.Collapse(elemCard, {toggle: true});
toggle: true
})
} }
}) })
.catch((error) => { .catch((error) => {
console.log(error); console.error('Error fetching ESI check:', error);
}); });
</script> </script>

View File

@ -133,34 +133,24 @@
<script> <script>
const elemRunning = document.getElementById("task-counts"); const elemRunning = document.getElementById('task-counts');
const elemQueued = document.getElementById("queued-tasks-count"); const elemQueued = document.getElementById('queued-tasks-count');
fetch('{% url "authentication:task_counts" %}') fetchGet({url: '{% url "authentication:task_counts" %}'})
.then((response) => { .then((data) => {
if (response.ok) { const running = data.tasks_running;
return response.json(); const queued = data.tasks_queued;
}
throw new Error("Something went wrong");
})
.then((responseJson) => {
const running = responseJson.tasks_running;
if (running == null) {
elemRunning.textContent = "N/A";
} else {
elemRunning.textContent = running.toLocaleString();
}
const queued = responseJson.tasks_queued; const updateTaskCount = (element, value) => {
if (queued == null) { element.textContent = value == null ? 'N/A' : value.toLocaleString();
elemQueued.textContent = "N/A"; };
} else {
elemQueued.textContent = queued.toLocaleString(); updateTaskCount(elemRunning, running);
} updateTaskCount(elemQueued, queued);
}) })
.catch((error) => { .catch((error) => {
console.log(error); console.error('Error fetching task queue:', error);
elemRunning.textContent = "ERROR";
elemQueued.textContent = "ERROR"; [elemRunning, elemQueued].forEach(elem => elem.textContent = 'ERROR');
}); });
</script> </script>