2015-11-20 19:22:02 +00:00
|
|
|
(ns $PROJECT_NAME_HYPHENATED$.handlers
|
|
|
|
(:require
|
2015-12-13 21:49:54 +00:00
|
|
|
[re-frame.core :refer [register-handler after]]
|
2017-05-12 13:38:37 +00:00
|
|
|
[clojure.spec.alpha :as s]
|
2016-07-24 22:06:55 +00:00
|
|
|
[$PROJECT_NAME_HYPHENATED$.db :as db :refer [app-db]]))
|
2015-11-20 19:22:02 +00:00
|
|
|
|
2015-12-13 21:49:54 +00:00
|
|
|
;; -- Middleware ------------------------------------------------------------
|
|
|
|
;;
|
|
|
|
;; See https://github.com/Day8/re-frame/wiki/Using-Handler-Middleware
|
|
|
|
;;
|
|
|
|
(defn check-and-throw
|
2016-07-24 22:06:55 +00:00
|
|
|
"Throw an exception if db doesn't have a valid spec."
|
|
|
|
[spec db]
|
|
|
|
(when-not (s/valid? spec db)
|
|
|
|
(let [explain-data (s/explain-data spec db)]
|
|
|
|
(throw (ex-info (str "Spec check failed: " explain-data) explain-data)))))
|
2015-12-13 21:49:54 +00:00
|
|
|
|
2016-07-24 22:06:55 +00:00
|
|
|
(def validate-spec-mw
|
2016-05-14 16:07:15 +00:00
|
|
|
(if goog.DEBUG
|
2016-07-24 22:06:55 +00:00
|
|
|
(after (partial check-and-throw ::db/app-db))
|
2016-05-14 16:07:15 +00:00
|
|
|
[]))
|
2015-12-13 21:49:54 +00:00
|
|
|
|
|
|
|
;; -- Handlers --------------------------------------------------------------
|
2015-11-20 19:22:02 +00:00
|
|
|
|
|
|
|
(register-handler
|
|
|
|
:initialize-db
|
2016-07-24 22:06:55 +00:00
|
|
|
validate-spec-mw
|
2015-11-20 19:22:02 +00:00
|
|
|
(fn [_ _]
|
|
|
|
app-db))
|
|
|
|
|
|
|
|
(register-handler
|
|
|
|
:set-greeting
|
2016-07-24 22:06:55 +00:00
|
|
|
validate-spec-mw
|
2015-11-20 19:22:02 +00:00
|
|
|
(fn [db [_ value]]
|
2016-07-24 22:06:55 +00:00
|
|
|
(assoc db :greeting value)))
|