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]
(let [body-elem (aget (.getElementsByTagName js/document "body") 0)]
(.addEventListener body-elem "mousedown" f)))
(defn event-val [event]
(.. event -target -value))

View file

@ -104,9 +104,6 @@
(om/set-state! owner :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]
(reify
om/IInitState
@ -126,7 +123,7 @@
:value new-grub
:on-key-up #(when (dom/enter-pressed? %)
(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
{:id "add-grub-btn"
:type "button"

View file

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