From a58832da22ca52e39544b6c53aaa3e1910a892bc Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Mon, 28 Jul 2014 21:11:48 +0300 Subject: [PATCH] Edit recipes --- src/cljs/grub/state.cljs | 23 +++++----- src/cljs/grub/view/app.cljs | 4 +- src/cljs/grub/view/recipe.cljs | 76 +++++++++++++++++++++++++++++++++- 3 files changed, 90 insertions(+), 13 deletions(-) diff --git a/src/cljs/grub/state.cljs b/src/cljs/grub/state.cljs index 262b7bc..7d3f94c 100644 --- a/src/cljs/grub/state.cljs +++ b/src/cljs/grub/state.cljs @@ -26,11 +26,11 @@ (into {}))) (defmethod handle-event :add-grub-list [event state] - (let [add-grub-events (:grubs event) - add-grubs (->> event - :grubs - (map-by-key :id))] - (assoc state :grubs (merge (:grubs state) add-grubs)))) + (->> event + :grubs + (map-by-key :id) + (merge (:grubs state)) + (assoc state :grubs))) (defmethod handle-event :update-grub [event state] (let [new-grub-info (dissoc event :event-type) @@ -48,15 +48,16 @@ (assoc-in state [:recipes (:id recipe)] recipe))) (defmethod handle-event :add-recipe-list [event state] - (->> (:recipes event) - (map #(new-recipe (:id %) (:name %) (:grubs %))) - (reduce (fn [recipes r] (assoc recipes (:id r) r)) (:recipes state)) + (->> event + :recipes + (map-by-key :id) + (merge (:recipes state)) (assoc state :recipes))) (defmethod handle-event :update-recipe [event state] - (->> state - (assoc-in [:recipes (:id event) :name] (:name event)) - (assoc-in [:recipes (:id event) :grubs] (:grubs event)))) + (-> state + (assoc-in [:recipes (:id event) :name] (:name event)) + (assoc-in [:recipes (:id event) :grubs] (:grubs event)))) (defn update-state-and-render [remote] (let [out (chan) diff --git a/src/cljs/grub/view/app.cljs b/src/cljs/grub/view/app.cljs index 8c7f1aa..db4a246 100644 --- a/src/cljs/grub/view/app.cljs +++ b/src/cljs/grub/view/app.cljs @@ -30,7 +30,8 @@ grub-clear-all (chan) recipe-add (chan) recipe-add-grubs (chan) - out (a/merge [grub-add grub-update grub-clear-all recipe-add recipe-add-grubs]) + recipe-update (chan) + out (a/merge [grub-add grub-update grub-clear-all recipe-add recipe-add-grubs recipe-update]) >events (chan) events :type)] (om/root app-view @@ -41,6 +42,7 @@ :grub-clear-all grub-clear-all :recipe-add recipe-add :recipe-add-grubs recipe-add-grubs + :recipe-update recipe-update :>events >events :> grubs-str (clojure.string/split-lines) @@ -52,7 +58,75 @@ :placeholder "2 grubs" :value grubs}] [:button.btn.btn-primary.hidden.pull-right.recipe-btn.recipe-done-btn - {:type "button"} "Done"]]]))))) + {:type "button"} "Save"]]]))))) + +(defn update-recipe [ch id name grubs owner] + (when (and (not (empty? name)) + (not (empty? grubs))) + (om/set-state! owner :editing false) + (put! ch (update-event id name grubs)))) + +(defn recipe-view [{:keys [id] :as props} owner] + (reify + om/IInitState + (init-state [_] + (let [publisher (chan)] + {:editing false + :>local-events publisher + :local-events name grubs]}] + (let [update (om/get-shared owner :recipe-update)] + (html + [: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" + :value name + :on-change #(om/set-state! owner :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 + :value grubs + :on-change #(om/set-state! owner :grubs (dom/event-val %))}] + [:button.btn.btn-primary.pull-right.recipe-btn.recipe-done-btn + {:type "button" + :on-click #(update-recipe update id name grubs owner)} + "Save"]]]))) + + om/IWillMount + (will-mount [_] + (let [