diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2022-04-12 20:26:36 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2022-05-08 19:10:19 -0400 |
| commit | 1d34616291ffac85596e56a4950ba29bb89c6a41 (patch) | |
| tree | 06c83decb5c7f03dcdb9ef5c3db0fe0582f7e2d8 /haunt | |
| parent | c3ef51bc753f48607032e77d0b344453c7142141 (diff) | |
[scheme] Generate receipt code for RSVP submission
Diffstat (limited to 'haunt')
| -rw-r--r-- | haunt/static/js/rsvp.js | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/haunt/static/js/rsvp.js b/haunt/static/js/rsvp.js index 9ba98ac..c7b4cd8 100644 --- a/haunt/static/js/rsvp.js +++ b/haunt/static/js/rsvp.js @@ -99,7 +99,7 @@ addEntry.addEventListener("click", () => { let i = guestView.childNodes.length; let elem = document.createElement("input"); elem.type = "text"; - elem.id = `guest-${i}`; + elem.name = `guest-${i}`; guestView.appendChild(elem); }); @@ -111,7 +111,60 @@ removeEntry.addEventListener("click", () => { function submitForm() { let data = new FormData(form); - console.log(data); + if (data.get("name").trim().length == 0) { + alert("You have to provide something for \"Name\"."); + return; + } else if (data.get("rsvp") === null) { + alert("You have to choose an option for \"RSVP Status\"."); + return; + } + + let res = { id: urlParams.get("i"), guests: [] }; + for (let pair of data.entries()) { + console.log(pair[0]); + if (pair[0].startsWith("guest-")) { + res.guests.push(pair[1]); + } else if (pair[0] === "rsvp") { + const radioButtons = document.querySelectorAll('input[name="rsvp"]'); + for (const radioButton of radioButtons) { + if (radioButton.checked) { + res.rsvp = radioButton.id; + break; + } + } + } else { + res[pair[0]] = pair[1]; + } + } + res.guests = res.guests.join(","); + console.log(res); + + let xhr = new XMLHttpRequest(); + xhr.open("POST", "http://localhost:8081/api/rsvp"); + xhr.send(JSON.stringify(res)); + xhr.onload = function(data) { + if (xhr.status != 200) { + alert("Failed to submit form."); + } else { + document.getElementById("rsvp").innerHTML = ""; + let resp = JSON.parse(xhr.response); + let elem = document.createElement("p"); + elem.innerHTML = "Thanks for registering! Please bookmark or save the following link:"; + document.getElementById("rsvp").appendChild(elem); + + elem = document.createElement("a"); + elem.href = [location.protocol, '//', location.host, location.pathname].join('') + `?r=${resp.receipt}`; + elem.textContent = elem.href; + document.getElementById("rsvp").appendChild(elem); + + elem = document.createElement("p"); + elem.innerHTML = "This will enable you to edit your RSVP later."; + document.getElementById("rsvp").appendChild(elem); + } + } + xhr.onerror = function(error) { + throw new Error(`Request failed: ${error}`); + } } submitButton.addEventListener("click", submitForm); |