Event to add grubs/recipes as list
This commit is contained in:
parent
42c02397a0
commit
48ccf9e447
5 changed files with 83 additions and 46 deletions
|
@ -12,12 +12,19 @@
|
|||
|
||||
(defmulti handle-event :event :default :unknown-event)
|
||||
|
||||
(defmethod handle-event :add-grub [event]
|
||||
(defn insert-grub [event]
|
||||
(let [grub (-> event
|
||||
(select-keys [:id :grub :completed])
|
||||
(clojure.set/rename-keys {:id :_id}))]
|
||||
(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]
|
||||
(mc/update grub-collection
|
||||
{:_id (:id event)}
|
||||
|
@ -50,29 +57,23 @@
|
|||
(defmethod handle-event :unknown-event [event]
|
||||
(println "Cannot handle unknown event:" event))
|
||||
|
||||
(defn get-current-grubs-as-events []
|
||||
(let [grubs (mc/find-maps grub-collection)
|
||||
sorted-grubs (sort-by :_id (vec grubs))
|
||||
events (map (fn [g] (-> g
|
||||
(defn get-current-grubs []
|
||||
(let [raw-grubs (mc/find-maps grub-collection)
|
||||
sorted-grubs (sort-by :_id (vec raw-grubs))
|
||||
grubs (map (fn [g] (-> g
|
||||
(select-keys [:_id :grub :completed])
|
||||
(clojure.set/rename-keys {:_id :id})
|
||||
(assoc :event :add-grub)))
|
||||
sorted-grubs)
|
||||
out (chan)]
|
||||
(a/onto-chan out events)
|
||||
out))
|
||||
(clojure.set/rename-keys {:_id :id})))
|
||||
sorted-grubs)]
|
||||
grubs))
|
||||
|
||||
(defn get-current-recipes-as-events []
|
||||
(let [recipes (mc/find-maps recipe-collection)
|
||||
sorted-recipes (sort-by :_id (vec recipes))
|
||||
events (map (fn [r] (-> r
|
||||
(defn get-current-recipes []
|
||||
(let [raw-recipes (mc/find-maps recipe-collection)
|
||||
sorted-recipes (sort-by :_id (vec raw-recipes))
|
||||
recipes (map (fn [r] (-> r
|
||||
(select-keys [:_id :name :grubs])
|
||||
(clojure.set/rename-keys {:_id :id})
|
||||
(assoc :event :add-recipe)))
|
||||
sorted-recipes)
|
||||
out (chan)]
|
||||
(a/onto-chan out events)
|
||||
out))
|
||||
(clojure.set/rename-keys {:_id :id})))
|
||||
sorted-recipes)]
|
||||
recipes))
|
||||
|
||||
(def production-db "grub")
|
||||
(def development-db "grub-dev")
|
||||
|
|
|
@ -43,8 +43,14 @@
|
|||
|
||||
|
||||
(defn send-current-grubs-and-recipes-to-client [client-chan]
|
||||
(a/pipe (db/get-current-grubs-as-events) client-chan false)
|
||||
(a/pipe (db/get-current-recipes-as-events) client-chan false))
|
||||
(let [add-grubs-event {:event :add-grub-list
|
||||
: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]
|
||||
(let [[ws-channel-id client-chan] (add-connected-client! ws-channel)]
|
||||
|
|
|
@ -39,6 +39,13 @@
|
|||
{:unlisten unlisten
|
||||
: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 []
|
||||
(get-enters (sel1 :body)))
|
||||
|
||||
|
@ -282,7 +289,7 @@
|
|||
(dommy/set-value! (sel1 this :#recipe-name) name)
|
||||
(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)
|
||||
recipe (Recipe. node id name grubs)
|
||||
recipe-list (sel1 :#recipe-list)]
|
||||
|
|
|
@ -142,6 +142,21 @@
|
|||
(dom/clear-new-grub-input!)
|
||||
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]
|
||||
(let [grub (get grubs (:id event))
|
||||
new-grubs (assoc-in grubs [(:id event) :completed] true)]
|
||||
|
|
|
@ -9,13 +9,6 @@
|
|||
(defn wait-for-new-recipe-input-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 []
|
||||
(let [name (dom/-get-name dom/new-recipe)
|
||||
grubs (dom/-get-grubs-str dom/new-recipe)]
|
||||
|
@ -29,7 +22,7 @@
|
|||
(defn wait-for-create-event []
|
||||
(let [out (chan)
|
||||
{ctrl-enters :chan
|
||||
ctrl-enters-unlisten :unlisten} (get-ctrl-enters)
|
||||
ctrl-enters-unlisten :unlisten} (dom/get-ctrl-enters)
|
||||
{away-clicks :chan
|
||||
away-clicks-unlisten :unlisten} (dom/get-away-clicks dom/new-recipe)
|
||||
{done-clicks :chan
|
||||
|
@ -40,9 +33,9 @@
|
|||
(ctrl-enters-unlisten)
|
||||
(away-clicks-unlisten)
|
||||
(done-clicks-unlisten)
|
||||
(if-let [event (parse-new-recipe-event)]
|
||||
(>! out event)
|
||||
(a/close! out)))
|
||||
(when-let [event (parse-new-recipe-event)]
|
||||
(>! out event))
|
||||
(a/close! out))
|
||||
out))
|
||||
|
||||
(defn get-create-events []
|
||||
|
@ -75,7 +68,7 @@
|
|||
(defn wait-for-update-event [elem]
|
||||
(let [out (chan)
|
||||
{ctrl-enters :chan
|
||||
ctrl-enters-unlisten :unlisten} (get-ctrl-enters)
|
||||
ctrl-enters-unlisten :unlisten} (dom/get-ctrl-enters)
|
||||
{away-clicks :chan
|
||||
away-clicks-unlisten :unlisten} (dom/get-away-clicks elem)
|
||||
{done-clicks :chan
|
||||
|
@ -86,9 +79,9 @@
|
|||
(ctrl-enters-unlisten)
|
||||
(away-clicks-unlisten)
|
||||
(done-clicks-unlisten)
|
||||
(if-let [event (parse-update-recipe-event elem)]
|
||||
(>! out event)
|
||||
(a/close! out)))
|
||||
(when-let [event (parse-update-recipe-event elem)]
|
||||
(>! out event))
|
||||
(a/close! out))
|
||||
out))
|
||||
|
||||
(defn get-update-events []
|
||||
|
@ -109,12 +102,14 @@
|
|||
(let [e (<! clicks)
|
||||
elem (dommy/closest (.-selectedTarget e) :.recipe-panel)
|
||||
id (.-id elem)
|
||||
grubs (dom/-get-grubs elem)
|
||||
events (map-indexed (fn [index g] {:event :add-grub
|
||||
:id (str "grub-" (.now js/Date) index)
|
||||
:grub g
|
||||
:completed false}) grubs)]
|
||||
(a/onto-chan out events false))
|
||||
grub-texts (dom/-get-grubs elem)
|
||||
grubs (map-indexed (fn [index g] {:id (str "grub-" (.now js/Date) index)
|
||||
:grub g
|
||||
:completed false})
|
||||
grub-texts)
|
||||
event {:event :add-grub-list
|
||||
:grubs grubs}]
|
||||
(>! out event))
|
||||
(recur))
|
||||
out))
|
||||
|
||||
|
@ -127,11 +122,24 @@
|
|||
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)))
|
||||
|
||||
(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]
|
||||
(let [recipe (get recipes (:id event))
|
||||
updated-recipe (-> recipe
|
||||
|
|
Loading…
Reference in a new issue