Simple alphabetical sorting

This commit is contained in:
Nicholas Kariniemi 2013-10-15 17:58:42 +03:00
parent 38935270e7
commit e5698f4ff4
2 changed files with 15 additions and 7 deletions

View file

@ -118,10 +118,9 @@
(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))]
(let [grub-list (sel1 :#grub-list)]
(aset grub-list "innerHTML" "")
(dommy/replace-contents! grub-list (grub-list-template sorted-grubs))))
(dommy/replace-contents! grub-list (grub-list-template grubs))))
(defn get-add-grub-text []
(dommy/value add-grub-text))

View file

@ -126,6 +126,15 @@
grub (grubs grub-index)]
[grub-index grub]))
(defn get-grub-ingredient [grub]
(let [text (clojure.string/lower-case (:grub grub))
match (re-find #"[a-z]{3}.*$" text)]
match))
(defn sort-and-render-grub-list! [grubs]
(let [sorted-grubs (sort-by (juxt :completed get-grub-ingredient) (vals grubs))]
(dom/render-grub-list sorted-grubs)))
(defmulti handle-event (fn [event grubs] (:event event))
:default :unknown-event)
@ -137,24 +146,24 @@
(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/render-grub-list new-grubs)
(sort-and-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))
new-grubs (assoc-in grubs [(:id event) :completed] true)]
(dom/render-grub-list new-grubs)
(sort-and-render-grub-list! new-grubs)
new-grubs))
(defmethod handle-event :uncomplete-grub [event grubs]
(let [new-grubs (assoc-in grubs [(:id event) :completed] false)]
(dom/render-grub-list new-grubs)
(sort-and-render-grub-list! new-grubs)
new-grubs))
(defmethod handle-event :update-grub [event grubs]
(let [new-grubs (assoc-in grubs [(:id event) :grub] (:grub event))]
(dom/render-grub-list new-grubs)
(sort-and-render-grub-list! new-grubs)
new-grubs))
(defmethod handle-event :clear-all-grubs [event grubs]