SHARE
TWEET

Untitled

a guest Sep 19th, 2019 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns app.state
  2.   (:require
  3.    [reagent.core :as reagent :refer [atom]]))
  4.  
  5. (defn accounts [state action]
  6.   (let [type (:type action) data (:data action)]
  7.     (if (nil? state)
  8.       []
  9.       (cond
  10.         (= type :accounts-replace) data
  11.         :else state))))
  12. (defn auth [state action]
  13.   (let [type (:type action) data (:data action)]
  14.     (if (nil? state)
  15.       {:access-token nil}
  16.       (cond
  17.         (= type :auth-accesstoken-set) (assoc state :access-token data)
  18.         :else state))))
  19. (defn login [state action]
  20.   (let [type (:type action) data (:data action)]
  21.     (if (nil? state)
  22.       {:client nil :customer nil :secret nil}
  23.       (cond
  24.         (= type :login-set-client) (assoc state :client data)
  25.         (= type :login-set-customer) (assoc state :customer data)
  26.         (= type :login-set-secret) (assoc state :secret data)        
  27.         :else state))))
  28. (defn transfer [state action]
  29.   (let [type (:type action) data (:data action)]
  30.     (if (nil? state)
  31.       {:from nil :to nil :amount nil :message nil}
  32.       (cond
  33.         (= type :transfer-set-from) (assoc state :from data)
  34.         (= type :transfer-set-to) (assoc state :to data)
  35.         (= type :transfer-set-message) (assoc state :message data)
  36.         (= type :transfer-set-amount) (assoc state :amount data)
  37.         :else state))))
  38.  
  39. (defn create-store [reducers] ;;reducers should be a hash of functions
  40.   {:reducers reducers :state (atom nil)})
  41.  
  42. (defn create-dispatch [s]
  43.   (fn [action]
  44.   (let [state (:state s) reducers (:reducers s)]
  45.     (doseq [reducer reducers]
  46.       (let [
  47.             key (first reducer)
  48.             f (second reducer)
  49.             value (f (get @state key) action)]
  50.         (reset! state (assoc @state key value)))))))
  51.  
  52. (def store (create-store {:accounts accounts :auth auth :login login :transfer transfer}))
  53. (def dispatch (create-dispatch store))
  54. (dispatch nil)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top