2016-10-04 14:49:59 +03:00
|
|
|
(ns status-im.data-store.accounts
|
2018-05-14 19:55:30 +02:00
|
|
|
(:require [re-frame.core :as re-frame]
|
2018-05-21 15:20:00 +02:00
|
|
|
[status-im.data-store.realm.core :as core]
|
|
|
|
[status-im.utils.types :as types]))
|
2016-10-04 14:49:59 +03:00
|
|
|
|
2018-04-02 18:17:15 +02:00
|
|
|
;; TODO janherich: define as cofx once debug handlers are refactored
|
2016-10-04 14:49:59 +03:00
|
|
|
(defn get-by-address [address]
|
2018-05-14 19:55:30 +02:00
|
|
|
(-> @core/base-realm
|
|
|
|
(core/get-by-field :account :address address)
|
|
|
|
(core/single-clj :account)
|
|
|
|
(update :settings core/deserialize)))
|
2016-10-04 14:49:59 +03:00
|
|
|
|
2018-05-21 15:20:00 +02:00
|
|
|
(defn- deserialize-account [account]
|
|
|
|
(-> account
|
|
|
|
(update :settings core/deserialize)
|
|
|
|
(update :networks (partial reduce-kv
|
|
|
|
(fn [acc network-id props]
|
|
|
|
(assoc acc network-id
|
|
|
|
(update props :config types/json->clj)))
|
|
|
|
{}))))
|
|
|
|
|
2018-04-02 18:17:15 +02:00
|
|
|
(re-frame/reg-cofx
|
2018-05-07 17:09:06 +03:00
|
|
|
:data-store/get-all-accounts
|
|
|
|
(fn [coeffects _]
|
2018-05-14 19:55:30 +02:00
|
|
|
(assoc coeffects :all-accounts (-> @core/base-realm
|
|
|
|
(core/get-all :account)
|
|
|
|
(core/all-clj :account)
|
|
|
|
(as-> accounts
|
2018-05-21 15:20:00 +02:00
|
|
|
(map deserialize-account accounts))))))
|
|
|
|
|
|
|
|
(defn- serialize-account [account]
|
|
|
|
(-> account
|
|
|
|
(update :settings core/serialize)
|
|
|
|
(update :networks (partial map (fn [[_ props]]
|
|
|
|
(update props :config types/clj->json))))))
|
2018-04-02 18:17:15 +02:00
|
|
|
|
2018-05-14 19:55:30 +02:00
|
|
|
(defn save-account-tx
|
|
|
|
"Returns tx function for saving account"
|
|
|
|
[{:keys [after-update-event] :as account}]
|
|
|
|
(fn [realm]
|
2018-05-21 15:20:00 +02:00
|
|
|
(let [account-to-save (-> (serialize-account account)
|
|
|
|
(dissoc :after-update-event))]
|
2018-05-14 19:55:30 +02:00
|
|
|
(core/create realm :account account-to-save true)
|
|
|
|
(when after-update-event
|
|
|
|
(re-frame/dispatch after-update-event)))))
|