1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
;;; Copyright © 2019 - 2023 Jakob L. Kreuze <zerodaysfordays@sdf.org>
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation; either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.
(define-module (jakob dynamic capabilities rsvp-form)
#:use-module (haunt html)
#:use-module (jakob dynamic capabilities rsvp)
#:use-module (jakob dynamic util)
#:use-module (jakob theme)
#:use-module (jakob utils sxml)
#:use-module (json)
#:use-module (rnrs bytevectors)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-11)
#:use-module (sxml simple)
#:use-module (web request)
#:use-module (web uri)
#:export (get-rsvp-form
get-rsvp-edit-form
post-rsvp-form
post-rsvp-edit-form))
(define (event-name invitation-code)
(let ((event-info (event-info-internal invitation-code)))
(assoc-ref event-info 'title)))
(define* (render-rsvp-form invitation-code #:optional edit)
(let ((event-info (event-info-internal invitation-code)))
`((div (@ (id "rsvp"))
(div (@ (id "event-info"))
(h1 ,(assoc-ref event-info 'title))
,@(if (assoc-ref event-info 'image)
(list `(img (@ (style "float: right; max-size: 200px; margin: 16px;")
(src ,(format #f "data:image/png;base64,~a" (assoc-ref event-info 'image)))))
'()))
(p ,(assoc-ref event-info 'location))
(p ,(assoc-ref event-info 'date))
,@(drop (call-with-input-string (assoc-ref event-info 'description) xml->sxml) 1)
(form (@ (id "rsvp-input")
(method "post")
(action ,(if edit
(format #f "/api/event/edit/~a" invitation-code)
(format #f "/api/event/~a" invitation-code))))
(fieldset
,@(if edit
`((input (@ (hidden #t) (name "update") (value ,invitation-code))))
`((input (@ (hidden #t) (name "id") (value ,invitation-code)))))
(legend "Your Info")
(label (@ (for "name")) "Name:")
(input (@ (type "text")
(id "name")
(name "name")
(value ,(if (and edit (assoc-ref event-info 'name))
(assoc-ref event-info 'name)
""))
(required #t)
(size 24)))
(label (@ (for "email")) "Email:")
(input (@ (type "text")
(id "email")
(name "email")
(value ,(if (and edit (assoc-ref event-info 'email))
(assoc-ref event-info 'email)
""))
(required #t)
(size 24))))
(fieldset
(legend "RSVP Status")
(input (@ (type "radio")
(id "attending")
(name "rsvp")
(value "attending")
,@(if (and edit
(assoc-ref event-info 'attending)
(string= "attending" (assoc-ref event-info 'attending)))
'((checked #t))
'())))
(label (@ (for "attending")) "Attending")
(input (@ (type "radio")
(id "tentative")
(name "rsvp")
(value "tentative")
,@(if (and edit
(assoc-ref event-info 'attending)
(string= "tentative" (assoc-ref event-info 'attending)))
'((checked #t))
'())))
(label (@ (for "tentative")) "Tentative")
(input (@ (type "radio")
(id "not-attending")
(name "rsvp")
(value "not-attending")
,@(if (and edit
(assoc-ref event-info 'attending)
(string= "not-attending" (assoc-ref event-info 'attending)))
'((checked #t))
'())))
(label (@ (for "not-attending")) "Not Attending"))
(fieldset
(legend "Guests")
(div (@ (id "guest-view"))
(input (@ (type "text") (id "guest-list") (name "guests"))))
(input (@ (type "button") (id "add-entry") (value "+") (hidden #t)))
(input (@ (type "button") (id "remove-entry") (value "-") (hidden #t))))
(fieldset
(legend "All Set?")
(input (@ (type "submit") (id "submit-form") (value "Submit")))))
,(script "rsvp-min.js"))))))
(define* (render-receipt params)
(let ((receipt-url (format #f "https://jakob.space/api/event/edit/~a"
(assoc-ref params "receipt"))))
`((div (@ (id "rsvp"))
(p "Thanks for registering! Please bookmark or save the following link: "
,(hyperlink receipt-url receipt-url)
".")
(p "This will enable you to edit your RSVP later.")))))
(define (get-rsvp-form request body)
;; TODO
(let* ((path-encoded (uri-path (request-uri request)))
(path (split-and-decode-uri-path path-encoded))
(invitation-code (last path))
(form (render-rsvp-form invitation-code)))
(values '((content-type . (text/html)))
(sxml->html-string
(theme
#:title (format #f "You've Been Invited to ~a!" (event-name invitation-code))
#:content form)))))
(define (get-rsvp-edit-form request body)
;; TODO
(let* ((path-encoded (uri-path (request-uri request)))
(path (split-and-decode-uri-path path-encoded))
(invitation-code (last path))
(form (render-rsvp-form invitation-code #t)))
(values '((content-type . (text/html)))
(sxml->html-string
(theme
#:title (format #f "Edit Your Invitation to ~a" (event-name invitation-code))
#:content form)))))
(define (formdata->alist params)
(map (lambda (pair)
(cons (car pair) (cadr pair)))
params))
(define (post-rsvp-form request body)
;; TODO
(let* ((params (formdata->alist (decode-form (utf8->string body)))))
(let-values (((_ json) (create-new-event-rsvp params)))
(values '((content-type . (text/html)))
(sxml->html-string
(theme
#:title (format #f "Thank you!")
#:content (render-receipt (json-string->scm json))))))))
(define (post-rsvp-edit-form request body)
;; TODO
(let* ((params (formdata->alist (decode-form (utf8->string body)))))
(let-values (((_ json) (update-event-rsvp params)))
(values '((content-type . (text/html)))
(sxml->html-string
(theme
#:title (format #f "Thank you!")
#:content (render-receipt (json-string->scm json))))))))
|