Complete/uncomplete grubs
This commit is contained in:
parent
cda4d2099e
commit
7bb5186e3d
3 changed files with 21 additions and 19 deletions
|
@ -38,9 +38,9 @@
|
|||
{mo/$set {:completed false}}))
|
||||
|
||||
(defmethod handle-event :update-grub [event]
|
||||
(mc/update @db grub-collection
|
||||
{:_id (:id event)}
|
||||
{mo/$set {:grub (:grub event)}}))
|
||||
(let [orig (mc/find-one-as-map @db grub-collection {:_id (:id event)})
|
||||
new (dissoc event :event-type :id)]
|
||||
(mc/update-by-id @db grub-collection (:id event) (merge orig new))))
|
||||
|
||||
(defmethod handle-event :clear-all-grubs [event]
|
||||
(clear-grubs))
|
||||
|
@ -60,13 +60,11 @@
|
|||
(println "Cannot handle unknown event:" event))
|
||||
|
||||
(defn get-current-grubs []
|
||||
(let [raw-grubs (mc/find-maps @db 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})))
|
||||
sorted-grubs)]
|
||||
grubs))
|
||||
(->> (mc/find-maps @db grub-collection)
|
||||
(sort-by :_id)
|
||||
(map #(select-keys % [:_id :grub :completed]))
|
||||
(map #(clojure.set/rename-keys % {:_id :id}))
|
||||
(vec)))
|
||||
|
||||
(defn get-current-recipes []
|
||||
(let [raw-recipes (mc/find-maps @db recipe-collection)
|
||||
|
|
|
@ -32,14 +32,10 @@
|
|||
add-grubs (make-add-grubs-map add-grub-events)]
|
||||
(assoc state :grubs (merge (:grubs state) add-grubs))))
|
||||
|
||||
(defmethod handle-event :complete-grub [event state]
|
||||
(assoc-in state [:grubs (:id event) :completed] true))
|
||||
|
||||
(defmethod handle-event :uncomplete-grub [event state]
|
||||
(assoc-in state [:grubs (:id event) :completed] false))
|
||||
|
||||
(defmethod handle-event :update-grub [event state]
|
||||
(assoc-in state [:grubs (:id event) :grub] (:grub event)))
|
||||
(let [new-grub-info (dissoc event :event-type)
|
||||
orig-grub (get-in state [:grubs (:id event)])]
|
||||
(assoc-in state [:grubs (:id event)] (merge orig-grub new-grub-info))))
|
||||
|
||||
(defmethod handle-event :clear-all-grubs [event state]
|
||||
(assoc state :grubs {}))
|
||||
|
|
|
@ -56,6 +56,11 @@
|
|||
(for [recipe (vals recipes)]
|
||||
(om/build recipe-view recipe))]]))))
|
||||
|
||||
(defn complete-event [{:keys [id completed]}]
|
||||
{:event :update-grub
|
||||
:id id
|
||||
:completed (not completed)})
|
||||
|
||||
(defn grub-view [grub-state owner]
|
||||
(reify
|
||||
om/IRender
|
||||
|
@ -64,7 +69,8 @@
|
|||
(html
|
||||
[:li.list-group-item.grub-item
|
||||
{:id id
|
||||
:class (when completed "completed")}
|
||||
:class (when completed "completed")
|
||||
:on-click #(put! (:update (om/get-shared owner)) (complete-event @grub-state))}
|
||||
[:span.grub-static
|
||||
(if completed
|
||||
[:span.glyphicon.glyphicon-check]
|
||||
|
@ -152,11 +158,13 @@
|
|||
|
||||
(defn render-app [state]
|
||||
(let [add (chan)
|
||||
update (chan)
|
||||
clear-all (chan)
|
||||
out (a/merge [add clear-all])]
|
||||
out (a/merge [add update clear-all])]
|
||||
(om/root app-view
|
||||
state
|
||||
{:target (.getElementById js/document "container")
|
||||
:shared {:add add
|
||||
:update update
|
||||
:clear-all clear-all}})
|
||||
out))
|
||||
|
|
Loading…
Reference in a new issue