Files
skirmish/src/cljs/skirmish/wui/components/home_page.cljs
T

55 lines
2.8 KiB
Clojure

(ns skirmish.wui.components.home-page
(:require [cljs-http.client :as http]
[reagent.core :refer [atom]]
[cljs.core.async :refer [<!] :refer-macros [go]]
[secretary.core :as secretary]))
(def lorem-ipsum ["Lorem ipsum dolor amet pickled bushwick next level adaptogen ugh franzen salvia hoodie man bun organic kinfolk. Hammock pickled banjo, artisan single-origin coffee brunch 90's occupy taxidermy. Put a bird on it gentrify offal tilde. Authentic put a bird on it prism semiotics, roof party banh mi typewriter. XOXO schlitz sriracha godard."
"Succulents shoreditch twee, VHS hammock hot chicken distillery. Edison bulb aesthetic pork belly meggings. 3 wolf moon artisan keytar blue bottle. Actually pok pok asymmetrical, banh mi before they sold out PBR&B portland tumblr."
"Gentrify palo santo enamel pin, tumeric jianbing thundercats shaman green juice chambray yr. Kale chips synth meditation chambray thundercats helvetica, cornhole whatever kickstarter ennui pop-up lo-fi godard. Austin unicorn brunch woke hammock. Coloring book kombucha occupy tofu wayfarers."])
(defn email? [s]
(let [email-pattern #"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"]
(->> s str (re-matches email-pattern) some?)))
(defn create-user [email]
(go
(let [url "http://localhost:1605/api/1/users"
resp (<! (http/post url {:edn-params {:email email}}))]
(secretary/dispatch! (str "/home/" email)))))
(defn login [state]
(let [login-field (atom (get-in state [:user :skirmish.broker.user/email] ""))
on-change #(reset! login-field (-> % .-target .-value))]
(fn []
(let [disabled? (-> @login-field email? not)
href (if (not disabled?) (str "/home/" @login-field) nil)]
[:div.field
[:div.control
[:label.label "Email"]
[:div.field.is-grouped
[:input.input.is-success {:type "email"
:value @login-field
:on-change on-change
:placeholder "boris.johnson@redneck.co.uk"
:style {:margin-right "1em"}}]
[:div.control
[:a.button.is-link.has-background-primary {:href href
:disabled disabled?} "login"]]
[:div.control
[:button.button.is-link.has-background-primary {:disabled disabled?
:on-click #(create-user @login-field)} "create"]]]]]))))
(defn page [state]
[:div
[:section.section
[:div.container
[:h1.title "lorem ipsum"]
[login state]
(for [[i sentence] (map vector (range) lorem-ipsum)]
[:p {:key i} sentence])]]])