fix: относительные пути к API (работа через прокси)

This commit is contained in:
dauren_yesserkepov 2026-08-10 09:30:18 +00:00
parent aaf3d30199
commit 2317564930

View File

@ -4,6 +4,9 @@ function api(url, opts) {
return r.json(); return r.json();
}); });
} }
function noServer(err) {
return /Failed to fetch|Unexpected token|<!doctype|NetworkError|TypeError: fetch/i.test(err && err.message || "");
}
/* ==== Обратный отсчёт до 20.07.2027 19:00 ==== */ /* ==== Обратный отсчёт до 20.07.2027 19:00 ==== */
var TARGET = new Date("2027-07-20T19:00:00").getTime(); var TARGET = new Date("2027-07-20T19:00:00").getTime();
@ -35,7 +38,7 @@ regForm.addEventListener("submit", function (e) {
note: document.getElementById("r-note").value.trim() note: document.getElementById("r-note").value.trim()
}; };
msg.className = "msg"; msg.textContent = "Отправляем клятву в королевство..."; msg.className = "msg"; msg.textContent = "Отправляем клятву в королевство...";
api("/api/regs", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data) }) api("api/regs", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data) })
.then(function (j) { .then(function (j) {
msg.className = "msg ok"; msg.className = "msg ok";
msg.textContent = "🎉 Ты в списке героев! Врата откроются 20.07.2027 в 19:00 — до встречи!"; msg.textContent = "🎉 Ты в списке героев! Врата откроются 20.07.2027 в 19:00 — до встречи!";
@ -44,7 +47,7 @@ regForm.addEventListener("submit", function (e) {
regForm.reset(); regForm.reset();
}) })
.catch(function (err) { .catch(function (err) {
if (err.message === "Failed to fetch" || /fetch/i.test(err.message)) { if (noServer(err)) {
msg.className = "msg ok"; msg.className = "msg ok";
msg.textContent = "🎉 Ты в списке героев! (демо-режим: сервер не запущен, на живой ссылке заявка сохранится)"; msg.textContent = "🎉 Ты в списке героев! (демо-режим: сервер не запущен, на живой ссылке заявка сохранится)";
} else { } else {
@ -84,14 +87,14 @@ function renderWishes(wishes) {
btn.addEventListener("click", function () { btn.addEventListener("click", function () {
var guest = prompt("Напиши своё имя, чтобы мы знали — кто несёт этот дар:"); var guest = prompt("Напиши своё имя, чтобы мы знали — кто несёт этот дар:");
if (!guest) return; if (!guest) return;
api("/api/wish/reserve", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id: Number(btn.dataset.id), guest: guest.trim() }) }) api("api/wish/reserve", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id: Number(btn.dataset.id), guest: guest.trim() }) })
.then(function (j) { renderWishes(j.data); }) .then(function (j) { renderWishes(j.data); })
.catch(function (err) { alert(err.message); }); .catch(function (err) { alert(err.message); });
}); });
}); });
} }
function loadWishes() { function loadWishes() {
api("/api/wish").then(function (j) { renderWishes(j.data); }) api("api/wish").then(function (j) { renderWishes(j.data); })
.catch(function () { renderWishes(MOCK); }); .catch(function () { renderWishes(MOCK); });
} }
loadWishes(); loadWishes();
@ -110,14 +113,14 @@ function renderAdminWishes(wishes) {
}).join(""); }).join("");
document.querySelectorAll("#a-wishes .a-del").forEach(function (b) { document.querySelectorAll("#a-wishes .a-del").forEach(function (b) {
b.addEventListener("click", function () { b.addEventListener("click", function () {
api("/api/admin/wish/del", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pin: pin, id: Number(b.dataset.id) }) }) api("api/admin/wish/del", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pin: pin, id: Number(b.dataset.id) }) })
.then(function (j) { renderAdminWishes(j.data); renderWishes(j.data); }) .then(function (j) { renderAdminWishes(j.data); renderWishes(j.data); })
.catch(function (err) { aMsg.className = "msg err"; aMsg.textContent = err.message; }); .catch(function (err) { aMsg.className = "msg err"; aMsg.textContent = err.message; });
}); });
}); });
} }
function loadAdmin() { function loadAdmin() {
api("/api/admin/regs", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pin: pin }) }) api("api/admin/regs", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pin: pin }) })
.then(function (j) { .then(function (j) {
aMsg.className = "msg ok"; aMsg.textContent = "Добро пожаловать, хозяин замка 🏰"; aMsg.className = "msg ok"; aMsg.textContent = "Добро пожаловать, хозяин замка 🏰";
aPanel.hidden = false; aPanel.hidden = false;
@ -129,7 +132,7 @@ function loadAdmin() {
: '<div class="muted">Пока никто не подтвердил участие.</div>'; : '<div class="muted">Пока никто не подтвердил участие.</div>';
document.getElementById("confirmedCount").hidden = false; document.getElementById("confirmedCount").hidden = false;
document.getElementById("confirmedCount").querySelector("b").textContent = j.total; document.getElementById("confirmedCount").querySelector("b").textContent = j.total;
return api("/api/wish"); return api("api/wish");
}) })
.then(function (j) { renderAdminWishes(j.data); }) .then(function (j) { renderAdminWishes(j.data); })
.catch(function (err) { .catch(function (err) {
@ -148,7 +151,7 @@ document.getElementById("a-wadd").addEventListener("click", function () {
var title = document.getElementById("a-wtitle").value.trim(); var title = document.getElementById("a-wtitle").value.trim();
var emoji = document.getElementById("a-wemoji").value.trim() || "🎁"; var emoji = document.getElementById("a-wemoji").value.trim() || "🎁";
if (!title) return; if (!title) return;
api("/api/admin/wish", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pin: pin, title: title, emoji: emoji }) }) api("api/admin/wish", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pin: pin, title: title, emoji: emoji }) })
.then(function (j) { .then(function (j) {
renderAdminWishes(j.data); renderWishes(j.data); renderAdminWishes(j.data); renderWishes(j.data);
document.getElementById("a-wtitle").value = ""; document.getElementById("a-wtitle").value = "";