Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bidirection contacts added after scanning QR code #33

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/co/gaiwan/compass/routes/profiles.clj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@

(defn GET-qr-html [req]
{:html/body [:div
{:style {:margin "var(--size-4)"}}
[:h2 "Add Contact"]
[:img {:src (url-for :contact/qr-png)}]]
:html/layout false})
Expand All @@ -157,8 +158,8 @@
[{:keys [identity] :as req}]
(let [user-eid (:db/id identity)
host (config/value :compass/origin)
qr-hash (eid->qr-hash user-eid)
url (str host "/attendees/" qr-hash)
qr-hash (str (eid->qr-hash user-eid))
url (str host (url-for :contact/add {:qr-hash qr-hash}))
qr-image (qr/as-bytes (qr/from url :size [400 400]))]
(-> (ring-response/response qr-image)
(assoc-in [:headers "content-type"] "image/png"))))
Expand All @@ -171,8 +172,17 @@
(for [atd (attendees/user-list attendees)]
(h/attendee-card atd))]}))

(defn GET-contact [req]
{:html/body [:button {:hx-post (url-for :contact/add {:uuid "123"})}]})
(defn GET-contact
[req]
{:html/body
[:div
[:a
{:href (url-for :profile/index)
:style {:display "none"}
:hx-trigger "contact-added from:body"}]
[:button
{:hx-post (url-for :contact/add {:qr-hash (get-in req [:path-params :qr-hash])})}
(str "Accept invite")]]})

(defn POST-contact
"Part of the url is hash of the contact's user eid
Expand All @@ -186,9 +196,11 @@
;; contact -> A
;; user -> B
_ @(db/transact [{:db/id contact-eid
:user/contacts user-eid}])]
(response/redirect "/profile"
{:flash "Successfully Saved!"})))
:user/contacts user-eid}
{:db/id user-eid
:user/contacts contact-eid}])]
{:location :contact/add
:hx/trigger "contact-added"}))

(defn routes []
[["/profile"
Expand Down Expand Up @@ -216,12 +228,13 @@
["/qr" {:name :contact/qr
:get {:handler GET-qr-html}}]
["/qr.png" {:name :contact/qr-png
:get {:handler GET-qr-code}}]]
:get {:handler GET-qr-code}}]
["/:qr-hash"
{:name :contact/add
:post {:handler POST-contact}
:get {:handler GET-contact}}]]
["/attendees"
[""
{:name :attendees/index
:middleware [[response/wrap-requires-auth]]
:get {:handler GET-attendees}}]
["/:qr-hash"
{:middleware [[response/wrap-requires-auth]]
:get {:handler GET-contact}}]]])
:get {:handler GET-attendees}}]]])
Loading