Last active
March 7, 2018 07:12
-
-
Save CalebMacdonaldBlack/a65b2f3ccee31bb7b8a574c0a1574374 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns whatever.sub-test | |
(:require [reagent.core :as r] | |
[reagent.ratom :as ratom] | |
[re-frame.core :as rf])) | |
(def invalidate-form | |
(rf/->interceptor | |
{:before identity | |
:after (fn [{{:keys [:db]} :effects :as ctx}] | |
(assoc-in ctx [:effects :db :test-changed] true))})) | |
(rf/reg-sub | |
:test | |
(fn [db] (:test db))) | |
(rf/reg-event-db | |
:set-test | |
[invalidate-form] | |
(fn [db [_ newval]] (assoc db :test newval))) | |
(rf/reg-event-db | |
:set-test-changed | |
(fn [db [_ newval]] | |
(assoc db :test-changed newval))) | |
(rf/reg-sub | |
:test-changed | |
(fn [db _] (:test-changed db))) | |
(rf/reg-event-db | |
:change-d | |
[invalidate-form] | |
(fn [db [_ newd]] | |
(assoc-in db [:test :b 1 :d] newd))) | |
(defn re-frame-idiomatic-games [] | |
(let [changed (rf/subscribe [:test-changed]) | |
test (rf/subscribe [:test])] | |
[:div | |
[:button {:on-click #(rf/dispatch [:set-test {:a 1 :b [{:c 3} {:d 4}]}])} | |
"Set initial state"] | |
[:button {:on-click #(rf/dispatch [:change-d (rand-int 10)])} | |
"Change field in :test randomly"] | |
[:button {:on-click #(rf/dispatch [:set-test-changed false])} | |
"Reset :test-changed"] | |
[:div ":test " (str @test)] | |
[:div ":test-changed " (str @changed)]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment