Skip to content

Commit

Permalink
prelaunch: Waitlist logic and form
Browse files Browse the repository at this point in the history
  • Loading branch information
goshatch committed Oct 3, 2024
1 parent 2339044 commit b2c4f79
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
23 changes: 21 additions & 2 deletions src/tools/ifs/parts/layouts/partials.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(ns tools.ifs.parts.layouts.partials)
(ns tools.ifs.parts.layouts.partials
(:require
[ring.middleware.anti-forgery :refer [*anti-forgery-token*]]))

(defn header
"Site header"
Expand All @@ -13,7 +15,7 @@
[:footer
[:div.copyright
[:p
"© 2024 "
"© 2024 "
[:a {:href "https://a.possible.space"} "A Possible Space Ltd"]
[:br]
"Company number 11617016"]]
Expand All @@ -25,3 +27,20 @@
"See the "
[:a {:href "https://github.com/apossiblespace/parts"} "source code on GitHub"]
"."]]])

(defn waitlist-signup-form
"Form for signing up for the waiting list"
[target]
[:div#signup-form
[:form {:hx-post "/waitlist-signup"
:hx-target target
:hx-swap "outerHTML"}
[:input {:type "email"
:id "email"
:name "email"
:placeholder "[email protected]"}]
[:input {:type "hidden"
:id "__anti-forgery-token"
:name "__anti-forgery-token"
:value *anti-forgery-token* }]
[:input {:type "submit" :value "Sign me up!"}]]])
6 changes: 4 additions & 2 deletions src/tools/ifs/parts/pages.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns tools.ifs.parts.pages
(:require [hiccup2.core :refer [html]]
[tools.ifs.parts.layouts.main :refer [layout]]
[tools.ifs.parts.layouts.partials :refer [header footer]]
[tools.ifs.parts.layouts.partials :refer [header footer waitlist-signup-form]]
[ring.util.response :as response]))

(defn system-graph
Expand Down Expand Up @@ -30,5 +30,7 @@
[:h3.hook
{:align "center"}
[:strong "Parts"]
" is a tool for IFS practitioners to keep track of, visualise, and explore the relationships between their clients’ parts."]]
" is a tool for IFS practitioners to keep track of, visualise, and explore the relationships between their clients’ parts."]
[:div#signup-form
(waitlist-signup-form "#signup-form")]]
(footer))))))
29 changes: 23 additions & 6 deletions src/tools/ifs/parts/waitlist.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@
[hiccup2.core :refer [html]]
[com.brunobonacci.mulog :as mulog]
[ring.util.response :as response]
[tools.ifs.parts.db :as db]))
[tools.ifs.parts.db :as db]
[tools.ifs.parts.layouts.partials :as partials]
[clojure.string :as str]))

(defn signup
"Register email address in private beta waitlist"
[request]
(db/insert! :waitlist_signups {:email (get-in request [:body :email])})
(-> (response/response
(html [:div#thankyou
[:p "Thank you for your interest!"]]))
(response/status 201)))
(let [email (get-in request [:form-params "email"])]
(if (and email (not (str/blank? email)))
(try
(db/insert! :waitlist_signups {:email email})
(mulog/log ::waitlist_signup :email email)
(-> (response/response
(html [:div.success
[:p "Thank you for your interest! We'll be in touch soon."]]))
(response/status 201))
(catch Exception _e
(-> (response/response
(html [:div.success
[:p "You're already on the list! We'll be in touch soon."]]))
(response/status 200))))
(-> (response/response
(html [:div#signup-form
(partials/waitlist-signup-form "#signup-form")
[:div.error
[:p "Please don't forget your email address!"]]]))
(response/status 200)))))
35 changes: 26 additions & 9 deletions test/tools/ifs/parts/waitlist_test.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
(ns tools.ifs.parts.waitlist-test
(:require [tools.ifs.parts.waitlist :as waitlist]
[tools.ifs.helpers.test-helpers :refer [with-test-db]]
[clojure.test :refer [deftest is testing use-fixtures]]
[tools.ifs.parts.api.middleware :as middleware]
[tools.ifs.parts.db :as db]))
(:require
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
[tools.ifs.helpers.test-helpers :refer [with-test-db]]
[tools.ifs.parts.api.middleware :as middleware]
[tools.ifs.parts.db :as db]
[tools.ifs.parts.waitlist :as waitlist]))

(use-fixtures :once with-test-db)

(deftest test-signup
(testing "saves the submitted email address in the database"
(let [attrs {:email "[email protected]"}
mock-request {:body attrs}
(let [mock-request {:form-params {"email" "[email protected]"}}
wrapped-handler (middleware/wrap-html-response waitlist/signup)
response (wrapped-handler mock-request)
record (db/query-one (db/sql-format {:select [:*]
:from [:waitlist_signups]
:where [:= :email (:email attrs)]}))]
:where [:= :email "[email protected]"]}))]
(is (= 201 (:status response)))
(is (= "[email protected]" (:email record)))
(is (= "<div id=\"thankyou\"><p>Thank you for your interest!</p></div>" (:body response))))))
(is (str/includes? (:body response) "Thank you for your interest!"))))

(testing "returns a reminder to not forget the email when email is missing"
(let [mock-request {:form-params {"email" ""}}
wrapped-handler (middleware/wrap-html-response waitlist/signup)
response (wrapped-handler mock-request)]
(is (= 200 (:status response)))
(is (str/includes? (:body response) "Please don&apos;t forget your email address!"))))

(testing "returns a notice that the person is already on list when email is a duplicate"
(let [email "[email protected]"
_ (db/insert! :waitlist_signups {:email email})
mock-request {:form-params {"email" email}}
wrapped-handler (middleware/wrap-html-response waitlist/signup)
response (wrapped-handler mock-request)]
(is (= 200 (:status response)))
(is (str/includes? (:body response) "You&apos;re already on the list! We&apos;ll be in touch soon.")))))

0 comments on commit b2c4f79

Please sign in to comment.