todomvc
This commit is contained in:
parent
6c63ad315c
commit
248faf9e66
|
@ -7,10 +7,7 @@
|
||||||
|
|
||||||
;; TODOs
|
;; TODOs
|
||||||
;; 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
|
;; load todos off localstorage via merge ... and write back via middleware
|
||||||
;; load todos off localstorage via merge ... and write back
|
|
||||||
;; Add Prismatic schema - modules called state
|
|
||||||
;; add middleware to save to local storage
|
|
||||||
|
|
||||||
(enable-console-print!)
|
(enable-console-print!)
|
||||||
|
|
||||||
|
|
|
@ -16,12 +16,16 @@
|
||||||
(def schema
|
(def schema
|
||||||
{:todos (s/both (s/pred map?) (s/pred sorted?))
|
{:todos (s/both (s/pred map?) (s/pred sorted?))
|
||||||
:showing (s/enum :all :done :active)
|
:showing (s/enum :all :done :active)
|
||||||
|
:blah (s/enum :all :done :active) ;; add this bogus schema item in, then watch the console
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
(defn valid-schema?
|
(defn valid-schema?
|
||||||
[db]
|
[db]
|
||||||
(s/validate schema db))
|
(let [res (s/check schema db)]
|
||||||
|
(if (some? res)
|
||||||
|
(.error js/console res))
|
||||||
|
db)) ;; so it can be used in middleare
|
||||||
|
|
||||||
|
|
||||||
;; -- Default Value ----------------------------------------------------------
|
;; -- Default Value ----------------------------------------------------------
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
(ns todomvc.handlers
|
(ns todomvc.handlers
|
||||||
(:require [re-frame.core :refer [register-pure-handler
|
(:require
|
||||||
path
|
[todomvc.db :refer [default-value valid-schema?]]
|
||||||
|
[re-frame.core :refer [register-pure-handler
|
||||||
|
path after
|
||||||
trim-v
|
trim-v
|
||||||
debug]]))
|
debug validate]]))
|
||||||
|
|
||||||
|
|
||||||
;; -- Middleware --------------------------------------------------------------
|
;; -- Middleware --------------------------------------------------------------
|
||||||
;; To be used for handlers operating solely on todos
|
;; To be used for handlers operating solely on todos
|
||||||
;;
|
;;
|
||||||
(def todo-middleware [(path [:todos]) debug trim-v])
|
(def check-schema (after valid-schema?))
|
||||||
|
(def todo-middleware [check-schema (path [:todos]) debug trim-v])
|
||||||
|
|
||||||
|
|
||||||
;; -- Helpers -----------------------------------------------------------------
|
;; -- Helpers -----------------------------------------------------------------
|
||||||
|
@ -22,15 +25,16 @@
|
||||||
|
|
||||||
;; -- Handlers ----------------------------------------------------------------
|
;; -- Handlers ----------------------------------------------------------------
|
||||||
|
|
||||||
(register-pure-handler ;; disptached to on program startup
|
(register-pure-handler ;; disptached to on app startup
|
||||||
:initialise-db ;; event id being handled
|
:initialise-db ;; event id being handled
|
||||||
|
check-schema ;; middleware
|
||||||
(fn [_ _] ;; the handler
|
(fn [_ _] ;; the handler
|
||||||
default-initial-state)) ;; all hail the new state
|
default-value)) ;; all hail the new state
|
||||||
|
|
||||||
|
|
||||||
(register-pure-handler ;; disptached to when
|
(register-pure-handler ;; disptached to when user changes the bottom filter
|
||||||
:set-showing ;; event-id
|
:set-showing ;; event-id
|
||||||
[debug trim-v] ;; middleware (wraps the handler)
|
[check-schema debug trim-v] ;; middleware (wraps the handler)
|
||||||
(fn ;; handler
|
(fn ;; handler
|
||||||
[db [filter-kw]]
|
[db [filter-kw]]
|
||||||
(assoc db :showing filter-kw)))
|
(assoc db :showing filter-kw)))
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
(ns todomvc.subs
|
(ns todomvc.subs
|
||||||
(:require-macros [reagent.ratom :refer [reaction]])
|
(:require-macros [reagent.ratom :refer [reaction]]) ;; remove for v0.2.0-alpha2
|
||||||
(:require [re-frame.core :refer [register-sub
|
(:require [re-frame.core :refer [register-sub]]))
|
||||||
subscribe ]]))
|
|
||||||
|
|
||||||
|
|
||||||
;; -- Helpers -----------------------------------------------------------------
|
;; -- Helpers -----------------------------------------------------------------
|
||||||
|
@ -16,34 +15,54 @@
|
||||||
|
|
||||||
|
|
||||||
(defn completed-count
|
(defn completed-count
|
||||||
"return the count of todos where :done is true"
|
"return the count of todos for which :done is true"
|
||||||
[todos]
|
[todos]
|
||||||
(count (filter :done (vals todos))))
|
(count (filter :done (vals todos))))
|
||||||
|
|
||||||
|
|
||||||
;; -- Subscriptions -----------------------------------------------------------
|
;; -- Subscription handlers and registration ---------------------------------
|
||||||
|
|
||||||
(register-sub ;; has app-db been initialised yet?
|
(register-sub ;; has app-db been initialised yet?
|
||||||
:initialised? ;; usage: (subscribe [:initialised?])
|
:initialised? ;; usage: (subscribe [:initialised?])
|
||||||
(fn [db _]
|
(fn [db _]
|
||||||
(reaction (not (empty? @db)))))
|
(reaction (not (empty? @db)))))
|
||||||
|
|
||||||
|
;; in v0.2.0-alpha2 will be
|
||||||
|
#_(register-sub ;; has app-db been initialised yet?
|
||||||
|
:initialised? ;; usage: (subscribe [:initialised?])
|
||||||
|
(complement empty? deref))
|
||||||
|
|
||||||
|
|
||||||
(register-sub
|
(register-sub
|
||||||
:visible-todos
|
:visible-todos
|
||||||
(fn [db _]
|
(fn [db _]
|
||||||
(reaction
|
(reaction
|
||||||
(let [filter-fn (filter-fn-for (:showing @db))
|
(let [filter-fn (filter-fn-for (:showing @db))
|
||||||
todos (:todos @db)]
|
todos (vals (:todos @db))]
|
||||||
(filter filter-fn todos)))))
|
(filter filter-fn todos)))))
|
||||||
|
|
||||||
|
|
||||||
|
;; in v0.2.0-alpha2 will be
|
||||||
|
#_(register-sub
|
||||||
|
:visible-todos
|
||||||
|
[(from [:showing]) (from [:todos])]
|
||||||
|
(fn [showing todos]
|
||||||
|
(filter (filter-fn-for @showing) @todos)))
|
||||||
|
|
||||||
|
|
||||||
(register-sub
|
(register-sub
|
||||||
:completed-count
|
:completed-count
|
||||||
(fn [db _]
|
(fn [db _]
|
||||||
(reaction (completed-count (:todos @db)))))
|
(reaction (completed-count (:todos @db)))))
|
||||||
|
|
||||||
|
|
||||||
|
;; in v0.2.0-alpha2 will be
|
||||||
|
#_(register-sub
|
||||||
|
:completed-count
|
||||||
|
(fn [db _]
|
||||||
|
(completed-count (:todos @db))))
|
||||||
|
|
||||||
|
|
||||||
(register-sub
|
(register-sub
|
||||||
:footer-stats
|
:footer-stats
|
||||||
(fn
|
(fn
|
||||||
|
@ -52,6 +71,17 @@
|
||||||
(let [todos (:todos @db)
|
(let [todos (:todos @db)
|
||||||
completed-count (completed-count todos)
|
completed-count (completed-count todos)
|
||||||
active-count (- (count todos) completed-count)
|
active-count (- (count todos) completed-count)
|
||||||
showing (:showing db)]
|
showing (:showing @db)]
|
||||||
[active-count completed-count showing])))) ;; tuple
|
[active-count completed-count showing])))) ;; tuple
|
||||||
|
|
||||||
|
|
||||||
|
;; in v0.2.0-alpha2 will be
|
||||||
|
#_(register-sub
|
||||||
|
:footer-stats
|
||||||
|
[(from [:showing]) (from [:todos])]
|
||||||
|
(fn
|
||||||
|
[showing todos _]
|
||||||
|
(let [completed-count (completed-count @todos)
|
||||||
|
active-count (- (count @todos) completed-count)]
|
||||||
|
[active-count completed-count @showing]))) ;; tuple
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,11 @@
|
||||||
[re-frame.core :refer [subscribe dispatch]]))
|
[re-frame.core :refer [subscribe dispatch]]))
|
||||||
|
|
||||||
|
|
||||||
|
;; -- Reagent Components ------------------------------------------------------
|
||||||
|
;;
|
||||||
|
;; The 'V' part of the app
|
||||||
|
;;
|
||||||
|
|
||||||
(defn todo-input [{:keys [title on-save on-stop]}]
|
(defn todo-input [{:keys [title on-save on-stop]}]
|
||||||
(let [val (atom title)
|
(let [val (atom title)
|
||||||
stop #(do (reset! val "")
|
stop #(do (reset! val "")
|
||||||
|
@ -32,7 +37,6 @@
|
||||||
props-for (fn [filter-kw]
|
props-for (fn [filter-kw]
|
||||||
{:class (if (= filter-kw filter) "selected")
|
{:class (if (= filter-kw filter) "selected")
|
||||||
:on-click #(dispatch [:set-showing filter-kw])})]
|
:on-click #(dispatch [:set-showing filter-kw])})]
|
||||||
(println active done filter)
|
|
||||||
[:footer#footer
|
[:footer#footer
|
||||||
[:div
|
[:div
|
||||||
[:span#todo-count
|
[:span#todo-count
|
||||||
|
@ -53,7 +57,8 @@
|
||||||
[:li {:class (str (if done "completed ")
|
[:li {:class (str (if done "completed ")
|
||||||
(if @editing "editing"))}
|
(if @editing "editing"))}
|
||||||
[:div.view
|
[:div.view
|
||||||
[:input.toggle {:type "checkbox" :checked done
|
[:input.toggle {:type "checkbox"
|
||||||
|
:checked done
|
||||||
:on-change #(dispatch [:toggle-done id])}]
|
:on-change #(dispatch [:toggle-done id])}]
|
||||||
[:label {:on-double-click #(reset! editing true)} title]
|
[:label {:on-double-click #(reset! editing true)} title]
|
||||||
[:button.destroy {:on-click #(dispatch [:delete-todo id])}]]
|
[:button.destroy {:on-click #(dispatch [:delete-todo id])}]]
|
||||||
|
@ -81,7 +86,7 @@
|
||||||
[:h1 "todos"]
|
[:h1 "todos"]
|
||||||
[todo-input {:id "new-todo"
|
[todo-input {:id "new-todo"
|
||||||
:placeholder "What needs to be done?"
|
:placeholder "What needs to be done?"
|
||||||
:on-save #(dispatch [:add-todo %])}]]
|
:on-save #(dispatch [:add-todo %1])}]]
|
||||||
(when-not (empty? @visible-todos)
|
(when-not (empty? @visible-todos)
|
||||||
[:div
|
[:div
|
||||||
[:section#main
|
[:section#main
|
||||||
|
|
Loading…
Reference in New Issue