Fix JS fetch credentials and CSV export from web UI

This commit is contained in:
2026-07-16 18:32:37 +07:00
parent 677f6028d9
commit 8e62eaf3f3
6 changed files with 41 additions and 9 deletions
+31 -4
View File
@@ -6,9 +6,9 @@
<div class="d-flex justify-content-between align-items-center mb-4">
<h2>Компьютеры</h2>
<div class="d-flex gap-2">
<a href="/api/computers/export/csv?{{ request.query_params }}" class="btn btn-outline-success btn-sm" target="_blank">
<button type="button" class="btn btn-outline-success btn-sm" id="export-csv-btn">
<i class="bi bi-download"></i> Экспорт CSV
</a>
</button>
<a href="/notifications" class="btn btn-outline-info btn-sm">
<i class="bi bi-bell"></i> Уведомления {% if unread_count %}<span class="badge bg-danger">{{ unread_count }}</span>{% endif %}
</a>
@@ -107,18 +107,45 @@ function currentQuery() {
async function deleteComputer(id, hostname) {
if (!confirm(`Удалить ПК ${hostname}?`)) return;
const res = await fetch(`/api/computers/${id}`, {method: 'DELETE'});
const res = await fetch(`/api/computers/${id}`, {method: 'DELETE', credentials: 'include'});
if (res.ok) location.reload();
else if (res.status === 401) location.href = '/login';
else alert('Ошибка удаления');
}
async function exportCsv() {
const res = await fetch('/api/computers/export/csv?' + currentQuery(), {credentials: 'include'});
if (!res.ok) {
if (res.status === 401) location.href = '/login';
else alert('Ошибка экспорта');
return;
}
const blob = await res.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
const disposition = res.headers.get('content-disposition');
let filename = 'computers.csv';
if (disposition) {
const match = disposition.match(/filename="?([^";]+)"?/);
if (match) filename = match[1];
}
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
document.getElementById('export-csv-btn').addEventListener('click', exportCsv);
document.querySelectorAll('.delete-pc').forEach(btn => {
btn.addEventListener('click', () => deleteComputer(btn.dataset.id, btn.dataset.hostname));
});
async function loadDashboard() {
try {
const res = await fetch('/api/computers?' + currentQuery());
const res = await fetch('/api/computers?' + currentQuery(), {credentials: 'include'});
if (!res.ok) return location.reload();
const data = await res.json();
const tbody = document.querySelector('#computers-table tbody');