Add back sorting of grubs
This commit is contained in:
parent
81299b43dd
commit
955e853e93
7 changed files with 50 additions and 97 deletions
|
@ -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)]
|
||||
|
|
|
@ -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)))
|
||||
|
||||
|
|
|
@ -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))))
|
|
@ -1,6 +1,5 @@
|
|||
(ns grub.view
|
||||
(:require [grub.state :as state]
|
||||
[grub.view.dom :as dom]
|
||||
(:require [grub.view.dom :as dom]
|
||||
[grub.view.grub :as grub-view]
|
||||
[grub.view.recipe :as recipe-view]
|
||||
[dommy.core :as dommy]
|
||||
|
|
|
@ -111,9 +111,19 @@
|
|||
new-recipe
|
||||
[: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 []
|
||||
(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 []
|
||||
(dommy/value add-grub-text))
|
||||
|
||||
|
@ -207,14 +217,15 @@
|
|||
(dommy/set-text! (sel1 elem ".grub-text") 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)
|
||||
grub (Grub. node id grub completed)
|
||||
grub-list (sel1 :#grub-list)]
|
||||
(dommy/append! grub-list grub)
|
||||
(dommy/set-value! (sel1 :#add-grub-input) "")
|
||||
grub))
|
||||
|
||||
(defn clear-new-grub-input! []
|
||||
(dommy/set-value! (sel1 :#add-grub-input) ""))
|
||||
|
||||
(extend-type js/HTMLDivElement
|
||||
IRecipe
|
||||
(-expand! [this]
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
(a/filter< #(not (empty? %)))
|
||||
(a/map< (fn [g]
|
||||
{:event :add-grub
|
||||
:_id (str "grub-" (.now js/Date))
|
||||
:id (str "grub-" (.now js/Date))
|
||||
:grub g
|
||||
:completed false})))))
|
||||
|
||||
|
@ -29,7 +29,7 @@
|
|||
(let [id (.-id elem)
|
||||
completed (dom/-completed? elem)
|
||||
event-type (if completed :uncomplete-grub :complete-grub)]
|
||||
{:_id id
|
||||
{:id id
|
||||
:event event-type}))
|
||||
|
||||
(defn get-complete-events []
|
||||
|
@ -88,7 +88,7 @@
|
|||
(when (not (= grub-text orig-grub-text))
|
||||
{:event :update-grub
|
||||
:grub grub-text
|
||||
:_id id})))
|
||||
:id id})))
|
||||
|
||||
(defn wait-for-update-event [grub]
|
||||
(let [out (chan)
|
||||
|
@ -120,7 +120,7 @@
|
|||
(defn get-grub-with-index [grubs id]
|
||||
(let [grub-index (->> 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)
|
||||
|
|
|
@ -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)))]
|
||||
|
|
Loading…
Reference in a new issue