fix: sort admin reports by newest date

This commit is contained in:
nyan404 committed 2026-10-10 22:08:51 +03:00
1 parent 4bf520fb42
commit a15c22b4a2
5 files changed
+22 -4

No files matched your search

+2 -2
View File
@@ -144,7 +144,7 @@
<button class="tab" type="button" data-category="training" aria-selected="false">ПУ / уроки</button>
</nav>
<section class="controls" aria-label="Фильтры истории">
<label class="control-group">Сортировать по<select id="sort"><option value="cat">Коту</option><option value="kitten">Котёнку</option><option value="photo">Фото</option><option value="date">Дате</option><option value="message">Сообщению</option><option value="author">Автору</option><option value="id">ID</option></select></label>
<label class="control-group">Сортировать по<select id="sort"><option value="date" selected>Дате</option><option value="cat">Коту</option><option value="kitten">Котёнку</option><option value="photo">Фото</option><option value="message">Сообщению</option><option value="author">Автору</option><option value="id">ID</option></select></label>
<label class="control-group">Порядок<select id="direction"><option value="asc">По возрастанию</option><option value="desc" selected>По убыванию</option></select></label>
<label class="control-group">Кот<select id="catFilter"><option value="">Все коты</option></select></label>
<label class="control-group">Котёнок<select id="kittenFilter"><option value="">Все котята</option></select></label>
@@ -236,7 +236,7 @@
function pointsCell(item) { const cell = document.createElement('td'); cell.className = 'points-cell'; const result = item.result || {}; if (item.mode !== 'inspection' || result.points === undefined) { cell.textContent = '—'; return cell; } const details = document.createElement('div'); details.className = 'points-details'; const grouped = new Map(); (result.resource_points || []).forEach(resource => { const name = resource.name || resource[0] || 'Ресурс'; const amount = Number(resource.amount ?? resource[1] ?? 0); const points = Number(resource.points ?? resource[2] ?? 0); const key = name.trim().replace(/\s+/g, ' ').toLocaleLowerCase('ru-RU'); const current = grouped.get(key) || {name, amount: 0, points: 0}; current.amount += amount; current.points += points; grouped.set(key, current); }); const resourceTotal = [...grouped.values()].reduce((sum, resource) => sum + resource.points, 0); const resourceHeader = document.createElement('strong'); resourceHeader.textContent = `Итого по ресурсам: ${resourceTotal}`; details.append(resourceHeader); grouped.forEach(resource => { const perItem = resource.amount ? resource.points / resource.amount : 0; const line = document.createElement('span'); line.textContent = `${resource.name} ×${resource.amount}: ${resource.points} (${perItem}Б)`; details.append(line); }); if (result.inspection_points !== undefined) { const base = document.createElement('span'); base.textContent = `Баллы осмотра: ${result.inspection_points}`; details.append(base); } const total = document.createElement('strong'); total.textContent = `Всего: ${result.points}`; details.append(total); cell.append(details); return cell; }
// Re-renders the report table, including empty and filtered states.
function render() { syncReportsExport(); rows.replaceChildren(); const visible = filteredItems(); recordCount.textContent = `${visible.length} записей`; if (!visible.length) { const row = document.createElement('tr'); const cell = document.createElement('td'); cell.className = 'empty'; cell.colSpan = 9; cell.textContent = 'По выбранным условиям записей нет.'; row.append(cell); rows.append(row); return; } visible.forEach(item => { const input = item.input || {}; const row = document.createElement('tr'); row.append(personCell(personData(input))); const kittenCell = document.createElement('td'); kittenCell.className = 'person'; kittenEntries(input).forEach(entry => { const strong = document.createElement('strong'); strong.textContent = entry.name; const small = document.createElement('span'); small.textContent = entry.id ? `[${entry.id}]` : 'ID не указан'; kittenCell.append(strong, small); }); if (!kittenCell.childElementCount) kittenCell.textContent = '—'; row.append(kittenCell, photoCell(input.attachments), (() => { const cell = document.createElement('td'); cell.className = 'id-cell'; cell.textContent = displayDate(item.created_at); return cell; })(), pointsCell(item)); const message = document.createElement('td'); const pre = document.createElement('pre'); pre.className = 'message'; pre.textContent = item.message; message.append(pre); row.append(message, (() => { const cell = document.createElement('td'); cell.className = 'id-cell'; cell.textContent = item.actor_id || '—'; return cell; })(), (() => { const cell = document.createElement('td'); cell.className = 'id-cell'; cell.textContent = item.id; return cell; })()); const actions = document.createElement('td'); const remove = document.createElement('button'); remove.className = 'delete'; remove.type = 'button'; remove.textContent = 'Удалить'; remove.addEventListener('click', async () => { if (!window.confirm(`Удалить запись №${item.id}?`)) return; remove.disabled = true; try { const response = await fetch(`/api/admin/calculations/${encodeURIComponent(item.id)}`, {method: 'DELETE'}); const result = await response.json(); if (!response.ok) throw new Error(result.error || 'Не удалось удалить запись'); items = items.filter(entry => entry.id !== item.id); fillFilters(); render(); } catch (err) { error.textContent = err.message; remove.disabled = false; } }); actions.append(remove); row.append(actions); rows.append(row); }); }
async function load() { error.textContent = ''; loadButton.disabled = true; try { const query = queryFor(reportPeriod, reportDateFrom, reportDateTo, {sort: 'escort_name', direction: 'asc', limit: '500', category: activeCategory, cat: catFilter.value, kitten: kittenFilter.value}); const response = await fetch(`/api/admin/calculations?${query}`); const data = await response.json(); if (!response.ok) throw new Error(data.error || 'Не удалось загрузить историю'); items = data.items || []; fillFilters(); render(); } catch (err) { error.textContent = err.message; recordCount.textContent = 'История не загружена'; } finally { loadButton.disabled = false; } }
async function load() { error.textContent = ''; loadButton.disabled = true; try { const query = queryFor(reportPeriod, reportDateFrom, reportDateTo, {sort: 'created_at', direction: 'desc', limit: '500', category: activeCategory, cat: catFilter.value, kitten: kittenFilter.value}); const response = await fetch(`/api/admin/calculations?${query}`); const data = await response.json(); if (!response.ok) throw new Error(data.error || 'Не удалось загрузить историю'); items = data.items || []; fillFilters(); render(); } catch (err) { error.textContent = err.message; recordCount.textContent = 'История не загружена'; } finally { loadButton.disabled = false; } }
// Loads report history from the admin API and refreshes filters/table.
const applicationLabels = {feather_donation: 'Пожертвование перьев', kitten: 'Стать бельчонком', flying_squirrel: 'Стать белкой-летягой', curator: 'Стать куратором'};
// Extracts display fields from one application for the detail panel.