todomvc closer

This commit is contained in:
mike-thompson-day8 2015-03-03 01:03:23 +11:00
parent 95de45a252
commit a00f9f67ad
1 changed files with 27 additions and 31 deletions

View File

@ -8,17 +8,15 @@
path trim-v debug]])) path trim-v debug]]))
;; TODOs ;; TODOs
;; add .gitignore
;; Get preoject.cljs up to speed `lein run` lein debug` ;; Get preoject.cljs up to speed `lein run` lein debug`
;; split into files view, handlers, subs, middleware ;; split into files view, handlers, subs, middleware
;; load todos off localstorage via merge ... and write back ;; load todos off localstorage via merge ... and write back
;; Show off debugging capabiliteis ;; Show off debugging capabiliteis
;; Add Prismatic schema - modules called state ;; Add Prismatic schema - modules called state
;; change example.html to todo.html ;; add middleware to save to local storage
;; add middleware to save to local storage - beware using
;; check that (path ) does an identity test before swaping back in
;; -- Helpers ------------- ;; -- Helpers -------------
(enable-console-print!) (enable-console-print!)
(defn next-id (defn next-id
@ -37,8 +35,9 @@
(defn completed-count (defn completed-count
"return the count of todos which have a :done of true"
[todos] [todos]
(count (filter :done todos))) (count (filter :done (vals todos))))
;; -- Middleware ------------- ;; -- Middleware -------------
;; ;;
@ -63,23 +62,22 @@
;; ;;
;; Middleware means our handlers are pure and simple. ;; Middleware means our handlers are pure and simple.
;; ;;
(def todo-middleware (comp (path [:todos]) (def todo-middleware [(path [:todos]) debug trim-v])
debug
trim-v ))
;; -- Handlers ------------- ;; -- Handlers -------------
;; we dispatch this event on program startup - responsible for initialising-db ;; we dispatch this event on program startup - responsible for initialising-db
(register-pure-handler (register-pure-handler
:initialise-db :initialise-db
(fn [debug] ;; middleware
(fn
[_ _] [_ _]
{:todos (sorted-map) {:todos (sorted-map)
:showing :all})) :showing :all}))
(register-pure-handler (register-pure-handler
:set-showing :set-showing
(comp debug trim-v) [debug trim-v] ;; middleware
(fn (fn
[db [filter-kw]] [db [filter-kw]]
(assoc db :showing filter-kw))) (assoc db :showing filter-kw)))
@ -93,13 +91,16 @@
(assoc todos id {:id id :title text :done false})))) (assoc todos id {:id id :title text :done false}))))
(register-pure-handler (register-pure-handler
:complete-all :complete-all-toggle
todo-middleware todo-middleware
(fn (fn
[todos [val]] [todos []]
(reduce #(assoc-in %1 [%2 :done] val) (let [val (every? (comp true? :done) (vals todos))]
todos (println "todos " todos)
(keys todos)))) (println "val " val)
(reduce #(assoc-in %1 [%2 :done] val)
todos
(keys todos)))))
(register-pure-handler (register-pure-handler
:toggle-done :toggle-done
@ -128,13 +129,12 @@
(fn (fn
[todos _] [todos _]
(->> (vals todos) (->> (vals todos)
(remove :done) (filter :done)
(map :id) (map :id)
(reduce dissoc todos)))) (reduce dissoc todos))))
;; -- Subscriptions --------- ;; -- Subscriptions ---------
(register-sub (register-sub
:initialised? :initialised?
(fn (fn
@ -145,11 +145,8 @@
:visible-todos :visible-todos
(fn (fn
[db _] [db _]
(let [todos (reaction (:todos @db)) (reaction (filter (filter-fn-for (:showing @db))
showing (reaction (:showing @db))] (vals (:todos @db))))))
(reaction (filter (filter-fn-for @showing)
(vals @todos))))))
(register-sub (register-sub
:completed-count :completed-count
@ -181,7 +178,6 @@
[active-count completed-count showing])))) ;; tuple [active-count completed-count showing])))) ;; tuple
;; -- Components --------- ;; -- Components ---------
@ -190,8 +186,8 @@
stop #(do (reset! val "") stop #(do (reset! val "")
(if on-stop (on-stop))) (if on-stop (on-stop)))
save #(let [v (-> @val str clojure.string/trim)] save #(let [v (-> @val str clojure.string/trim)]
(if-not (empty? v) (on-save v)) (if-not (empty? v) (on-save v))
(stop))] (stop))]
(fn [props] (fn [props]
[:input (merge props [:input (merge props
{:type "text" {:type "text"
@ -199,12 +195,12 @@
:on-blur save :on-blur save
:on-change #(reset! val (-> % .-target .-value)) :on-change #(reset! val (-> % .-target .-value))
:on-key-down #(case (.-which %) :on-key-down #(case (.-which %)
13 (save) 13 (save)
27 (stop) 27 (stop)
nil)})]))) nil)})])))
(def todo-edit (with-meta todo-input (def todo-edit (with-meta todo-input
{:component-did-mount #(.focus (reagent/dom-node %))})) {:component-did-mount #(.focus (reagent/dom-node %))}))
(defn stats-footer (defn stats-footer
[] []
@ -268,7 +264,7 @@
[:input#toggle-all [:input#toggle-all
{:type "checkbox" {:type "checkbox"
:checked (pos? @completed-count) :checked (pos? @completed-count)
:on-change #(dispatch [:complete-all (pos? @completed-count)])}] :on-change #(dispatch [:complete-all-toggle])}]
[:label {:for "toggle-all"} "Mark all as complete"] [:label {:for "toggle-all"} "Mark all as complete"]
[todo-list visible-todos]] [todo-list visible-todos]]
[stats-footer]])] [stats-footer]])]
@ -288,4 +284,4 @@
(dispatch [:initialise-db]) (dispatch [:initialise-db])
(reagent/render [main-panel] (js/document.getElementById "app"))) (reagent/render [main-panel] (js/document.getElementById "app")))
;; you must always return the db ;; you must always return the db