Add recipe

This commit is contained in:
Nicholas Kariniemi 2014-07-27 11:51:26 +03:00
parent a22a32d996
commit cbd44a5756
3 changed files with 50 additions and 31 deletions

View file

@ -10,3 +10,6 @@
(defn on-body-mousedown [f] (defn on-body-mousedown [f]
(let [body-elem (aget (.getElementsByTagName js/document "body") 0)] (let [body-elem (aget (.getElementsByTagName js/document "body") 0)]
(.addEventListener body-elem "mousedown" f))) (.addEventListener body-elem "mousedown" f)))
(defn event-val [event]
(.. event -target -value))

View file

@ -104,9 +104,6 @@
(om/set-state! owner :new-grub "") (om/set-state! owner :new-grub "")
(put! add (add-event new-grub)))) (put! add (add-event new-grub))))
(defn update-new-grub-value [event owner]
(om/set-state! owner :new-grub (.. event -target -value)))
(defn grubs-view [props owner] (defn grubs-view [props owner]
(reify (reify
om/IInitState om/IInitState
@ -126,7 +123,7 @@
:value new-grub :value new-grub
:on-key-up #(when (dom/enter-pressed? %) :on-key-up #(when (dom/enter-pressed? %)
(add-grub add new-grub owner)) (add-grub add new-grub owner))
:on-change #(update-new-grub-value % owner)}]] :on-change #(om/set-state! owner :new-grub (dom/event-val %))}]]
[:button.btn.btn-primary [:button.btn.btn-primary
{:id "add-grub-btn" {:id "add-grub-btn"
:type "button" :type "button"

View file

@ -6,6 +6,12 @@
(:require-macros [grub.macros :refer [log logs]] (:require-macros [grub.macros :refer [log logs]]
[cljs.core.async.macros :refer [go go-loop]])) [cljs.core.async.macros :refer [go go-loop]]))
(defn add-event [name grubs]
{:event :add-recipe
:id (str "recipe-" (.now js/Date))
:name name
:grubs grubs})
(defn recipe-view [recipe owner] (defn recipe-view [recipe owner]
(reify (reify
om/IRender om/IRender
@ -28,11 +34,18 @@
[:textarea.form-control.recipe-grubs-input [:textarea.form-control.recipe-grubs-input
{:id "recipe-grubs" {:id "recipe-grubs"
:rows 3 :rows 3
:placeholder "2 grubs"} :placeholder "2 grubs"
grubs] :value grubs}]
[:button.btn.btn-primary.hidden.pull-right.recipe-btn.recipe-done-btn [:button.btn.btn-primary.hidden.pull-right.recipe-btn.recipe-done-btn
{:type "button"} "Done"]]]))))) {:type "button"} "Done"]]])))))
(defn add-recipe [ch name grubs owner]
(when (and (not (empty? name))
(not (empty? grubs)))
(om/set-state! owner :new-recipe-name "")
(om/set-state! owner :new-recipe-grubs [])
(put! ch (add-event name grubs))))
(defn recipes-view [recipes owner] (defn recipes-view [recipes owner]
(reify (reify
om/IInitState om/IInitState
@ -40,10 +53,13 @@
(let [publisher (chan)] (let [publisher (chan)]
{:editing false {:editing false
:>local-events publisher :>local-events publisher
:<local-events (a/pub publisher identity)})) :<local-events (a/pub publisher identity)
:new-recipe-name ""
:new-recipe-grubs ""}))
om/IRenderState om/IRenderState
(render-state [this {:keys [editing >local-events]}] (render-state [this {:keys [editing >local-events new-recipe-name new-recipe-grubs]}]
(let [add (om/get-shared owner :recipe-add)]
(html (html
[:div [:div
[:h3.recipes-title "Recipes"] [:h3.recipes-title "Recipes"]
@ -51,34 +67,37 @@
{:on-click #(put! >local-events :click)} {:on-click #(put! >local-events :click)}
[:div.panel-heading.recipe-header [:div.panel-heading.recipe-header
[:input.form-control.recipe-header-input [:input.form-control.recipe-header-input
{:id "recipe-name" {:type "text"
:type "text" :placeholder "New recipe"
:placeholder "Grub pie"}]] :value new-recipe-name
:on-change #(om/set-state! owner :new-recipe-name (dom/event-val %))}]]
[:div.panel-body.recipe-grubs [:div.panel-body.recipe-grubs
{:class (when (not editing) "hidden")} {:class (when (not editing) "hidden")}
[:textarea.form-control.recipe-grubs-input [:textarea.form-control.recipe-grubs-input
{:id "recipe-grubs" {:id "recipe-grubs"
:rows 3 :rows 3
:placeholder "2 grubs"}] :placeholder "Recipe ingredients"
[:button.btn.btn-primary.hidden.pull-right.recipe-btn.recipe-done-btn :value new-recipe-grubs
{:type "button"} "Done"]]] :on-change #(om/set-state! owner :new-recipe-grubs (dom/event-val %))}]
[:button.btn.btn-primary.pull-right.recipe-btn.recipe-done-btn
{:type "button"
:on-click #(add-recipe add new-recipe-name new-recipe-grubs owner)}
"Done"]]]
[:ul#recipe-list.list-group.recipe-list [:ul#recipe-list.list-group.recipe-list
(for [recipe (vals recipes)] (for [recipe (vals recipes)]
(om/build recipe-view recipe {:key :id}))]])) (om/build recipe-view recipe {:key :id}))]])))
om/IWillMount om/IWillMount
(will-mount [_] (will-mount [_]
(let [<local-events (om/get-state owner :<local-events) (let [<local-events (om/get-state owner :<local-events)
<events (om/get-shared owner :<events)] <events (om/get-shared owner :<events)]
(go-loop [] (go-loop []
(log "waiting")
(let [subscriber (chan)] (let [subscriber (chan)]
(a/sub <local-events :click subscriber) (a/sub <local-events :click subscriber)
(<! subscriber) (<! subscriber)
(a/unsub <local-events :click subscriber) (a/unsub <local-events :click subscriber)
(a/close! subscriber)) (a/close! subscriber))
(om/set-state! owner :editing true) (om/set-state! owner :editing true)
(log "editing")
(let [subscriber (chan)] (let [subscriber (chan)]
(a/sub <events :body-mousedown subscriber) (a/sub <events :body-mousedown subscriber)
(loop [] (loop []