feat: add undo for bulk personal actions
CI / test (push) Successful in 35s

This commit is contained in:
strsv committed 2026-11-06 18:24:13 +03:00
1 parent 94cc60b393
commit 1bd4ca8c53
10 files changed
+143 -22

No files matched your search

+47 -3
View File
@@ -517,6 +517,8 @@ class Handler(BaseHTTPRequestHandler):
self._json({"queued": len(ids)})
elif path == "/api/personal/bulk":
self._update_personal_bulk(self._body())
elif path == "/api/personal/restore":
self._restore_personal(self._body())
elif path.startswith("/api/personal/"):
appid = int(path.rsplit("/", 1)[1])
self._update_personal(appid, self._body())
@@ -1074,11 +1076,17 @@ class Handler(BaseHTTPRequestHandler):
con = connect()
try:
placeholders = ",".join("?" * len(appids))
existing = [r["appid"] for r in con.execute(
f"SELECT appid FROM games WHERE appid IN ({placeholders})", appids)]
rows = con.execute(
f"""SELECT g.appid, COALESCE(pe.status, 'backlog') AS status,
COALESCE(pe.favorite, 0) AS favorite
FROM games g LEFT JOIN personal pe ON pe.appid = g.appid
WHERE g.appid IN ({placeholders})""", appids).fetchall()
existing = [r["appid"] for r in rows]
if not existing:
self._error(404, "unknown appids")
return
previous = [{"appid": r["appid"], "status": r["status"],
"favorite": r["favorite"]} for r in rows]
now = datetime.now(timezone.utc).isoformat(timespec="seconds")
con.executemany(
"INSERT OR IGNORE INTO personal (appid, status, updated_at) VALUES (?, 'backlog', ?)",
@@ -1090,7 +1098,43 @@ class Handler(BaseHTTPRequestHandler):
f"WHERE appid IN ({','.join('?' * len(existing))})",
[*vals, *existing])
con.commit()
self._json({"updated": len(existing)})
self._json({"updated": len(existing), "previous": previous})
finally:
con.close()
def _restore_personal(self, body):
items = []
for item in body.get("items") or []:
try:
appid = int(item.get("appid"))
except (AttributeError, TypeError, ValueError):
continue
if item.get("status") not in STATUSES:
continue
items.append((appid, item["status"], 1 if item.get("favorite") else 0))
if not items:
self._error(400, "no items")
return
con = connect()
try:
now = datetime.now(timezone.utc).isoformat(timespec="seconds")
updated = 0
for appid, status, favorite in items:
if not con.execute("SELECT 1 FROM games WHERE appid = ?", (appid,)).fetchone():
continue
con.execute(
"INSERT OR IGNORE INTO personal (appid, status, updated_at) "
"VALUES (?, 'backlog', ?)", (appid, now))
con.execute(
"UPDATE personal SET status = ?, favorite = ?, updated_at = ? "
"WHERE appid = ?", (status, favorite, now, appid))
updated += 1
con.commit()
if not updated:
self._error(404, "unknown appids")
return
self._json({"updated": updated})
finally:
con.close()
+36 -4
View File
@@ -366,7 +366,7 @@ async function fillDescriptions() {
}
}
function toast(message) {
function toast(message, action = null) {
let el = $("#toast");
if (!el) {
el = document.createElement("div");
@@ -374,10 +374,24 @@ function toast(message) {
el.className = "toast";
document.body.appendChild(el);
}
el.textContent = message;
el.innerHTML = "";
const text = document.createElement("span");
text.textContent = message;
el.appendChild(text);
if (action) {
const button = document.createElement("button");
button.type = "button";
button.className = "toast-action";
button.textContent = action.label;
button.addEventListener("click", () => {
el.classList.remove("show");
action.onClick();
});
el.appendChild(button);
}
el.classList.add("show");
clearTimeout(el._timer);
el._timer = setTimeout(() => el.classList.remove("show"), 2600);
el._timer = setTimeout(() => el.classList.remove("show"), action ? 8000 : 2600);
}
/* ---------- meta ---------- */
@@ -556,10 +570,28 @@ async function bulkUpdate(fields) {
method: "POST",
body: JSON.stringify({ appids, ...fields }),
});
toast(`Обновлено игр: ${result.updated}`);
clearSelection();
await loadGames(true);
await loadMeta();
const previous = result.previous || [];
toast(`Обновлено игр: ${result.updated}`, previous.length ? {
label: "отменить",
onClick: () => restoreBulk(previous),
} : null);
} catch (err) {
toast(err.message);
}
}
async function restoreBulk(items) {
try {
await api("/api/personal/restore", {
method: "POST",
body: JSON.stringify({ items }),
});
toast("Действие отменено");
await loadGames(true);
await loadMeta();
} catch (err) {
toast(err.message);
}
+12
View File
@@ -834,6 +834,9 @@ html[data-theme="light"] .select-box {
left: 50%;
bottom: 26px;
transform: translateX(-50%) translateY(20px);
display: flex;
align-items: center;
gap: 12px;
padding: 10px 18px;
border-radius: 12px;
background: rgba(20, 26, 38, .95);
@@ -846,6 +849,15 @@ html[data-theme="light"] .select-box {
z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast-action {
border: 0;
background: none;
padding: 0;
color: var(--accent);
font-weight: 650;
white-space: nowrap;
}
.toast-action:hover { text-decoration: underline; }
.theme-toggle .icon-moon { display: none; }
html[data-theme="light"] .theme-toggle .icon-sun { display: none; }