Created
September 17, 2014 20:12
-
-
Save sritchie/b9e30f2420df32c1f0fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(sm/defn coupon-lookup | |
"If the code supplied is empty, sends a failed discount on | |
immediately. (This won't affect the rendering, since nothing's | |
shown if the code is blank.) If the code is NOT blank, fires off an | |
Ajax request to the server to look up the discount. | |
TODO: Ideally we'd have a channel that could signal a dissoc, | |
instead of only adding items to the state." | |
[title :- regatta/Title | |
code :- coupon/Code | |
user-id :- user/UserID | |
out :- ps/Channel] | |
(let [out (a/map> (fn [resp] | |
(assoc (if (:success resp) | |
{:state :successful | |
:discount (:discount resp)} | |
{:state :failed | |
:error (:error resp)}) | |
:code code)) | |
out)] | |
(if (empty? code) | |
(put! out {:success false :error "Blank code."}) | |
(ajax/GET (str "/races/" | |
(u/clear-specials title) | |
"/coupons/" code) | |
{:headers {"Accept" "application/edn"} | |
:params {:user-id user-id} | |
:handler #(put! out %) | |
:error-handler #(put! out (:response %))})))) | |
(sm/defn coupon-feedback :- s/Str | |
"Renders the help message that gets displayed if there's a discount | |
for this particular coupon." | |
[discount :- vr/DiscountResponse] | |
(case (:state discount) | |
:successful (let [discount (:discount discount)] | |
(cond | |
(coupon/free? discount) "Your registration is free!" | |
(coupon/amount? discount) | |
(str (u/pennies->currency (:value discount)) " off.") | |
(coupon/percent? discount) | |
(str (:value discount) "% off."))) | |
:pending "Retrieving coupon..." | |
:failed (:error discount))) | |
(defcomponentk coupon-field | |
[[:data code discount title user-id] state] | |
(init-state [_] {:fetcher (chan)}) | |
(will-mount | |
[_] (let [code-ch (:fetcher @state) | |
result-ch (:discount-ch @state)] | |
(go-loop [] | |
(let [new-code (a/<! code-ch)] | |
(a/>! result-ch {:state :pending :code new-code}) | |
(coupon-lookup title new-code user-id result-ch) | |
(recur))))) | |
(render-state | |
[_ {:keys [code-ch fetcher]}] | |
(let [show-feedback? (and (not-empty code) (= code (:code discount)))] | |
(i/input {:type "text" | |
:label "Coupon Code" | |
:feedback? true | |
:bs-style (when show-feedback? | |
(case (:state discount) | |
:successful "success" | |
:pending "warning" | |
:failed "error")) | |
:value code | |
:auto-complete false | |
:on-change #(put! code-ch (.. % -target -value)) | |
:on-blur #(let [s (.. % -target -value)] | |
(when (or (not discount) | |
(not= s (:code @discount))) | |
(put! fetcher (.. % -target -value)))) | |
:help (when show-feedback? | |
(coupon-feedback discount)) | |
:label-classname "col-sm-3" | |
:wrapper-classname "col-sm-3"})))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment