Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns app.state
- (:require
- [reagent.core :as reagent :refer [atom]]))
- (defn accounts [state action]
- (let [type (:type action) data (:data action)]
- (if (nil? state)
- []
- (cond
- (= type :accounts-replace) data
- :else state))))
- (defn auth [state action]
- (let [type (:type action) data (:data action)]
- (if (nil? state)
- {:access-token nil}
- (cond
- (= type :auth-accesstoken-set) (assoc state :access-token data)
- :else state))))
- (defn login [state action]
- (let [type (:type action) data (:data action)]
- (if (nil? state)
- {:client nil :customer nil :secret nil}
- (cond
- (= type :login-set-client) (assoc state :client data)
- (= type :login-set-customer) (assoc state :customer data)
- (= type :login-set-secret) (assoc state :secret data)
- :else state))))
- (defn transfer [state action]
- (let [type (:type action) data (:data action)]
- (if (nil? state)
- {:from nil :to nil :amount nil :message nil}
- (cond
- (= type :transfer-set-from) (assoc state :from data)
- (= type :transfer-set-to) (assoc state :to data)
- (= type :transfer-set-message) (assoc state :message data)
- (= type :transfer-set-amount) (assoc state :amount data)
- :else state))))
- (defn create-store [reducers] ;;reducers should be a hash of functions
- {:reducers reducers :state (atom nil)})
- (defn create-dispatch [s]
- (fn [action]
- (let [state (:state s) reducers (:reducers s)]
- (doseq [reducer reducers]
- (let [
- key (first reducer)
- f (second reducer)
- value (f (get @state key) action)]
- (reset! state (assoc @state key value)))))))
- (def store (create-store {:accounts accounts :auth auth :login login :transfer transfer}))
- (def dispatch (create-dispatch store))
- (dispatch nil)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement