diff --git a/web/resources/static/js/connection-event.js b/web/resources/static/js/connection-event.js new file mode 100644 index 0000000..d622cae --- /dev/null +++ b/web/resources/static/js/connection-event.js @@ -0,0 +1,48 @@ +// atmo.quest — toggle whether a connection is tagged with the event you're +// currently checked into. POSTs {associate: bool} to +// /connections/{did}/event. The server re-derives which event; we only send +// the boolean. On failure we revert the checkbox so the UI matches the PDS. +(function () { + "use strict"; + + var toggle = document.querySelector(".pv-event-toggle"); + if (!toggle) return; + + var targetDID = toggle.getAttribute("data-target-did"); + var checkbox = document.getElementById("pv-event-associate"); + var status = document.getElementById("pv-event-status"); + if (!targetDID || !checkbox) return; + + function showStatus(msg) { + if (!status) return; + status.textContent = msg; + clearTimeout(status._timer); + status._timer = setTimeout(function () { status.textContent = ""; }, 2000); + } + + checkbox.addEventListener("change", function () { + var desired = checkbox.checked; + checkbox.disabled = true; + fetch("/connections/" + encodeURI(targetDID) + "/event", { + method: "POST", + credentials: "same-origin", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ associate: desired }) + }) + .then(function (resp) { + if (resp.ok) { + showStatus("✓ saved"); + } else { + checkbox.checked = !desired; // revert + showStatus("save failed"); + } + }) + .catch(function () { + checkbox.checked = !desired; // revert + showStatus("save failed"); + }) + .finally(function () { + checkbox.disabled = false; + }); + }); +})();