Event to add grubs/recipes as list

This commit is contained in:
Nicholas Kariniemi 2013-10-21 22:56:23 +03:00
parent 42c02397a0
commit 48ccf9e447
5 changed files with 83 additions and 46 deletions

View file

@ -12,12 +12,19 @@
(defmulti handle-event :event :default :unknown-event) (defmulti handle-event :event :default :unknown-event)
(defmethod handle-event :add-grub [event] (defn insert-grub [event]
(let [grub (-> event (let [grub (-> event
(select-keys [:id :grub :completed]) (select-keys [:id :grub :completed])
(clojure.set/rename-keys {:id :_id}))] (clojure.set/rename-keys {:id :_id}))]
(mc/insert grub-collection grub))) (mc/insert grub-collection grub)))
(defmethod handle-event :add-grub [event]
(insert-grub event))
(defmethod handle-event :add-grub-list [event]
(doseq [grub-event (:grubs event)]
(insert-grub grub-event)))
(defmethod handle-event :complete-grub [event] (defmethod handle-event :complete-grub [event]
(mc/update grub-collection (mc/update grub-collection
{:_id (:id event)} {:_id (:id event)}
@ -50,29 +57,23 @@
(defmethod handle-event :unknown-event [event] (defmethod handle-event :unknown-event [event]
(println "Cannot handle unknown event:" event)) (println "Cannot handle unknown event:" event))
(defn get-current-grubs-as-events [] (defn get-current-grubs []
(let [grubs (mc/find-maps grub-collection) (let [raw-grubs (mc/find-maps grub-collection)
sorted-grubs (sort-by :_id (vec grubs)) sorted-grubs (sort-by :_id (vec raw-grubs))
events (map (fn [g] (-> g grubs (map (fn [g] (-> g
(select-keys [:_id :grub :completed]) (select-keys [:_id :grub :completed])
(clojure.set/rename-keys {:_id :id}) (clojure.set/rename-keys {:_id :id})))
(assoc :event :add-grub))) sorted-grubs)]
sorted-grubs) grubs))
out (chan)]
(a/onto-chan out events)
out))
(defn get-current-recipes-as-events [] (defn get-current-recipes []
(let [recipes (mc/find-maps recipe-collection) (let [raw-recipes (mc/find-maps recipe-collection)
sorted-recipes (sort-by :_id (vec recipes)) sorted-recipes (sort-by :_id (vec raw-recipes))
events (map (fn [r] (-> r recipes (map (fn [r] (-> r
(select-keys [:_id :name :grubs]) (select-keys [:_id :name :grubs])
(clojure.set/rename-keys {:_id :id}) (clojure.set/rename-keys {:_id :id})))
(assoc :event :add-recipe))) sorted-recipes)]
sorted-recipes) recipes))
out (chan)]
(a/onto-chan out events)
out))
(def production-db "grub") (def production-db "grub")
(def development-db "grub-dev") (def development-db "grub-dev")

View file

@ -43,8 +43,14 @@
(defn send-current-grubs-and-recipes-to-client [client-chan] (defn send-current-grubs-and-recipes-to-client [client-chan]
(a/pipe (db/get-current-grubs-as-events) client-chan false) (let [add-grubs-event {:event :add-grub-list
(a/pipe (db/get-current-recipes-as-events) client-chan false)) :grubs (db/get-current-grubs)}
add-recipes-event {:event :add-recipe-list
:recipes (db/get-current-recipes)}]
(go (>! client-chan add-grubs-event)
(>! client-chan add-recipes-event))))
;(a/pipe (db/get-current-grubs-as-events) client-chan false)
;(a/pipe (db/get-current-recipes-as-events) client-chan false))
(defn setup-new-connection [ws-channel] (defn setup-new-connection [ws-channel]
(let [[ws-channel-id client-chan] (add-connected-client! ws-channel)] (let [[ws-channel-id client-chan] (add-connected-client! ws-channel)]

View file

@ -39,6 +39,13 @@
{:unlisten unlisten {:unlisten unlisten
:chan (a/filter< #(= (.-keyIdentifier %) "Enter") c)})) :chan (a/filter< #(= (.-keyIdentifier %) "Enter") c)}))
(defn get-ctrl-enters []
(let [{c :chan unlisten :unlisten} (listen (sel1 :body) :keyup)
filtered-chan (a/filter< #(and (= (.-keyIdentifier %) "Enter")
(.-ctrlKey %))
c)]
{:chan filtered-chan :unlisten unlisten}))
(defn get-body-enters [] (defn get-body-enters []
(get-enters (sel1 :body))) (get-enters (sel1 :body)))
@ -282,7 +289,7 @@
(dommy/set-value! (sel1 this :#recipe-name) name) (dommy/set-value! (sel1 this :#recipe-name) name)
(dommy/set-text! (sel1 this :#recipe-grubs) grubs))) (dommy/set-text! (sel1 this :#recipe-grubs) grubs)))
(defn add-new-recipe [id name grubs] (defn add-new-recipe! [id name grubs]
(let [node (make-recipe-node id name grubs) (let [node (make-recipe-node id name grubs)
recipe (Recipe. node id name grubs) recipe (Recipe. node id name grubs)
recipe-list (sel1 :#recipe-list)] recipe-list (sel1 :#recipe-list)]

View file

@ -142,6 +142,21 @@
(dom/clear-new-grub-input!) (dom/clear-new-grub-input!)
new-grubs)) new-grubs))
(defn assoc-new-grub [current new]
(assoc current (:id new)
(dom/make-new-grub (:id new) (:grub new) (:completed new))))
(defn make-add-grubs-map [grub-events]
(reduce assoc-new-grub {} grub-events))
(defmethod handle-event :add-grub-list [event grubs]
(let [add-grub-events (:grubs event)
add-grubs (make-add-grubs-map add-grub-events)
new-grubs (merge grubs add-grubs)]
(dom/-show! dom/clear-all-btn)
(sort-and-render-grub-list! new-grubs)
new-grubs))
(defmethod handle-event :complete-grub [event grubs] (defmethod handle-event :complete-grub [event grubs]
(let [grub (get grubs (:id event)) (let [grub (get grubs (:id event))
new-grubs (assoc-in grubs [(:id event) :completed] true)] new-grubs (assoc-in grubs [(:id event) :completed] true)]

View file

@ -9,13 +9,6 @@
(defn wait-for-new-recipe-input-click [] (defn wait-for-new-recipe-input-click []
(:chan (dom/listen-once dom/new-recipe :click))) (:chan (dom/listen-once dom/new-recipe :click)))
(defn get-ctrl-enters []
(let [{c :chan unlisten :unlisten} (dom/listen (sel1 :body) :keyup)
filtered-chan (a/filter< #(and (= (.-keyIdentifier %) "Enter")
(.-ctrlKey %))
c)]
{:chan filtered-chan :unlisten unlisten}))
(defn parse-new-recipe-event [] (defn parse-new-recipe-event []
(let [name (dom/-get-name dom/new-recipe) (let [name (dom/-get-name dom/new-recipe)
grubs (dom/-get-grubs-str dom/new-recipe)] grubs (dom/-get-grubs-str dom/new-recipe)]
@ -29,7 +22,7 @@
(defn wait-for-create-event [] (defn wait-for-create-event []
(let [out (chan) (let [out (chan)
{ctrl-enters :chan {ctrl-enters :chan
ctrl-enters-unlisten :unlisten} (get-ctrl-enters) ctrl-enters-unlisten :unlisten} (dom/get-ctrl-enters)
{away-clicks :chan {away-clicks :chan
away-clicks-unlisten :unlisten} (dom/get-away-clicks dom/new-recipe) away-clicks-unlisten :unlisten} (dom/get-away-clicks dom/new-recipe)
{done-clicks :chan {done-clicks :chan
@ -40,9 +33,9 @@
(ctrl-enters-unlisten) (ctrl-enters-unlisten)
(away-clicks-unlisten) (away-clicks-unlisten)
(done-clicks-unlisten) (done-clicks-unlisten)
(if-let [event (parse-new-recipe-event)] (when-let [event (parse-new-recipe-event)]
(>! out event) (>! out event))
(a/close! out))) (a/close! out))
out)) out))
(defn get-create-events [] (defn get-create-events []
@ -75,7 +68,7 @@
(defn wait-for-update-event [elem] (defn wait-for-update-event [elem]
(let [out (chan) (let [out (chan)
{ctrl-enters :chan {ctrl-enters :chan
ctrl-enters-unlisten :unlisten} (get-ctrl-enters) ctrl-enters-unlisten :unlisten} (dom/get-ctrl-enters)
{away-clicks :chan {away-clicks :chan
away-clicks-unlisten :unlisten} (dom/get-away-clicks elem) away-clicks-unlisten :unlisten} (dom/get-away-clicks elem)
{done-clicks :chan {done-clicks :chan
@ -86,9 +79,9 @@
(ctrl-enters-unlisten) (ctrl-enters-unlisten)
(away-clicks-unlisten) (away-clicks-unlisten)
(done-clicks-unlisten) (done-clicks-unlisten)
(if-let [event (parse-update-recipe-event elem)] (when-let [event (parse-update-recipe-event elem)]
(>! out event) (>! out event))
(a/close! out))) (a/close! out))
out)) out))
(defn get-update-events [] (defn get-update-events []
@ -109,12 +102,14 @@
(let [e (<! clicks) (let [e (<! clicks)
elem (dommy/closest (.-selectedTarget e) :.recipe-panel) elem (dommy/closest (.-selectedTarget e) :.recipe-panel)
id (.-id elem) id (.-id elem)
grubs (dom/-get-grubs elem) grub-texts (dom/-get-grubs elem)
events (map-indexed (fn [index g] {:event :add-grub grubs (map-indexed (fn [index g] {:id (str "grub-" (.now js/Date) index)
:id (str "grub-" (.now js/Date) index) :grub g
:grub g :completed false})
:completed false}) grubs)] grub-texts)
(a/onto-chan out events false)) event {:event :add-grub-list
:grubs grubs}]
(>! out event))
(recur)) (recur))
out)) out))
@ -127,11 +122,24 @@
recipes) recipes)
(defmethod handle-event :add-recipe [event 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) (:name event)
(:grubs event))] (:grubs event))]
(assoc recipes (:id recipe) recipe))) (assoc recipes (:id recipe) recipe)))
(defn assoc-new-recipe! [current new]
(assoc current (:id new)
(dom/add-new-recipe! (:id new) (:name new) (:grubs new))))
(defn add-new-recipes! [recipe-events]
(reduce assoc-new-recipe! {} recipe-events))
(defmethod handle-event :add-recipe-list [event recipes]
(let [add-recipe-events (:recipes event)
added-recipes (add-new-recipes! add-recipe-events)
new-recipes (merge recipes added-recipes)]
new-recipes))
(defmethod handle-event :update-recipe [event recipes] (defmethod handle-event :update-recipe [event recipes]
(let [recipe (get recipes (:id event)) (let [recipe (get recipes (:id event))
updated-recipe (-> recipe updated-recipe (-> recipe