Use localstorage ... WIP
This commit is contained in:
parent
29e36eb862
commit
c116d01a87
|
@ -1,6 +1,6 @@
|
||||||
(ns todomvc.handlers
|
(ns todomvc.handlers
|
||||||
(:require
|
(:require
|
||||||
[todomvc.db :refer [default-value valid-schema?]]
|
[todomvc.db :refer [default-value valid-schema? get-local-storage]]
|
||||||
[re-frame.core :refer [register-pure-handler
|
[re-frame.core :refer [register-pure-handler
|
||||||
path after
|
path after
|
||||||
trim-v
|
trim-v
|
||||||
|
@ -8,9 +8,11 @@
|
||||||
|
|
||||||
|
|
||||||
;; -- Middleware --------------------------------------------------------------
|
;; -- Middleware --------------------------------------------------------------
|
||||||
;; To be used for handlers operating solely on todos
|
|
||||||
;;
|
;;
|
||||||
|
;; checks that the structure in app-db matches the schema
|
||||||
(def check-schema (after valid-schema?))
|
(def check-schema (after valid-schema?))
|
||||||
|
|
||||||
|
;; middleware for any handler which manipulates todos.
|
||||||
(def todo-middleware [check-schema (path [:todos]) debug trim-v])
|
(def todo-middleware [check-schema (path [:todos]) debug trim-v])
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,10 +31,11 @@
|
||||||
:initialise-db ;; event id being handled
|
:initialise-db ;; event id being handled
|
||||||
check-schema ;; middleware
|
check-schema ;; middleware
|
||||||
(fn [_ _] ;; the handler
|
(fn [_ _] ;; the handler
|
||||||
default-value)) ;; all hail the new state
|
(merge default-value
|
||||||
|
(get-local-storage)))) ;; all hail the new state
|
||||||
|
|
||||||
|
|
||||||
(register-pure-handler ;; disptached to when user changes the bottom filter
|
(register-pure-handler ;; handlers changes the footer filter
|
||||||
:set-showing ;; event-id
|
:set-showing ;; event-id
|
||||||
[check-schema debug trim-v] ;; middleware (wraps the handler)
|
[check-schema debug trim-v] ;; middleware (wraps the handler)
|
||||||
(fn ;; handler
|
(fn ;; handler
|
||||||
|
@ -40,10 +43,10 @@
|
||||||
(assoc db :showing filter-kw)))
|
(assoc db :showing filter-kw)))
|
||||||
|
|
||||||
|
|
||||||
(register-pure-handler ;; given only the text, create a new todo
|
(register-pure-handler ;; given the text, create a new todo
|
||||||
:add-todo
|
:add-todo
|
||||||
todo-middleware ;; only 'todo' part of db provided
|
todo-middleware
|
||||||
(fn [todos [text]]
|
(fn [todos [text]] ;; "path" middlware means we are given :todo
|
||||||
(let [id (next-id todos)]
|
(let [id (next-id todos)]
|
||||||
(assoc todos id {:id id :title text :done false}))))
|
(assoc todos id {:id id :title text :done false}))))
|
||||||
|
|
||||||
|
@ -52,7 +55,7 @@
|
||||||
:complete-all-toggle
|
:complete-all-toggle
|
||||||
todo-middleware
|
todo-middleware
|
||||||
(fn [todos]
|
(fn [todos]
|
||||||
(let [val (not-every? :done (vals todos))]
|
(let [val (not-every? :done (vals todos))] ;; toggle true or false?
|
||||||
(reduce #(assoc-in %1 [%2 :done] val)
|
(reduce #(assoc-in %1 [%2 :done] val)
|
||||||
todos
|
todos
|
||||||
(keys todos)))))
|
(keys todos)))))
|
||||||
|
|
Loading…
Reference in New Issue