Add back sorting of grubs

This commit is contained in:
Nicholas Kariniemi 2013-10-08 22:16:53 +03:00
parent 81299b43dd
commit 955e853e93
7 changed files with 50 additions and 97 deletions

View file

@ -14,34 +14,37 @@
(defmethod handle-event :add-grub [event] (defmethod handle-event :add-grub [event]
(let [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))) (mc/insert grub-collection grub)))
(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)}
{mo/$set {:completed true}})) {mo/$set {:completed true}}))
(defmethod handle-event :uncomplete-grub [event] (defmethod handle-event :uncomplete-grub [event]
(mc/update grub-collection (mc/update grub-collection
{:_id (:_id event)} {:_id (:id event)}
{mo/$set {:completed false}})) {mo/$set {:completed false}}))
(defmethod handle-event :update-grub [event] (defmethod handle-event :update-grub [event]
(mc/update grub-collection (mc/update grub-collection
{:_id (:_id event)} {:_id (:id event)}
{mo/$set {:grub (:grub event)}})) {mo/$set {:grub (:grub event)}}))
(defmethod handle-event :clear-all-grubs [event] (defmethod handle-event :clear-all-grubs [event]
(clear-grubs)) (clear-grubs))
(defmethod handle-event :add-recipe [event] (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))) (mc/insert recipe-collection recipe)))
(defmethod handle-event :update-recipe [event] (defmethod handle-event :update-recipe [event]
(mc/update recipe-collection (mc/update recipe-collection
{:_id (:_id event)} {:_id (:id event)}
{mo/$set {:name (:name event) :grubs (:grubs event)}})) {mo/$set {:name (:name event) :grubs (:grubs event)}}))
(defmethod handle-event :unknown-event [event] (defmethod handle-event :unknown-event [event]
@ -52,6 +55,7 @@
sorted-grubs (sort-by :_id (vec grubs)) sorted-grubs (sort-by :_id (vec grubs))
events (map (fn [g] (-> g events (map (fn [g] (-> g
(select-keys [:_id :grub :completed]) (select-keys [:_id :grub :completed])
(clojure.set/rename-keys {:_id :id})
(assoc :event :add-grub))) (assoc :event :add-grub)))
sorted-grubs) sorted-grubs)
out (chan)] out (chan)]
@ -63,6 +67,7 @@
sorted-recipes (sort-by :_id (vec recipes)) sorted-recipes (sort-by :_id (vec recipes))
events (map (fn [r] (-> r events (map (fn [r] (-> r
(select-keys [:_id :name :grubs]) (select-keys [:_id :name :grubs])
(clojure.set/rename-keys {:_id :id})
(assoc :event :add-recipe))) (assoc :event :add-recipe)))
sorted-recipes) sorted-recipes)
out (chan)] out (chan)]

View file

@ -1,7 +1,6 @@
(ns grub.core (ns grub.core
(:require [grub.view :as view] (:require [grub.view :as view]
[grub.websocket :as ws] [grub.websocket :as ws]
[grub.state :as state]
[cljs.core.async :as a :refer [<! >! chan]] [cljs.core.async :as a :refer [<! >! chan]]
[cljs.reader]) [cljs.reader])
(:require-macros [grub.macros :refer [log logs go-loop]] (:require-macros [grub.macros :refer [log logs go-loop]]
@ -9,15 +8,9 @@
(defn wire-channels-together [] (defn wire-channels-together []
(let [to-remote (chan) (let [to-remote (chan)
;to-state (chan)
to-view (chan) to-view (chan)
from-remote (ws/get-remote-chan to-remote) from-remote (ws/get-remote-chan to-remote)
from-view (view/setup-and-get-view-events to-view)] 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-remote to-view)
(a/pipe from-view to-remote))) (a/pipe from-view to-remote)))

View file

@ -1,61 +0,0 @@
(ns grub.state
(:require [cljs.core.async :as a :refer [chan <!]])
(:require-macros [grub.macros :refer [log logs]]
[cljs.core.async.macros :refer [go go-loop]]))
(def grubs (atom []))
(defn get-grub-with-index [grubs id]
(let [grub-index (->> 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 (<! incoming-events)]
(handle-event event)
(recur))))

View file

@ -1,6 +1,5 @@
(ns grub.view (ns grub.view
(:require [grub.state :as state] (:require [grub.view.dom :as dom]
[grub.view.dom :as dom]
[grub.view.grub :as grub-view] [grub.view.grub :as grub-view]
[grub.view.recipe :as recipe-view] [grub.view.recipe :as recipe-view]
[dommy.core :as dommy] [dommy.core :as dommy]

View file

@ -111,9 +111,19 @@
new-recipe new-recipe
[:ul#recipe-list.list-group.recipe-list]]]]) [:ul#recipe-list.list-group.recipe-list]]]])
(deftemplate grub-list-template [grubs]
(node (for [grub grubs]
(make-grub-node (:id grub) (:grub grub) (:completed grub)))))
(defn render-body [] (defn render-body []
(dommy/prepend! (sel1 :body) (main-template))) (dommy/prepend! (sel1 :body) (main-template)))
(defn render-grub-list [grubs]
(let [grub-list (sel1 :#grub-list)
sorted-grubs (sort-by (juxt :completed :id) (vals grubs))]
(aset grub-list "innerHTML" "")
(dommy/replace-contents! grub-list (grub-list-template sorted-grubs))))
(defn get-add-grub-text [] (defn get-add-grub-text []
(dommy/value add-grub-text)) (dommy/value add-grub-text))
@ -207,14 +217,15 @@
(dommy/set-text! (sel1 elem ".grub-text") grub) (dommy/set-text! (sel1 elem ".grub-text") grub)
(dommy/set-value! (sel1 elem ".grub-input") grub))) (dommy/set-value! (sel1 elem ".grub-input") grub)))
(defn add-new-grub [id grub completed] (defn make-new-grub [id grub completed]
(let [node (make-grub-node id grub completed) (let [node (make-grub-node id grub completed)
grub (Grub. node id grub completed) grub (Grub. node id grub completed)
grub-list (sel1 :#grub-list)] grub-list (sel1 :#grub-list)]
(dommy/append! grub-list grub)
(dommy/set-value! (sel1 :#add-grub-input) "")
grub)) grub))
(defn clear-new-grub-input! []
(dommy/set-value! (sel1 :#add-grub-input) ""))
(extend-type js/HTMLDivElement (extend-type js/HTMLDivElement
IRecipe IRecipe
(-expand! [this] (-expand! [this]

View file

@ -21,7 +21,7 @@
(a/filter< #(not (empty? %))) (a/filter< #(not (empty? %)))
(a/map< (fn [g] (a/map< (fn [g]
{:event :add-grub {:event :add-grub
:_id (str "grub-" (.now js/Date)) :id (str "grub-" (.now js/Date))
:grub g :grub g
:completed false}))))) :completed false})))))
@ -29,7 +29,7 @@
(let [id (.-id elem) (let [id (.-id elem)
completed (dom/-completed? elem) completed (dom/-completed? elem)
event-type (if completed :uncomplete-grub :complete-grub)] event-type (if completed :uncomplete-grub :complete-grub)]
{:_id id {:id id
:event event-type})) :event event-type}))
(defn get-complete-events [] (defn get-complete-events []
@ -88,7 +88,7 @@
(when (not (= grub-text orig-grub-text)) (when (not (= grub-text orig-grub-text))
{:event :update-grub {:event :update-grub
:grub grub-text :grub grub-text
:_id id}))) :id id})))
(defn wait-for-update-event [grub] (defn wait-for-update-event [grub]
(let [out (chan) (let [out (chan)
@ -120,7 +120,7 @@
(defn get-grub-with-index [grubs id] (defn get-grub-with-index [grubs id]
(let [grub-index (->> grubs (let [grub-index (->> grubs
(map-indexed vector) (map-indexed vector)
(filter #(= (:_id (second %)) id)) (filter #(= (:id (second %)) id))
(first) (first)
(first)) (first))
grub (grubs grub-index)] grub (grubs grub-index)]
@ -134,22 +134,28 @@
grubs) grubs)
(defmethod handle-event :add-grub [event 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) (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] (defmethod handle-event :complete-grub [event grubs]
(let [grub (get grubs (:_id event))] (let [grub (get grubs (:id event))
(dom/-complete! grub) new-grubs (assoc-in grubs [(:id event) :completed] true)]
(assoc-in grubs [(:_id event) :completed] true))) (dom/render-grub-list new-grubs)
new-grubs))
(defmethod handle-event :uncomplete-grub [event grubs] (defmethod handle-event :uncomplete-grub [event grubs]
(dom/-uncomplete! (get grubs (:_id event))) (let [new-grubs (assoc-in grubs [(:id event) :completed] false)]
(assoc-in grubs [(:_id event) :completed] false)) (dom/render-grub-list new-grubs)
new-grubs))
(defmethod handle-event :update-grub [event grubs] (defmethod handle-event :update-grub [event grubs]
(dom/-update-grub! (get grubs (:_id event)) (:grub event)) (let [new-grubs (assoc-in grubs [(:id event) :grub] (:grub event))]
(assoc-in grubs [(:_id event) :grub] (:grub event))) (dom/render-grub-list new-grubs)
new-grubs))
(defmethod handle-event :clear-all-grubs [event grubs] (defmethod handle-event :clear-all-grubs [event grubs]
(dom/-hide! dom/clear-all-btn) (dom/-hide! dom/clear-all-btn)

View file

@ -24,7 +24,7 @@
{:event :add-recipe {:event :add-recipe
:name name :name name
:grubs grubs :grubs grubs
:_id id})))) :id id}))))
(defn wait-for-create-event [] (defn wait-for-create-event []
(let [out (chan) (let [out (chan)
@ -69,7 +69,7 @@
{:event :update-recipe {:event :update-recipe
:name name :name name
:grubs grubs :grubs grubs
:_id id}))) :id id})))
(defn wait-for-update-event [elem] (defn wait-for-update-event [elem]
(let [out (chan) (let [out (chan)
@ -110,7 +110,7 @@
id (.-id elem) id (.-id elem)
grubs (dom/-get-grubs elem) grubs (dom/-get-grubs elem)
events (map-indexed (fn [index g] {:event :add-grub 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 :grub g
:completed false}) grubs)] :completed false}) grubs)]
(a/onto-chan out events false)) (a/onto-chan out events false))
@ -126,13 +126,13 @@
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)))
(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
(assoc :name (:name event)) (assoc :name (:name event))
(assoc :grubs (:grubs event)))] (assoc :grubs (:grubs event)))]