dika/public/app.js
Peppa 8ff13c4af6
All checks were successful
peppa/lint lint: success за 0с
peppa/test test: success за 3с
peppa/build build: success за 0с
ci / checks (pull_request) Successful in 10s
deploy / deploy (pull_request) Successful in 15s
DIK-1: список ближайших мест для обеда с бизнес-ланчами и акциями
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 13:06:11 +05:00

55 lines
2.5 KiB
JavaScript

let t = {}, pos = null;
const $ = (id) => document.getElementById(id);
const fmt = (s, o) => s.replace(/\{(\w+)\}/g, (_, k) => o[k]);
const dist = (m) => (m < 1000 ? m + " " + t.m : (m / 1000).toFixed(1) + " " + t.km);
async function setLang(l) {
t = await (await fetch(`i18n/${l}.json`)).json();
document.documentElement.lang = l;
document.querySelectorAll("[data-i]").forEach((e) => (e.textContent = t[e.dataset.i]));
document.querySelectorAll("[data-ph]").forEach((e) => (e.placeholder = t[e.dataset.ph]));
document.title = t.title;
if (pos) load();
}
function msg(key) { $("out").innerHTML = `<div class="msg">${t[key]}</div>`; }
async function load() {
const r = await fetch(`/api/venues?lat=${pos.lat}&lng=${pos.lng}&only=${$("only").checked ? 1 : 0}`);
if (!r.ok) return msg("error");
const list = await r.json();
if (!list.length) return msg($("only").checked ? "noOffers" : "noneNearby");
const out = $("out"); out.textContent = "";
for (const v of list) {
const c = document.createElement("div"); c.className = "card";
const h = document.createElement("h2"); h.textContent = v.name;
const m = document.createElement("div"); m.className = "meta"; m.textContent = `${dist(v.distance)} · ${v.address}`;
c.append(h, m);
if (v.lunch) c.append(tag(fmt(t.lunch, { price: v.lunch.price, from: v.lunch.from, to: v.lunch.to })));
if (v.promo) c.append(tag(fmt(t.promo, { text: v.promo.text, until: v.promo.until })));
out.append(c);
}
}
function tag(s) { const d = document.createElement("div"); d.className = "tag"; d.textContent = s; return d; }
function showManual() { $("manual").hidden = false; }
$("geo").onclick = () => {
if (!navigator.geolocation) { showManual(); return msg("geoDenied"); }
navigator.geolocation.getCurrentPosition(
(p) => { pos = { lat: p.coords.latitude, lng: p.coords.longitude }; load(); },
() => { showManual(); msg("geoDenied"); });
};
$("district").onchange = (e) => { const d = districts.find((x) => x.id === e.target.value); if (d) { pos = d; load(); } };
$("addrBtn").onclick = async () => {
const r = await fetch("/api/locate?q=" + encodeURIComponent($("addr").value));
if (!r.ok) return msg("addrNotFound");
pos = await r.json(); load();
};
$("only").onchange = () => pos && load();
$("lang").onchange = (e) => setLang(e.target.value);
const districts = await (await fetch("/api/districts")).json();
$("district").innerHTML = "<option value=''>—</option>" + districts.map((d) => `<option value="${d.id}">${d.name}</option>`).join("");
await setLang("ru");