Refactor to look sequential, use log macro, filter empty todos
This commit is contained in:
parent
e54c30633a
commit
34ea58e11b
2 changed files with 59 additions and 23 deletions
|
@ -1,20 +1,9 @@
|
||||||
(ns grub-client.core
|
(ns grub-client.core
|
||||||
;(:use-macros [dommy.macros :only [deftemplate sel1 node]])
|
|
||||||
(:require [dommy.core :as dommy]
|
(:require [dommy.core :as dommy]
|
||||||
[cljs.core.async :as async :refer [<! >! chan close! timeout]])
|
[cljs.core.async :as async :refer [<! >! chan close! timeout]])
|
||||||
(:require-macros [dommy.macros :refer [deftemplate sel1 node]]
|
(:require-macros [dommy.macros :refer [deftemplate sel1 node]]
|
||||||
[cljs.core.async.macros :as m :refer [go alt!]]))
|
[cljs.core.async.macros :as m :refer [go alt!]]
|
||||||
|
[grub-client.macros :refer [log]]))
|
||||||
(defn log [& args]
|
|
||||||
(apply #(.log js/console %) args))
|
|
||||||
|
|
||||||
(def test-grubs
|
|
||||||
["8 dl water"
|
|
||||||
"8 whole peppercorns"
|
|
||||||
"2 bay leaves"
|
|
||||||
"1 - 2 (150 g) onions"
|
|
||||||
"2 dl cream"
|
|
||||||
"1 dl dill"])
|
|
||||||
|
|
||||||
(deftemplate grub-template [grub]
|
(deftemplate grub-template [grub]
|
||||||
[:tr
|
[:tr
|
||||||
|
@ -42,20 +31,63 @@
|
||||||
(for [grub grubs] (grub-template grub))]]]
|
(for [grub grubs] (grub-template grub))]]]
|
||||||
[:div.col-lg-4]]])
|
[:div.col-lg-4]]])
|
||||||
|
|
||||||
(def add-grub-chan (chan))
|
(defn render-body [grubs]
|
||||||
|
(dommy/prepend! (sel1 :body) (main-template grubs)))
|
||||||
|
|
||||||
(defn on-add-grub-clicked [& args]
|
(defn push-new-grub [channel]
|
||||||
(let [new-grub (dommy/value add-grub-text)]
|
(let [new-grub (dommy/value add-grub-text)]
|
||||||
(dommy/set-value! add-grub-text "")
|
(dommy/set-value! add-grub-text "")
|
||||||
(go (>! add-grub-chan new-grub))))
|
(go (>! channel new-grub))))
|
||||||
|
|
||||||
(defn add-grub [grub]
|
(defn put-grubs-from-clicks [channel]
|
||||||
|
(dommy/listen! add-grub-btn :click #(push-new-grub channel)))
|
||||||
|
|
||||||
|
(defn put-grubs-if-enter-pressed [channel event]
|
||||||
|
(when (= (.-keyIdentifier event) "Enter")
|
||||||
|
(push-new-grub channel)))
|
||||||
|
|
||||||
|
(defn put-grubs-from-enter [channel]
|
||||||
|
(dommy/listen! add-grub-text
|
||||||
|
:keyup
|
||||||
|
(partial put-grubs-if-enter-pressed channel)))
|
||||||
|
|
||||||
|
(defn get-added-grubs []
|
||||||
|
(let [out (chan)]
|
||||||
|
(put-grubs-from-clicks out)
|
||||||
|
(put-grubs-from-enter out)
|
||||||
|
out))
|
||||||
|
|
||||||
|
(defn append-new-grub [grub]
|
||||||
(dommy/append! (sel1 :#grubList) (grub-template grub)))
|
(dommy/append! (sel1 :#grubList) (grub-template grub)))
|
||||||
|
|
||||||
(dommy/prepend! (sel1 :body) (main-template test-grubs))
|
(defn add-grubs-to-list [in]
|
||||||
(dommy/listen! add-grub-btn :click on-add-grub-clicked)
|
(go (while true
|
||||||
|
(let [new-grub (<! in)]
|
||||||
|
(log "Added grub: " new-grub)
|
||||||
|
(append-new-grub new-grub)))))
|
||||||
|
|
||||||
(go (while true
|
(defn filter-empty-grubs [in]
|
||||||
(let [new-grub (<! add-grub-chan)]
|
(let [out (chan)]
|
||||||
(log new-grub)
|
(go (while true
|
||||||
(add-grub new-grub))))
|
(let [grub (<! in)]
|
||||||
|
(when-not (empty? grub) (>! out grub)))))
|
||||||
|
out))
|
||||||
|
|
||||||
|
(defn add-new-grubs-to-list []
|
||||||
|
(let [added-grubs (get-added-grubs)
|
||||||
|
filtered-grubs (filter-empty-grubs added-grubs)]
|
||||||
|
(add-grubs-to-list filtered-grubs)))
|
||||||
|
|
||||||
|
(def test-grubs
|
||||||
|
["8 dl water"
|
||||||
|
"8 whole peppercorns"
|
||||||
|
"2 bay leaves"
|
||||||
|
"1 - 2 (150 g) onions"
|
||||||
|
"2 dl cream"
|
||||||
|
"1 dl dill"])
|
||||||
|
|
||||||
|
(defn init []
|
||||||
|
(render-body test-grubs)
|
||||||
|
(add-new-grubs-to-list))
|
||||||
|
|
||||||
|
(init)
|
||||||
|
|
4
src-cljs/grub_client/macros.clj
Normal file
4
src-cljs/grub_client/macros.clj
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
(ns grub-client.macros)
|
||||||
|
|
||||||
|
(defmacro log [& args]
|
||||||
|
`(.log js/console ~@args))
|
Loading…
Add table
Reference in a new issue