Edit recipes

This commit is contained in:
Nicholas Kariniemi 2014-07-28 21:11:48 +03:00
parent c208d9a625
commit a58832da22
3 changed files with 90 additions and 13 deletions

View file

@ -26,11 +26,11 @@
(into {}))) (into {})))
(defmethod handle-event :add-grub-list [event state] (defmethod handle-event :add-grub-list [event state]
(let [add-grub-events (:grubs event) (->> event
add-grubs (->> event
:grubs :grubs
(map-by-key :id))] (map-by-key :id)
(assoc state :grubs (merge (:grubs state) add-grubs)))) (merge (:grubs state))
(assoc state :grubs)))
(defmethod handle-event :update-grub [event state] (defmethod handle-event :update-grub [event state]
(let [new-grub-info (dissoc event :event-type) (let [new-grub-info (dissoc event :event-type)
@ -48,13 +48,14 @@
(assoc-in state [:recipes (:id recipe)] recipe))) (assoc-in state [:recipes (:id recipe)] recipe)))
(defmethod handle-event :add-recipe-list [event state] (defmethod handle-event :add-recipe-list [event state]
(->> (:recipes event) (->> event
(map #(new-recipe (:id %) (:name %) (:grubs %))) :recipes
(reduce (fn [recipes r] (assoc recipes (:id r) r)) (:recipes state)) (map-by-key :id)
(merge (:recipes state))
(assoc state :recipes))) (assoc state :recipes)))
(defmethod handle-event :update-recipe [event state] (defmethod handle-event :update-recipe [event state]
(->> state (-> state
(assoc-in [:recipes (:id event) :name] (:name event)) (assoc-in [:recipes (:id event) :name] (:name event))
(assoc-in [:recipes (:id event) :grubs] (:grubs event)))) (assoc-in [:recipes (:id event) :grubs] (:grubs event))))

View file

@ -30,7 +30,8 @@
grub-clear-all (chan) grub-clear-all (chan)
recipe-add (chan) recipe-add (chan)
recipe-add-grubs (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 (chan)
<events (a/pub >events :type)] <events (a/pub >events :type)]
(om/root app-view (om/root app-view
@ -41,6 +42,7 @@
:grub-clear-all grub-clear-all :grub-clear-all grub-clear-all
:recipe-add recipe-add :recipe-add recipe-add
:recipe-add-grubs recipe-add-grubs :recipe-add-grubs recipe-add-grubs
:recipe-update recipe-update
:>events >events :>events >events
:<events <events}}) :<events <events}})
out)) out))

View file

@ -14,6 +14,12 @@
:name name :name name
:grubs grubs}) :grubs grubs})
(defn update-event [id name grubs]
{:event :update-recipe
:id id
:name name
:grubs grubs})
(defn parse-grubs-from-str [grubs-str] (defn parse-grubs-from-str [grubs-str]
(->> grubs-str (->> grubs-str
(clojure.string/split-lines) (clojure.string/split-lines)
@ -52,7 +58,75 @@
:placeholder "2 grubs" :placeholder "2 grubs"
:value 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"} "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 (a/pub publisher identity)
:name (:name props)
:grubs (:grubs props)}))
om/IWillReceiveProps
(will-receive-props [this next-props]
(om/set-state! owner :name (:name next-props))
(om/set-state! owner :grubs (:grubs next-props)))
om/IRenderState
(render-state [this {:keys [editing >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 [<local-events (om/get-state owner :<local-events)
<events (om/get-shared owner :<events)]
(go-loop []
(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)
(let [subscriber (chan)]
(a/sub <events :body-mousedown subscriber)
(loop []
(let [event (<! subscriber)]
(when (and (= (:type event) :body-mousedown)
(dom/click-on-self? (:event event) (om/get-node owner)))
(recur))))
(a/unsub <events :body-mousedown subscriber)
(a/close! subscriber))
(om/set-state! owner :editing false)
(recur))))))
(defn add-recipe [ch name grubs owner] (defn add-recipe [ch name grubs owner]
(when (and (not (empty? name)) (when (and (not (empty? name))