reagent/demo/reagentdemo/news.cljs

52 lines
1.5 KiB
Plaintext
Raw Normal View History

(ns reagentdemo.news
2014-01-19 09:25:16 +00:00
(:require [reagent.core :as reagent :refer [atom]]
[reagent.debug :refer-macros [dbg println]]
[reagentdemo.syntax :refer-macros [get-source]]
[reagentdemo.common :as common :refer [demo-component title]]
2014-01-19 09:25:16 +00:00
[todomvc :as todomvc]))
2014-01-19 09:25:16 +00:00
(def funmap (-> "reagentdemo/news.cljs" get-source common/fun-map))
(def src-for (partial common/src-for funmap))
(def state todomvc/todos)
(def undo-list (atom nil))
(defn undo []
(let [undos @undo-list]
(when-let [old (first undos)]
(reset! state old)
(reset! undo-list (rest undos)))))
(defn undo-button []
(let [n (count @undo-list)]
[:input {:type "button" :on-click undo
:disabled (zero? n)
:value (str "Undo (" n ")")}]))
(defn todomvc-with-undo []
(add-watch state ::undo-watcher
(fn [_ _ old-state _]
(swap! undo-list conj old-state)))
[:div
2014-01-19 09:25:16 +00:00
[undo-button]
[todomvc/todo-app]])
(defn undo-demo []
[demo-component {:comp todomvc-with-undo
:src (src-for [:state :undo-list :undo :save-state
:undo-button :todomvc-with-undo])}])
(def undo-demo-cleanup
(with-meta undo-demo {:component-will-unmount
(fn []
(reset! undo-list nil)
(remove-watch state ::undo-watcher))}))
(defn main []
(let [head "This should become news"]
[:div.reagent-demo
[:h1 head]
[title head]
[undo-demo-cleanup]]))