/* ▲ ● ▼ ★ for one role: the feed row and the read page share them. * * news's `voteButtons`, `starButton` and `vote()`, adapted. Three stances and * no stance is four states from three buttons; pressing the stance a role * already has clears it, which is the only way back to unjudged, since ● is * an answer ("neither way") and not the absence of one. A vote never hides a * role: a stray tap stays on screen, pressed, and one more tap undoes it. * * ★ is Save. It files the role under Applications (`POST /api/applications`) * and, pressed again, takes it back out (`DELETE /api/applications/{id}`), * which the server allows only while the application is still at `saved`; a * 409 is toasted by `api()` and the star stays lit. * * Both are optimistic: the button changes before the request and changes back * if it fails. `api()` has already toasted the failure by then. * * await loadTracked(); * container.append(judgeButtons(role, (role) => row.classList.toggle(...))); */ (function () { "use strict"; const { api } = window; /** @type {Map} role_uid -> application, for roles saved */ const tracked = new Map(); async function loadTracked() { const applications = await api("/api/applications"); tracked.clear(); for (const a of applications) if (a.role_uid) tracked.set(a.role_uid, a); return tracked; } const STANCES = [ { value: 1, cls: "up", glyph: "▲", said: "Like" }, { value: 0, cls: "mid", glyph: "●", said: "Neither way" }, { value: -1, cls: "down", glyph: "▼", said: "Pass" }, ]; function button(cls, glyph) { const b = document.createElement("button"); b.type = "button"; b.className = cls; b.textContent = glyph; return b; } /* The four buttons as a fragment, bound to `role`, which they read and write in place (`role.vote`), so the caller's copy is always current. `onChange(role)` runs after every change, optimistic or reverted: the feed dims a passed row with it. */ function judgeButtons(role, onChange) { const fragment = document.createDocumentFragment(); const votes = STANCES.map((s) => { const b = button(s.cls, s.glyph); b.addEventListener("click", () => vote(s.value)); fragment.appendChild(b); return b; }); const star = button("star", "★"); star.addEventListener("click", () => toggleSave()); fragment.appendChild(star); function paint() { STANCES.forEach((s, i) => { const on = role.vote === s.value; const said = on ? `${s.said} — tap again to undo` : s.said; votes[i].setAttribute("aria-pressed", String(on)); votes[i].title = said; votes[i].setAttribute("aria-label", said); }); const saved = tracked.has(role.uid); const said = saved ? "Saved to Applications — tap to unsave" : "Save to Applications"; star.setAttribute("aria-pressed", String(saved)); star.title = said; star.setAttribute("aria-label", said); if (onChange) onChange(role); } async function vote(value) { const before = role.vote ?? null; const clearing = before === value; role.vote = clearing ? null : value; paint(); const path = `/api/roles/${encodeURIComponent(role.uid)}/vote`; try { const updated = clearing ? await api(path, { method: "DELETE" }) : await api(path, { method: "POST", body: { value } }); role.vote = updated.vote; role.voted_at = updated.voted_at; } catch (err) { role.vote = before; } paint(); } // One request at a time: a second tap while the first is in flight would // unsave an application whose id has not come back yet. async function toggleSave() { if (star.disabled) return; star.disabled = true; const before = tracked.get(role.uid); try { if (before) { tracked.delete(role.uid); paint(); await api(`/api/applications/${encodeURIComponent(before.id)}`, { method: "DELETE" }); } else { tracked.set(role.uid, { role_uid: role.uid }); paint(); tracked.set(role.uid, await api("/api/applications", { method: "POST", body: { role_uid: role.uid, status: "saved" }, })); } } catch (err) { if (before) tracked.set(role.uid, before); else tracked.delete(role.uid); } finally { star.disabled = false; } paint(); } paint(); return fragment; } Object.assign(window, { judgeButtons, loadTracked }); })();