From 901f43e49782535d23eff2e8273a4d4b48d28fc2 Mon Sep 17 00:00:00 2001 From: oliver Date: Mon, 22 Jun 2026 18:29:48 -0300 Subject: [PATCH] Update app.js --- app.js | 76 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/app.js b/app.js index 9d7f031..c868f60 100644 --- a/app.js +++ b/app.js @@ -1402,7 +1402,6 @@ async function loadAffiliateLinks() { function renderAffiliateLinks(data) { const body = document.getElementById("affiliate-links-body"); if (!body) return; - // data is either { areas: [...] } or [{ areas: [...] }] or { data: [...] } let areas = []; if (data && Array.isArray(data.areas)) { areas = data.areas; @@ -1413,32 +1412,73 @@ function renderAffiliateLinks(data) { const first = data[0]; if (first && Array.isArray(first.areas)) areas = first.areas; } - const filtered = areas.filter((a) => a.text && a.link); - if (!filtered.length) { - body.innerHTML = - '

No affiliate links available yet.

'; - return; - } + body.innerHTML = ` -
-
- Area - Text - Link -
- ${filtered +
+ ${areas.length} areas + +
+
+ ${areas .map( (a) => ` -
- ${escHtml(a.area || "—")} - ${escHtml(a.text || "")} - ${a.link ? `Open →` : ``} +
+
${escHtml(a.area)}
+
+
+ + +
+
+
+
+ + +
+
`, ) .join("")}
`; } +function affiliateAreaChanged() { + const btn = document.getElementById("save-affiliate-btn"); + if (btn) btn.style.display = "inline-flex"; +} + +async function saveAffiliateLinks() { + const btn = document.getElementById("save-affiliate-btn"); + if (!btn) return; + btnLoad(btn, "Saving…"); + try { + const inputs = document.querySelectorAll(".affiliate-input"); + const map = {}; + for (const inp of inputs) { + const area = inp.dataset.area; + const field = inp.dataset.field; + if (!map[area]) map[area] = { area, text: "", link: "" }; + map[area][field] = inp.value; + } + const areas = Object.values(map); + const res = await fetch(AFFILIATE_LINKS_URL, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ areas }), + }); + if (!res.ok) throw new Error(`HTTP ${res.status}`); + toast("Affiliate links saved.", "success"); + btn.style.display = "none"; + loadAffiliateLinks(); + } catch (err) { + toast(err.message, "error"); + } finally { + btnReset(btn); + } +} + /* ═══════════════════════════════════════════════════════════════ RESTART ═══════════════════════════════════════════════════════════════ */