31 lines
1.7 KiB
JavaScript
31 lines
1.7 KiB
JavaScript
const LANGS = ["ru", "kk", "en"];
|
|
const saved = () => { try { return localStorage.getItem("lang"); } catch { return null; } };
|
|
const lang = LANGS.includes(saved()) ? saved() : "ru";
|
|
const t = await (await fetch(`i18n/${lang}.json`)).json();
|
|
const fmt = (s, o) => s.replace(/\{(\w+)\}/g, (_, k) => o[k]);
|
|
const date = (iso) => (iso ? iso.split("-").reverse().join(".") : "");
|
|
document.documentElement.lang = lang;
|
|
document.title = t.baseTitle;
|
|
document.querySelectorAll("[data-i]").forEach((e) => (e.textContent = t[e.dataset.i]));
|
|
const COLS = ["colName", "colAddress", "colCoords", "colLunch", "colPromo", "colChecked", "colSource"];
|
|
document.getElementById("head").replaceChildren(...COLS.map((k) => { const th = document.createElement("th"); th.textContent = t[k]; return th; }));
|
|
try {
|
|
const r = await fetch("/api/base");
|
|
if (!r.ok) throw new Error();
|
|
const list = await r.json();
|
|
document.getElementById("count").textContent = fmt(t.baseCount, { n: list.length });
|
|
const body = document.getElementById("rows");
|
|
if (!list.length) { const e = document.getElementById("err"); e.textContent = t.listPreparing; e.hidden = false; }
|
|
for (const v of list) {
|
|
const promo = v.promo ? (t.promoTexts?.[v.promo.text] || v.promo.text) + (v.promo.until ? ` (${date(v.promo.until)})` : "") : t.noPromo;
|
|
const lunch = v.lunch ? fmt(t.baseLunch, v.lunch) : t.noLunch;
|
|
const tr = document.createElement("tr");
|
|
for (const s of [v.name, v.address, `${v.lat}, ${v.lng}`, lunch, promo, date(v.checked)]) {
|
|
const td = document.createElement("td"); td.textContent = s; tr.append(td);
|
|
}
|
|
body.append(tr);
|
|
}
|
|
} catch {
|
|
const e = document.getElementById("err"); e.textContent = t.baseError; e.hidden = false;
|
|
}
|