 ;; So, `path` operates a little like `update-in`
 ;;
 #_(reg-event-db
-  :set-showing
-
-  ;; this now a chain of 2 interceptors. Note use of `path`
-  [check-spec-interceptor (path :showing)]
-
-  ;; The event handler
-  ;; Because of the `path` interceptor above, the 1st parameter to
-  ;; the handler below won't be the entire 'db', and instead will
-  ;; be the value at the path `[:showing]` within db.
-  ;; Equally the value returned will be the new value for that path
-  ;; within app-db.
-  (fn [old-showing-value [_ new-showing-value]]
-    new-showing-value))                  ;; return new state for the path
+    :set-showing
+
+    ;; this now a chain of 2 interceptors. Note use of `path`
+    [check-spec-interceptor (path :showing)]
+
+    ;; The event handler
+    ;; Because of the `path` interceptor above, the 1st parameter to
+    ;; the handler below won't be the entire 'db', and instead will
+    ;; be the value at the path `[:showing]` within db.
+    ;; Equally the value returned will be the new value for that path
+    ;; within app-db.
+    (fn [old-showing-value [_ new-showing-value]]
+      new-showing-value))                                   ;; return new state for the path
 
 
 ;; usage:  (dispatch [:add-todo  "a description string"])
-(reg-event-db                     ;; given the text, create a new todo
+(reg-event-db                                               ;; given the text, create a new todo
   :add-todo
 
   ;; Use the standard interceptors, defined above, which we

(ns todomvc.events
  (:require
    [todomvc.db    :refer [default-db todos->local-store]]
    [re-frame.core :refer [reg-event-db reg-event-fx inject-cofx path after]]
    [cljs.spec.alpha :as s]
    [day8.re-frame.tracing :refer-macros [fn-traced]]))

;; To fully understand this advanced topic, you'll have to read the tutorials
;; and look at the bottom of `db.cljs` for the `:local-store-todos` cofx
;; registration.
(reg-event-fx                                               ;; part of the re-frame API
  :initialise-db                                            ;; event id being handled

  ;; the interceptor chain (a vector of 2 interceptors in this case)
  [(inject-cofx :local-store-todos)                         ;; gets todos from localstore, and puts value into coeffects arg
   check-spec-interceptor]                                  ;; after event handler runs, check app-db for correctness. Does it still match Spec?

  ;; the event handler (function) being registered
  (fn [{:keys [db local-store-todos]} _]                    ;; take 2 values from coeffects. Ignore event vector itself.
    {:db (assoc default-db :todos local-store-todos)}))     ;; all hail the new state to be put in app-db


;; usage:  (dispatch [:set-showing  :active])
;; This event is dispatched when the user clicks on one of the 3
;; filter buttons at the bottom of the display.
(reg-event-db                                               ;; part of the re-frame API
  :set-showing                                              ;; event-id

  ;; only one interceptor
  [check-spec-interceptor]                                  ;; after event handler runs, check app-db for correctness. Does it still match Spec?

  ;; handler
  (fn [db [_ new-filter-kw]]                                ;; new-filter-kw is one of :all, :active or :done
    (assoc db :showing new-filter-kw)))

;; NOTE: below is a rewrite of the event handler (above) using a `path` Interceptor
