diff --git a/src/clj/grub/db.clj b/src/clj/grub/db.clj index 1d4cec6..b450196 100644 --- a/src/clj/grub/db.clj +++ b/src/clj/grub/db.clj @@ -14,34 +14,37 @@ (defmethod handle-event :add-grub [event] (let [grub (-> event - (select-keys [:_id :grub :completed]))] + (select-keys [:id :grub :completed]) + (clojure.set/rename-keys {:id :_id}))] (mc/insert grub-collection grub))) (defmethod handle-event :complete-grub [event] (mc/update grub-collection - {:_id (:_id event)} + {:_id (:id event)} {mo/$set {:completed true}})) (defmethod handle-event :uncomplete-grub [event] (mc/update grub-collection - {:_id (:_id event)} + {:_id (:id event)} {mo/$set {:completed false}})) (defmethod handle-event :update-grub [event] (mc/update grub-collection - {:_id (:_id event)} + {:_id (:id event)} {mo/$set {:grub (:grub event)}})) (defmethod handle-event :clear-all-grubs [event] (clear-grubs)) (defmethod handle-event :add-recipe [event] - (let [recipe (select-keys event [:_id :name :steps])] + (let [recipe (-> event + (select-keys [:id :name :steps]) + (clojure.set/rename-keys {:id :_id}))] (mc/insert recipe-collection recipe))) (defmethod handle-event :update-recipe [event] (mc/update recipe-collection - {:_id (:_id event)} + {:_id (:id event)} {mo/$set {:name (:name event) :grubs (:grubs event)}})) (defmethod handle-event :unknown-event [event] @@ -52,6 +55,7 @@ sorted-grubs (sort-by :_id (vec grubs)) events (map (fn [g] (-> g (select-keys [:_id :grub :completed]) + (clojure.set/rename-keys {:_id :id}) (assoc :event :add-grub))) sorted-grubs) out (chan)] @@ -63,6 +67,7 @@ sorted-recipes (sort-by :_id (vec recipes)) events (map (fn [r] (-> r (select-keys [:_id :name :grubs]) + (clojure.set/rename-keys {:_id :id}) (assoc :event :add-recipe))) sorted-recipes) out (chan)] diff --git a/src/cljs/grub/core.cljs b/src/cljs/grub/core.cljs index b8a9730..23dcd22 100644 --- a/src/cljs/grub/core.cljs +++ b/src/cljs/grub/core.cljs @@ -1,7 +1,6 @@ (ns grub.core (:require [grub.view :as view] [grub.websocket :as ws] - [grub.state :as state] [cljs.core.async :as a :refer [! chan]] [cljs.reader]) (:require-macros [grub.macros :refer [log logs go-loop]] @@ -9,15 +8,9 @@ (defn wire-channels-together [] (let [to-remote (chan) - ;to-state (chan) to-view (chan) from-remote (ws/get-remote-chan to-remote) from-view (view/setup-and-get-view-events to-view)] - ;(state/handle-incoming-events to-state) - ;; (a/tap from-view to-state) - ;; (a/tap from-view to-remote) - ;(a/tap from-remote to-state))) - ;(a/tap from-remote to-view))) (a/pipe from-remote to-view) (a/pipe from-view to-remote))) diff --git a/src/cljs/grub/state.cljs b/src/cljs/grub/state.cljs deleted file mode 100644 index 868afbe..0000000 --- a/src/cljs/grub/state.cljs +++ /dev/null @@ -1,61 +0,0 @@ -(ns grub.state - (:require [cljs.core.async :as a :refer [chan > grubs - (map-indexed vector) - (filter #(= (:_id (second %)) id)) - (first) - (first)) - grub (grubs grub-index)] - [grub-index grub])) - -(defmulti handle-event :event :default :unknown-event) - -(defmethod handle-event :add [event] - (logs "handle event add:" event) - (let [grub (select-keys event [:_id :grub :completed])] - (swap! grubs (fn [current] (conj current grub))))) - -(defmethod handle-event :complete [event] - (swap! grubs - (fn [current] - (let [[grub-index grub] (get-grub-with-index current (:_id event)) - completed-grub (assoc grub :completed true)] - (assoc current grub-index completed-grub))))) - -(defmethod handle-event :uncomplete [event] - (swap! grubs - (fn [current] - (let [[grub-index grub] (get-grub-with-index current (:_id event)) - incomplete-grub (assoc grub :completed false)] - (assoc current grub-index incomplete-grub))))) - -(defmethod handle-event :update [event] - (swap! grubs - (fn [current] - (let [[grub-index grub] (get-grub-with-index current (:_id event)) - updated-grub (assoc grub :grub (:grub event))] - (assoc current grub-index updated-grub))))) - -(defmethod handle-event :delete [event] - (swap! grubs - (fn [current] - (vec (remove #(= (:_id %) (:_id event)) current))))) - -(defmethod handle-event :clear-all [event] - (reset! grubs [])) - -(defmethod handle-event :unknown-event [event] - (logs "Cannot handle unknown event:" event)) - -(defn handle-incoming-events [incoming-events] - (go-loop [] - (let [event (> grubs (map-indexed vector) - (filter #(= (:_id (second %)) id)) + (filter #(= (:id (second %)) id)) (first) (first)) grub (grubs grub-index)] @@ -134,22 +134,28 @@ grubs) (defmethod handle-event :add-grub [event grubs] - (let [grub (dom/add-new-grub (:_id event) (:grub event) (:completed event))] + (let [grub (dom/make-new-grub (:id event) (:grub event) (:completed event)) + new-grubs (assoc grubs (:id grub) grub)] (dom/-show! dom/clear-all-btn) - (assoc grubs (:id grub) grub))) + (dom/render-grub-list new-grubs) + (dom/clear-new-grub-input!) + new-grubs)) (defmethod handle-event :complete-grub [event grubs] - (let [grub (get grubs (:_id event))] - (dom/-complete! grub) - (assoc-in grubs [(:_id event) :completed] true))) + (let [grub (get grubs (:id event)) + new-grubs (assoc-in grubs [(:id event) :completed] true)] + (dom/render-grub-list new-grubs) + new-grubs)) (defmethod handle-event :uncomplete-grub [event grubs] - (dom/-uncomplete! (get grubs (:_id event))) - (assoc-in grubs [(:_id event) :completed] false)) + (let [new-grubs (assoc-in grubs [(:id event) :completed] false)] + (dom/render-grub-list new-grubs) + new-grubs)) (defmethod handle-event :update-grub [event grubs] - (dom/-update-grub! (get grubs (:_id event)) (:grub event)) - (assoc-in grubs [(:_id event) :grub] (:grub event))) + (let [new-grubs (assoc-in grubs [(:id event) :grub] (:grub event))] + (dom/render-grub-list new-grubs) + new-grubs)) (defmethod handle-event :clear-all-grubs [event grubs] (dom/-hide! dom/clear-all-btn) diff --git a/src/cljs/grub/view/recipe.cljs b/src/cljs/grub/view/recipe.cljs index 1484677..6e96de1 100644 --- a/src/cljs/grub/view/recipe.cljs +++ b/src/cljs/grub/view/recipe.cljs @@ -24,7 +24,7 @@ {:event :add-recipe :name name :grubs grubs - :_id id})))) + :id id})))) (defn wait-for-create-event [] (let [out (chan) @@ -69,7 +69,7 @@ {:event :update-recipe :name name :grubs grubs - :_id id}))) + :id id}))) (defn wait-for-update-event [elem] (let [out (chan) @@ -110,7 +110,7 @@ id (.-id elem) grubs (dom/-get-grubs elem) events (map-indexed (fn [index g] {:event :add-grub - :_id (str "grub-" (.now js/Date) index) + :id (str "grub-" (.now js/Date) index) :grub g :completed false}) grubs)] (a/onto-chan out events false)) @@ -126,13 +126,13 @@ recipes) (defmethod handle-event :add-recipe [event recipes] - (let [recipe (dom/add-new-recipe (:_id event) + (let [recipe (dom/add-new-recipe (:id event) (:name event) (:grubs event))] (assoc recipes (:id recipe) recipe))) (defmethod handle-event :update-recipe [event recipes] - (let [recipe (get recipes (:_id event)) + (let [recipe (get recipes (:id event)) updated-recipe (-> recipe (assoc :name (:name event)) (assoc :grubs (:grubs event)))]