From cbd44a5756d1e2cc1ea488a0cce760eb05f1563b Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Sun, 27 Jul 2014 11:51:26 +0300 Subject: [PATCH] Add recipe --- src/cljs/grub/view/dom.cljs | 3 ++ src/cljs/grub/view/grub.cljs | 5 +-- src/cljs/grub/view/recipe.cljs | 73 +++++++++++++++++++++------------- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/cljs/grub/view/dom.cljs b/src/cljs/grub/view/dom.cljs index d0fe1ba..9041363 100644 --- a/src/cljs/grub/view/dom.cljs +++ b/src/cljs/grub/view/dom.cljs @@ -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)) diff --git a/src/cljs/grub/view/grub.cljs b/src/cljs/grub/view/grub.cljs index 3eacef8..06b4152 100644 --- a/src/cljs/grub/view/grub.cljs +++ b/src/cljs/grub/view/grub.cljs @@ -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" diff --git a/src/cljs/grub/view/recipe.cljs b/src/cljs/grub/view/recipe.cljs index 3b08bee..0c8b84a 100644 --- a/src/cljs/grub/view/recipe.cljs +++ b/src/cljs/grub/view/recipe.cljs @@ -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]}] - (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 [