admin: unify response error extraction into errText helper
removes a double-body-read in the delete handlers and makes the four alert sites consistent.
This commit is contained in:
parent
ac6a50197e
commit
df513fda68
1 changed files with 13 additions and 4 deletions
|
|
@ -6,6 +6,15 @@ async function j<T = any>(res: Response): Promise<T> {
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Best-effort error message from a failed response ({ error } JSON or status text).
|
||||||
|
async function errText(res: Response): Promise<string> {
|
||||||
|
try {
|
||||||
|
return (await res.json()).error ?? res.statusText;
|
||||||
|
} catch {
|
||||||
|
return res.statusText;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
interface Drink { id: number; name: string; price_cents: number; archived: number }
|
interface Drink { id: number; name: string; price_cents: number; archived: number }
|
||||||
interface BarRow { id: number; name: string; pfand_cents: number; drink_ids: number[] }
|
interface BarRow { id: number; name: string; pfand_cents: number; drink_ids: number[] }
|
||||||
interface Totals { bar_id: number; bar_name: string; tx_count: number; paid_cents: number; crew_count: number; pfand_returns: number }
|
interface Totals { bar_id: number; bar_name: string; tx_count: number; paid_cents: number; crew_count: number; pfand_returns: number }
|
||||||
|
|
@ -189,7 +198,7 @@ function Drinks() {
|
||||||
async function del(d: Drink) {
|
async function del(d: Drink) {
|
||||||
if (!confirm(`Getränk „${d.name}“ wirklich löschen?`)) return;
|
if (!confirm(`Getränk „${d.name}“ wirklich löschen?`)) return;
|
||||||
const res = await fetch(`/admin/api/drinks/${d.id}`, { method: 'DELETE' });
|
const res = await fetch(`/admin/api/drinks/${d.id}`, { method: 'DELETE' });
|
||||||
if (!res.ok) { alert(`Fehler: ${(await res.json().catch(() => null))?.error ?? await res.text()}`); return; }
|
if (!res.ok) { alert(`Fehler: ${await errText(res)}`); return; }
|
||||||
reload();
|
reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -337,14 +346,14 @@ function Bars() {
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
});
|
});
|
||||||
if (!res.ok) alert(`Fehler: ${await res.text()}`);
|
if (!res.ok) alert(`Fehler: ${await errText(res)}`);
|
||||||
reload();
|
reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function delBar(b: BarRow) {
|
async function delBar(b: BarRow) {
|
||||||
if (!confirm(`Tresen „${b.name}“ wirklich löschen?`)) return;
|
if (!confirm(`Tresen „${b.name}“ wirklich löschen?`)) return;
|
||||||
const res = await fetch(`/admin/api/bars/${b.id}`, { method: 'DELETE' });
|
const res = await fetch(`/admin/api/bars/${b.id}`, { method: 'DELETE' });
|
||||||
if (!res.ok) { alert(`Fehler: ${(await res.json().catch(() => null))?.error ?? await res.text()}`); return; }
|
if (!res.ok) { alert(`Fehler: ${await errText(res)}`); return; }
|
||||||
reload();
|
reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -357,7 +366,7 @@ function Bars() {
|
||||||
body: JSON.stringify({ name, pfand_cents: 200 }),
|
body: JSON.stringify({ name, pfand_cents: 200 }),
|
||||||
});
|
});
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
alert(`Fehler: ${await res.text()}`);
|
alert(`Fehler: ${await errText(res)}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setNewName('');
|
setNewName('');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue