diff --git a/src/cljs/grub/state.cljs b/src/cljs/grub/state.cljs index 81df91e..03317f1 100644 --- a/src/cljs/grub/state.cljs +++ b/src/cljs/grub/state.cljs @@ -66,13 +66,11 @@ (assoc state :recipes (dissoc (:recipes state) (:id event)))) (defn update-state-and-render [remote] - (let [out (chan) - view-events (view/render-app app-state)] + (view/render-app app-state) + (let [out (chan)] (go-loop [] - (let [[event ch] (alts! [remote view-events]) + (let [event (! out event)) (recur))) out)) diff --git a/src/cljs/grub/view/app.cljs b/src/cljs/grub/view/app.cljs index dfa73be..5f1463e 100644 --- a/src/cljs/grub/view/app.cljs +++ b/src/cljs/grub/view/app.cljs @@ -26,29 +26,12 @@ (dom/on-window-scroll #(put! >events {:type :body-scroll :event %})))))) (defn render-app [state] - (let [grub-add (chan) - grub-update (chan) - grub-clear-all (chan) - grub-remove (chan) - recipe-add (chan) - recipe-add-grubs (chan) - recipe-update (chan) - recipe-remove (chan) - out (a/merge [grub-add grub-update grub-clear-all grub-remove - recipe-add recipe-add-grubs recipe-update recipe-remove]) - >events (chan) - events :type)] + (let [>events (chan) + events :type) + add-grubs-ch (chan)] (om/root app-view state {:target (.getElementById js/document "container") - :shared {:grub-add grub-add - :grub-update grub-update - :grub-clear-all grub-clear-all - :grub-remove grub-remove - :recipe-add recipe-add - :recipe-add-grubs recipe-add-grubs - :recipe-update recipe-update - :recipe-remove recipe-remove - :>events >events - :events >events + :> grubs-str (clojure.string/split-lines) (map grub-view/new-grub) (into []))) +(defn map-by-key [key coll] + (->> coll + (map (fn [a] [(get a key) a])) + (into {}))) + (defn add-grubs [add-grubs-ch grubs-str] (let [grubs (parse-grubs-from-str grubs-str) - event (grub-view/add-list-event grubs)] - (put! add-grubs-ch event))) + grubs-map (map-by-key :id grubs)] + (put! add-grubs-ch grubs-map))) (def transitions {:waiting {:click :editing} @@ -44,38 +38,35 @@ (let [current (om/get-state owner :edit-state) next (or (get-in transitions [current event]) current)] (condp = [current next] - [:editing :waiting] (let [update-ch (om/get-shared owner :recipe-update) - id (:id @(om/get-props owner)) + [:editing :waiting] (let [recipe (om/get-props owner) name (om/get-state owner :name) - grubs (om/get-state owner :grubs) - event (update-event id name grubs)] - (put! update-ch event)) + grubs (om/get-state owner :grubs)] + (om/transact! recipe #(assoc % :name name :grubs grubs))) nil) (om/set-state! owner :edit-state next))) (defn num-newlines [str] (count (re-seq #"\n" str))) -(defn view [{:keys [id] :as props} owner] +(defn view [{:keys [id] :as recipe} owner {:keys [remove-recipe-ch]}] (reify om/IInitState (init-state [_] (let [publisher (chan)] {:edit-state :waiting - :name (:name props) - :grubs (:grubs props) + :name (:name recipe) + :grubs (:grubs recipe) :unmounted false})) om/IWillReceiveProps - (will-receive-props [this next-props] - (om/set-state! owner :name (:name next-props)) - (om/set-state! owner :grubs (:grubs next-props))) + (will-receive-props [this next-recipe] + (om/set-state! owner :name (:name next-recipe)) + (om/set-state! owner :grubs (:grubs next-recipe))) om/IRenderState (render-state [this {:keys [edit-state name grubs]}] - (let [update (om/get-shared owner :recipe-update) - add-grubs-ch (om/get-shared owner :recipe-add-grubs)] + (let [update (om/get-shared owner :recipe-update)] (html [:div.panel.panel-default.recipe-panel {:on-click @@ -92,7 +83,7 @@ {:type "button" :class (when (= edit-state :editing) "hidden") :ref :add-grubs-btn - :on-click #(add-grubs add-grubs-ch grubs)} + :on-click #(add-grubs (om/get-shared owner :add-grubs-ch) grubs)} [:span.glyphicon.glyphicon-plus] " Grubs"]] [:div.panel-body.recipe-grubs @@ -105,7 +96,7 @@ :on-change #(om/set-state! owner :grubs (dom/event-val %))}] [:button.btn.btn-danger.pull-left.recipe-remove-btn {:type "button" - :on-click #(put! (om/get-shared owner :recipe-remove) (remove-event id))} + :on-click #(put! remove-recipe-ch id)} [:span.glyphicon.glyphicon-trash]] [:button.btn.btn-primary.pull-right.recipe-done-btn {:type "button" diff --git a/src/cljs/grub/view/recipe_list.cljs b/src/cljs/grub/view/recipe_list.cljs index b16bc53..205493e 100644 --- a/src/cljs/grub/view/recipe_list.cljs +++ b/src/cljs/grub/view/recipe_list.cljs @@ -1,7 +1,7 @@ (ns grub.view.recipe-list (:require [om.core :as om :include-macros true] [sablono.core :as html :refer-macros [html]] - [cljs.core.async :as a :refer [