diff options
| -rw-r--r-- | dynamic/api.scm | 21 | ||||
| -rw-r--r-- | haunt/static/js/rsvp.js | 57 |
2 files changed, 70 insertions, 8 deletions
diff --git a/dynamic/api.scm b/dynamic/api.scm index 1bd3540..7ca3e36 100644 --- a/dynamic/api.scm +++ b/dynamic/api.scm @@ -1,7 +1,9 @@ (add-to-load-path (dirname (current-filename))) +;; (add-to-load-path "/home/jakob/Blog/dynamic") (use-modules (base64) (captcha) + (ice-9 binary-ports) (json) (srfi srfi-1) (srfi srfi-11) @@ -84,16 +86,22 @@ (x-captcha-id . ,uuid)) (base64-encode image)))) -(define (valid-invite-code invitation) - (and (= (string-length)))) +;; (define (valid-invite-code invitation) +;; (and (= (string-length)))) + +(define (generate-vanity-code) + (call-with-input-file "/dev/urandom" + (lambda (port) + (base64-encode (get-bytevector-n port 9))))) (define (post-event-rsvp request body) (let* ((params (json-string->scm (utf8->string body))) (invitation-code (assoc-ref params "id")) (name (assoc-ref params "name")) (email (assoc-ref params "email")) + (attending (assoc-ref params "rsvp")) (guests (assoc-ref params "guests"))) - (cond ((not (and invitation-code name email guests)) + (cond ((not (and invitation-code name email attending guests)) (values (build-response #:code 400 #:headers '((Access-Control-Allow-Origin . "*"))) (scm->json-string `((success . #f) @@ -104,12 +112,13 @@ `((success . #f) (error . "Invalid invitation code"))))) (else - (let* ((event-id (caar (exec-query conn "SELECT event_id FROM invitations WHERE vanity = $1" (list invitation-code))))) - (exec-query conn "INSERT INTO rsvps (event_id, fullname, email, guests) VALUES ($1, $2, $3, $4)" (list event-id name email guests)) + (let* ((receipt-code (generate-vanity-code)) + (event-id (caar (exec-query conn "SELECT event_id FROM invitations WHERE vanity = $1" (list invitation-code))))) + (exec-query conn "INSERT INTO rsvps (vanity, event_id, fullname, email, attending, guests) VALUES ($1, $2, $3, $4, $5, $6)" (list receipt-code event-id name email attending guests)) (values '((content-type . (application/json)) (Access-Control-Allow-Origin . "*")) (scm->json-string - `((success . #t))))))))) + `((receipt . ,receipt-code))))))))) (define (get-event-info request body) (define (format-rsvp rsvp) 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); |