diff --git a/src/status_im/chat/events.cljs b/src/status_im/chat/events.cljs index db76a6b1a0..0aec4b61bd 100644 --- a/src/status_im/chat/events.cljs +++ b/src/status_im/chat/events.cljs @@ -22,6 +22,7 @@ [status-im.transport.message.v1.group-chat :as group-chat] [status-im.data-store.chats :as chats-store] [status-im.data-store.messages :as messages-store] + [status-im.data-store.contacts :as contacts-store] status-im.chat.events.commands status-im.chat.events.requests status-im.chat.events.send-message @@ -167,8 +168,9 @@ contacts-to-add (select-keys new-contacts (set/difference (set (keys new-contacts)) (set (keys existing-contacts))))] (handlers-macro/merge-fx cofx - {:db (update db :contacts/contacts merge contacts-to-add) - :data-store/save-contacts (vals contacts-to-add)} + {:db (update db :contacts/contacts merge contacts-to-add) + :data-store/tx [(contacts-store/save-contacts-tx + (vals contacts-to-add))]} (events.loading/load-commands)))) (handlers/register-handler-fx diff --git a/src/status_im/chat/events/requests.cljs b/src/status_im/chat/events/requests.cljs index 55f22e5a00..f613493890 100644 --- a/src/status_im/chat/events/requests.cljs +++ b/src/status_im/chat/events/requests.cljs @@ -1,6 +1,6 @@ (ns status-im.chat.events.requests - (:require [re-frame.core :as re-frame] - [status-im.constants :as constants])) + (:require [status-im.constants :as constants] + [status-im.data-store.requests :as requests-store])) ;; Functions @@ -8,9 +8,8 @@ "Takes chat-id, message-id and cofx, returns fx necessary data for marking request as answered" [chat-id message-id {:keys [db]}] (when message-id - {:db (update-in db [:chats chat-id :requests] dissoc message-id) - :data-store/mark-request-as-answered {:chat-id chat-id - :message-id message-id}})) + {:db (update-in db [:chats chat-id :requests] dissoc message-id) + :data-store/tx [(requests-store/mark-request-as-answered-tx chat-id message-id)]})) (defn add-request "Takes chat-id, message-id + cofx and returns fx with necessary data for adding new request" @@ -21,5 +20,5 @@ :message-id message-id :response (:request-command content) :status "open"}] - {:db (assoc-in db [:chats chat-id :requests message-id] request) - :data-store/save-request request})))) + {:db (assoc-in db [:chats chat-id :requests message-id] request) + :data-store/tx [(requests-store/save-request-tx request)]})))) diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index 37622128b8..488ff64786 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -7,7 +7,8 @@ [status-im.commands.utils :refer [reg-handler]] [status-im.constants :refer [console-chat-id]] [status-im.i18n :refer [get-contact-translated]] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [status-im.data-store.local-storage :as local-storage-store])) (defn command-handler! [_ [chat-id @@ -105,5 +106,6 @@ :set-local-storage [trim-v] (fn [_ [{:keys [data chat-id]}]] - {:data-store/set-local-storage-data {:chat-id chat-id - :data data}})) + {:data-store/tx [(local-storage-store/set-local-storage-data-tx + {:chat-id chat-id + :data data})]})) diff --git a/src/status_im/data_store/accounts.cljs b/src/status_im/data_store/accounts.cljs index ed149b72ec..5f104c5b6b 100644 --- a/src/status_im/data_store/accounts.cljs +++ b/src/status_im/data_store/accounts.cljs @@ -1,24 +1,31 @@ (ns status-im.data-store.accounts - (:require [cljs.core.async :as async] - [re-frame.core :as re-frame] - [status-im.data-store.realm.core :as core] - [status-im.data-store.realm.accounts :as data-store])) + (:require [re-frame.core :as re-frame] + [status-im.data-store.realm.core :as core])) ;; TODO janherich: define as cofx once debug handlers are refactored (defn get-by-address [address] - (data-store/get-by-address address)) + (-> @core/base-realm + (core/get-by-field :account :address address) + (core/single-clj :account) + (update :settings core/deserialize))) (re-frame/reg-cofx :data-store/get-all-accounts (fn [coeffects _] - (assoc coeffects :all-accounts (data-store/get-all-as-list)))) - -(re-frame/reg-fx - :data-store/save-account - (fn [{:keys [after-update-event] :as account}] - (let [account-to-save (dissoc account :after-update-event)] - (async/go (async/>! core/realm-queue #(if after-update-event - (do (data-store/save account-to-save true) - (re-frame/dispatch after-update-event)) - (data-store/save account-to-save true))))))) + (assoc coeffects :all-accounts (-> @core/base-realm + (core/get-all :account) + (core/all-clj :account) + (as-> accounts + (map #(update % :settings core/deserialize) accounts)))))) +(defn save-account-tx + "Returns tx function for saving account" + [{:keys [after-update-event] :as account}] + (fn [realm] + (let [account-to-save (-> account + (dissoc :after-update-event) + (update :settings core/serialize) + (update :networks vals))] + (core/create realm :account account-to-save true) + (when after-update-event + (re-frame/dispatch after-update-event))))) diff --git a/src/status_im/data_store/browser.cljs b/src/status_im/data_store/browser.cljs index cf54c6cf34..8207da4a7b 100644 --- a/src/status_im/data_store/browser.cljs +++ b/src/status_im/data_store/browser.cljs @@ -1,21 +1,24 @@ (ns status-im.data-store.browser - (:require [cljs.core.async :as async] - [re-frame.core :as re-frame] - [status-im.data-store.realm.core :as core] - [status-im.data-store.realm.browser :as data-store]) - (:refer-clojure :exclude [exists?])) + (:require [re-frame.core :as re-frame] + [status-im.data-store.realm.core :as core])) (re-frame/reg-cofx :data-store/all-browsers (fn [cofx _] - (assoc cofx :all-stored-browsers (data-store/get-all)))) + (assoc cofx :all-stored-browsers (-> @core/account-realm + (core/get-all :browser) + (core/sorted :timestamp :desc) + (core/all-clj :browser))))) -(re-frame/reg-fx - :data-store/save-browser - (fn [{:keys [browser-id] :as browser}] - (async/go (async/>! core/realm-queue #(data-store/save browser (data-store/exists? browser-id)))))) +(defn save-browser-tx + "Returns tx function for saving browser" + [{:keys [browser-id] :as browser}] + (fn [realm] + (core/create realm :browser browser (core/exists? realm :browser :browser-id browser-id)))) -(re-frame/reg-fx - :data-store/remove-browser - (fn [browser-id] - (async/go (async/>! core/realm-queue #(data-store/delete browser-id))))) +(defn remove-browser-tx + "Returns tx function for removing browser" + [browser-id] + (fn [realm] + (let [browser (core/single (core/get-by-field realm :browser :browser-id browser-id))] + (core/delete realm browser)))) diff --git a/src/status_im/data_store/contact_groups.cljs b/src/status_im/data_store/contact_groups.cljs index 333bc4c2be..daf5d11511 100644 --- a/src/status_im/data_store/contact_groups.cljs +++ b/src/status_im/data_store/contact_groups.cljs @@ -1,34 +1,35 @@ (ns status-im.data-store.contact-groups - (:require [cljs.core.async :as async] + (:require [goog.object :as object] [re-frame.core :as re-frame] - [status-im.data-store.realm.core :as core] - [status-im.data-store.realm.contact-groups :as data-store]) - (:refer-clojure :exclude [exists?])) + [status-im.data-store.realm.core :as core])) (re-frame/reg-cofx :data-store/get-all-contact-groups (fn [cofx _] (assoc cofx :all-contact-groups (into {} (map (juxt :group-id identity)) - (data-store/get-all-as-list))))) + (-> @core/account-realm + (core/get-all :contact-group) + (core/all-clj :contact-group)))))) -(re-frame/reg-fx - :data-store/save-contact-group - (fn [{:keys [group-id] :as group}] - (async/go (async/>! core/realm-queue #(data-store/save group (data-store/exists? group-id)))))) +(defn save-contact-group-tx + "Returns tx function for saving contact group" + [{:keys [group-id] :as group}] + (fn [realm] + (core/create realm :contact-group group (core/exists? realm :contact-group :group-id group-id)))) -(re-frame/reg-fx - :data-store/save-contact-groups - (fn [groups] - (doseq [{:keys [group-id] :as group} groups] - (async/go (async/>! core/realm-queue #(data-store/save group (data-store/exists? group-id))))))) +(defn save-contact-groups-tx + "Returns tx function for saving contact groups" + [groups] + (fn [realm] + (doseq [group groups] + ((save-contact-group-tx group) realm)))) -(re-frame/reg-fx - :data-store/save-contact-group-property - (fn [[group-id property-name value]] - (async/go (async/>! core/realm-queue #(data-store/save-property group-id property-name value))))) - -(re-frame/reg-fx - :data-store/add-contacts-to-contact-group - (fn [[group-id contacts]] - (async/go (async/>! core/realm-queue #(data-store/add-contacts group-id contacts))))) +(defn add-contacts-to-contact-group-tx + "Returns tx function for adding contacts to contact group" + [group-id contacts] + (fn [realm] + (let [group (core/single (core/get-by-field realm :contact-group :group-id group-id)) + existing-contacts (object/get group "contacts")] + (aset group "contacts" (clj->js (into #{} (concat contacts + (core/list->clj existing-contacts)))))))) diff --git a/src/status_im/data_store/contacts.cljs b/src/status_im/data_store/contacts.cljs index e7fcb789fb..cc9b56e1b9 100644 --- a/src/status_im/data_store/contacts.cljs +++ b/src/status_im/data_store/contacts.cljs @@ -1,42 +1,32 @@ (ns status-im.data-store.contacts - (:require [cljs.core.async :as async] - [re-frame.core :as re-frame] - [status-im.data-store.realm.core :as core] - [status-im.data-store.realm.contacts :as data-store]) - (:refer-clojure :exclude [exists?])) + (:require [re-frame.core :as re-frame] + [status-im.data-store.realm.core :as core])) (re-frame/reg-cofx :data-store/get-all-contacts (fn [coeffects _] - (assoc coeffects :all-contacts (data-store/get-all-as-list)))) + (assoc coeffects :all-contacts (-> @core/account-realm + (core/get-all :contact) + (core/all-clj :contact))))) -(defn- get-by-id +(defn save-contact-tx + "Returns tx function for saving contact" + [{:keys [whisper-identity] :as contact}] + (fn [realm] + (core/create realm + :contact + (dissoc contact :command :response :subscriptions :jail-loaded-events) + (core/exists? realm :contact :whisper-identity whisper-identity)))) + +(defn save-contacts-tx + "Returns tx function for saving contacts" + [contacts] + (fn [realm] + (doseq [contact contacts] + ((save-contact-tx contact) realm)))) + +(defn delete-contact-tx + "Returns tx function for deleting contact" [whisper-identity] - (data-store/get-by-id-cljs whisper-identity)) - -(defn- save - [{:keys [whisper-identity pending?] :as contact}] - (let [{pending-db? :pending? - :as contact-db} (get-by-id whisper-identity) - contact' (-> contact - (assoc :pending? (boolean (if contact-db - (if (nil? pending?) pending-db? pending?) - pending?))) - (dissoc :command :response :subscriptions :jail-loaded-events))] - (data-store/save contact' (boolean contact-db)))) - -(re-frame/reg-fx - :data-store/save-contact - (fn [contact] - (async/go (async/>! core/realm-queue #(save contact))))) - -(re-frame/reg-fx - :data-store/save-contacts - (fn [contacts] - (doseq [contact contacts] - (async/go (async/>! core/realm-queue #(save contact)))))) - -(re-frame/reg-fx - :data-store/delete-contact - (fn [contact] - (async/go (async/>! core/realm-queue #(data-store/delete contact))))) + (fn [realm] + (core/delete realm (core/single (core/get-by-field realm :contact :whisper-identity whisper-identity))))) diff --git a/src/status_im/data_store/local_storage.cljs b/src/status_im/data_store/local_storage.cljs index c985c9290b..c63f98d930 100644 --- a/src/status_im/data_store/local_storage.cljs +++ b/src/status_im/data_store/local_storage.cljs @@ -1,15 +1,17 @@ (ns status-im.data-store.local-storage - (:require [cljs.core.async :as async] - [re-frame.core :as re-frame] - [status-im.data-store.realm.core :as core] - [status-im.data-store.realm.local-storage :as data-store])) + (:require [re-frame.core :as re-frame] + [status-im.data-store.realm.core :as core])) (re-frame/reg-cofx :data-store/get-local-storage-data (fn [cofx _] - (assoc cofx :get-local-storage-data (comp :data data-store/get-by-chat-id)))) + (assoc cofx :get-local-storage-data #(-> @core/account-realm + (core/get-by-field :local-storage :chat-id %) + (core/single-clj :local-storage) + :data)))) -(re-frame/reg-fx - :data-store/set-local-storage-data - (fn [data] - (async/go (async/>! core/realm-queue #(data-store/save data))))) +(defn set-local-storage-data-tx + "Returns tx function setting local storage data" + [data] + (fn [realm] + (core/create realm :local-storage data true))) diff --git a/src/status_im/data_store/realm/accounts.cljs b/src/status_im/data_store/realm/accounts.cljs deleted file mode 100644 index 033d7e7010..0000000000 --- a/src/status_im/data_store/realm/accounts.cljs +++ /dev/null @@ -1,22 +0,0 @@ -(ns status-im.data-store.realm.accounts - (:require [status-im.data-store.realm.core :as realm])) - -(defn get-all-as-list [] - (->> (realm/all-clj (realm/get-all @realm/base-realm :account) :account) - (mapv #(update % :settings realm/deserialize)))) - -(defn get-by-address [address] - (-> @realm/base-realm - (realm/get-by-field :account :address address) - (realm/single-clj :account) - (update :settings realm/deserialize))) - -(defn- create-account-fn [account update?] - #(realm/create @realm/base-realm :account account update?)) - -(defn save [account update?] - (realm/write @realm/base-realm - (-> account - (update :settings realm/serialize) - (update :networks vals) - (create-account-fn update?)))) diff --git a/src/status_im/data_store/realm/browser.cljs b/src/status_im/data_store/realm/browser.cljs deleted file mode 100644 index c122b63518..0000000000 --- a/src/status_im/data_store/realm/browser.cljs +++ /dev/null @@ -1,29 +0,0 @@ -(ns status-im.data-store.realm.browser - (:require [status-im.data-store.realm.core :as realm]) - (:refer-clojure :exclude [exists?])) - -(defn get-all - [] - (-> @realm/account-realm - (realm/get-all :browser) - (realm/sorted :timestamp :desc) - (realm/all-clj :browser))) - -(defn save - [browser update?] - (realm/save @realm/account-realm :browser browser update?)) - -(defn delete - [browser-id] - (when-let [browser (realm/single (realm/get-by-field @realm/account-realm :browser :browser-id browser-id))] - (realm/write @realm/account-realm #(realm/delete @realm/account-realm browser)))) - -(defn exists? - [browser-id] - (realm/exists? @realm/account-realm :browser :browser-id browser-id)) - -(defn get-by-id - [browser-id] - (-> @realm/account-realm - (realm/get-by-field :browser :browser-id browser-id) - (realm/single-clj :browser))) diff --git a/src/status_im/data_store/realm/contact_groups.cljs b/src/status_im/data_store/realm/contact_groups.cljs deleted file mode 100644 index 94ab8a6550..0000000000 --- a/src/status_im/data_store/realm/contact_groups.cljs +++ /dev/null @@ -1,45 +0,0 @@ -(ns status-im.data-store.realm.contact-groups - (:require [goog.object :as object] - [status-im.data-store.realm.core :as realm]) - (:refer-clojure :exclude [exists?])) - -(defn get-all-as-list - [] - (realm/all-clj (realm/get-all @realm/account-realm :contact-group) :contact-group)) - -(defn save - [group update?] - (realm/save @realm/account-realm :contact-group group update?)) - -(defn save-property - [group-id property-name value] - (realm/write @realm/account-realm - (fn [] - (-> @realm/account-realm - (realm/get-by-field :contact-group :group-id group-id) - realm/single - (aset (name property-name) value))))) - -(defn exists? - [group-id] - (realm/exists? @realm/account-realm :contact-group :group-id group-id)) - -(defn delete - [group-id] - (when-let [group (-> @realm/account-realm - (realm/get-by-field :contact-group :group-id group-id) - realm/single)] - (realm/write @realm/account-realm #(realm/delete @realm/account-realm group)))) - -(defn- get-by-id-obj - [group-id] - (realm/single (realm/get-by-field @realm/account-realm :contact-group :group-id group-id))) - -(defn add-contacts - [group-id identities] - (let [group (get-by-id-obj group-id) - contacts (object/get group "contacts")] - (realm/write @realm/account-realm - #(aset group "contacts" - (clj->js (into #{} (concat identities - (realm/list->clj contacts)))))))) diff --git a/src/status_im/data_store/realm/contacts.cljs b/src/status_im/data_store/realm/contacts.cljs deleted file mode 100644 index 7e7137401d..0000000000 --- a/src/status_im/data_store/realm/contacts.cljs +++ /dev/null @@ -1,30 +0,0 @@ -(ns status-im.data-store.realm.contacts - (:require [status-im.data-store.realm.core :as realm]) - (:refer-clojure :exclude [exists?])) - -(defn get-all-as-list - [] - (realm/all-clj (realm/get-all @realm/account-realm :contact) :contact)) - -(defn get-by-id - [whisper-identity] - (realm/single (realm/get-by-field @realm/account-realm :contact :whisper-identity whisper-identity))) - -(defn get-by-id-cljs - [whisper-identity] - (-> @realm/account-realm - (realm/get-by-field :contact :whisper-identity whisper-identity) - (realm/single-clj :contact))) - -(defn save - [contact update?] - (realm/save @realm/account-realm :contact contact update?)) - -(defn delete - [{:keys [whisper-identity]}] - (realm/write @realm/account-realm - #(realm/delete @realm/account-realm (get-by-id whisper-identity)))) - -(defn exists? - [whisper-identity] - (realm/exists? @realm/account-realm :contact :whisper-identity whisper-identity)) diff --git a/src/status_im/data_store/realm/local_storage.cljs b/src/status_im/data_store/realm/local_storage.cljs deleted file mode 100644 index 30e94aa128..0000000000 --- a/src/status_im/data_store/realm/local_storage.cljs +++ /dev/null @@ -1,10 +0,0 @@ -(ns status-im.data-store.realm.local-storage - (:require [status-im.data-store.realm.core :as realm])) - -(defn get-by-chat-id - [chat-id] - (realm/single-clj (realm/get-by-field @realm/account-realm :local-storage :chat-id chat-id) :local-storage)) - -(defn save - [local-storage] - (realm/save @realm/account-realm :local-storage local-storage true)) diff --git a/src/status_im/data_store/realm/requests.cljs b/src/status_im/data_store/realm/requests.cljs deleted file mode 100644 index 56d749e0a9..0000000000 --- a/src/status_im/data_store/realm/requests.cljs +++ /dev/null @@ -1,27 +0,0 @@ -(ns status-im.data-store.realm.requests - (:require [status-im.data-store.realm.core :as realm])) - -(defn get-all-unanswered - [] - (-> @realm/account-realm - (realm/get-by-field :request :status "open") - (realm/all-clj :request))) - -(defn save - [request] - (realm/save @realm/account-realm :request request true)) - -(defn- get-by-message-id - [chat-id message-id] - (-> @realm/account-realm - (realm/get-by-fields :request :and [[:chat-id chat-id] - [:message-id message-id]]) - realm/single)) - -(defn mark-as-answered - [chat-id message-id] - (realm/write @realm/account-realm - (fn [] - (-> (get-by-message-id chat-id message-id) - (.-status) - (set! "answered"))))) diff --git a/src/status_im/data_store/requests.cljs b/src/status_im/data_store/requests.cljs index 4d34d16bb4..3c32ae0e27 100644 --- a/src/status_im/data_store/requests.cljs +++ b/src/status_im/data_store/requests.cljs @@ -1,20 +1,25 @@ (ns status-im.data-store.requests - (:require [cljs.core.async :as async] - [re-frame.core :as re-frame] - [status-im.data-store.realm.core :as core] - [status-im.data-store.realm.requests :as data-store])) + (:require [re-frame.core :as re-frame] + [status-im.data-store.realm.core :as core])) (re-frame/reg-cofx :data-store/get-unanswered-requests (fn [cofx _] - (assoc cofx :stored-unanswered-requests (data-store/get-all-unanswered)))) + (assoc cofx :stored-unanswered-requests (-> @core/account-realm + (core/get-by-field :request :status "open") + (core/all-clj :request))))) -(re-frame/reg-fx - :data-store/save-request - (fn [request] - (async/go (async/>! core/realm-queue #(data-store/save request))))) +(defn save-request-tx + "Returns tx function for saving request" + [request] + (fn [realm] + (core/create realm :request request true))) -(re-frame/reg-fx - :data-store/mark-request-as-answered - (fn [{:keys [chat-id message-id]}] - (async/go (async/>! core/realm-queue #(data-store/mark-as-answered chat-id message-id))))) +(defn mark-request-as-answered-tx + "Given chat-id and message-id, returns tx function for marking request as answered" + [chat-id message-id] + (fn [realm] + (some-> (core/get-by-fields realm :request :and [[:chat-id chat-id] + [:message-id message-id]]) + core/single + (aset "status" "answered")))) diff --git a/src/status_im/ui/screens/accounts/events.cljs b/src/status_im/ui/screens/accounts/events.cljs index 532d8e8038..05c75df05d 100644 --- a/src/status_im/ui/screens/accounts/events.cljs +++ b/src/status_im/ui/screens/accounts/events.cljs @@ -17,7 +17,8 @@ [status-im.transport.message.core :as transport] status-im.ui.screens.accounts.create.navigation [status-im.chat.models :as chat.models] - [status-im.ui.screens.accounts.utils :as accounts.utils])) + [status-im.ui.screens.accounts.utils :as accounts.utils] + [status-im.data-store.accounts :as accounts-store])) ;;;; COFX @@ -55,8 +56,8 @@ :network network :networks networks :address address)] - {:db (assoc-in db [:accounts/accounts address] enriched-account) - :data-store/save-account enriched-account})) + {:db (assoc-in db [:accounts/accounts address] enriched-account) + :data-store/base-tx [(accounts-store/save-account-tx enriched-account)]})) ;; TODO(janherich) we have this handler here only because of the tests, refactor/improve tests ASAP (handlers/register-handler-fx @@ -103,13 +104,13 @@ (fn [{{:accounts/keys [accounts] :networks/keys [networks] :as db} :db} [_ id]] (let [current-account (get accounts id) new-account (assoc current-account :networks networks)] - {:db (assoc-in db [:accounts/accounts id] new-account) - :data-store/save-account new-account}))) + {:db (assoc-in db [:accounts/accounts id] new-account) + :data-store/base-tx [(accounts-store/save-account-tx new-account)]}))) (defn update-settings [settings {{:keys [account/account] :as db} :db :as cofx}] (let [new-account (assoc account :settings settings)] - {:db (assoc db :account/account new-account) - :data-store/save-account new-account})) + {:db (assoc db :account/account new-account) + :data-store/base-tx [(accounts-store/save-account-tx new-account)]})) (handlers/register-handler-fx :send-account-update-if-needed @@ -160,4 +161,4 @@ (handlers/register-handler-fx :wallet-set-up-passed (fn [cofx] - (accounts.utils/account-update {:wallet-set-up-passed? true} cofx))) \ No newline at end of file + (accounts.utils/account-update {:wallet-set-up-passed? true} cofx))) diff --git a/src/status_im/ui/screens/accounts/utils.cljs b/src/status_im/ui/screens/accounts/utils.cljs index 9abc5e841e..d465c3a0fc 100644 --- a/src/status_im/ui/screens/accounts/utils.cljs +++ b/src/status_im/ui/screens/accounts/utils.cljs @@ -1,7 +1,8 @@ (ns status-im.ui.screens.accounts.utils (:require [status-im.transport.message.core :as transport] [status-im.utils.handlers-macro :as handlers-macro] - [status-im.transport.message.v1.contact :as message.contact])) + [status-im.transport.message.v1.contact :as message.contact] + [status-im.data-store.accounts :as accounts-store])) (defn account-update "Takes effects (containing :db) + new account fields, adds all effects necessary for account update. @@ -11,8 +12,11 @@ ([new-account-fields after-update-event {:keys [db] :as cofx}] (let [current-account (:account/account db) new-account (merge current-account new-account-fields) - fx {:db (assoc db :account/account new-account) - :data-store/save-account (assoc new-account :after-update-event after-update-event)} + fx {:db (assoc db :account/account new-account) + :data-store/base-tx [(accounts-store/save-account-tx + (assoc new-account + :after-update-event + after-update-event))]} {:keys [name photo-path]} new-account] (if (or (:name new-account-fields) (:photo-path new-account-fields)) (handlers-macro/merge-fx cofx fx (transport/send (message.contact/ContactUpdate. name photo-path) nil)) diff --git a/src/status_im/ui/screens/browser/events.cljs b/src/status_im/ui/screens/browser/events.cljs index 44c4e4c4ed..8a756cef2f 100644 --- a/src/status_im/ui/screens/browser/events.cljs +++ b/src/status_im/ui/screens/browser/events.cljs @@ -3,7 +3,8 @@ [status-im.utils.handlers :as handlers] [re-frame.core :as re-frame] [status-im.utils.random :as random] - [status-im.i18n :as i18n])) + [status-im.i18n :as i18n] + [status-im.data-store.browser :as browser-store])) (handlers/register-handler-fx :initialize-browsers @@ -28,8 +29,9 @@ (defn add-browser-fx [{:keys [db now]} browser] (let [new-browser (get-new-browser browser now)] - {:db (update-in db [:browser/browsers (:browser-id new-browser)] merge new-browser) - :data-store/save-browser new-browser})) + {:db (update-in db [:browser/browsers (:browser-id new-browser)] + merge new-browser) + :data-store/tx [(browser-store/save-browser-tx new-browser)]})) (handlers/register-handler-fx :open-dapp-in-browser @@ -69,5 +71,5 @@ :remove-browser [re-frame/trim-v] (fn [{:keys [db]} [browser-id]] - {:db (update-in db [:browser/browsers] dissoc browser-id) - :data-store/remove-browser browser-id})) + {:db (update-in db [:browser/browsers] dissoc browser-id) + :data-store/tx [(browser-store/remove-browser-tx browser-id)]})) diff --git a/src/status_im/ui/screens/contacts/core.cljs b/src/status_im/ui/screens/contacts/core.cljs index ec64e91709..3ba282bbb2 100644 --- a/src/status_im/ui/screens/contacts/core.cljs +++ b/src/status_im/ui/screens/contacts/core.cljs @@ -3,7 +3,8 @@ [status-im.utils.handlers-macro :as handlers-macro] [status-im.data-store.messages :as data-store.messages] [status-im.chat.models :as chat.models] - [status-im.constants :as constants])) + [status-im.constants :as constants] + [status-im.data-store.contacts :as contacts-store])) (defn receive-contact-request [public-key @@ -21,8 +22,10 @@ :chat-id public-key :contact-info (prn-str contact-props)}] (handlers-macro/merge-fx cofx - {:db (update-in db [:contacts/contacts public-key] merge contact-props) - :data-store/save-contact contact-props} + {:db (update-in db [:contacts/contacts public-key] + merge contact-props) + :data-store/tx [(contacts-store/save-contact-tx + contact-props)]} (chat.models/upsert-chat chat-props))))) (defn receive-contact-request-confirmation @@ -38,14 +41,16 @@ chat-props {:name name :chat-id public-key}] (handlers-macro/merge-fx cofx - {:db (update-in db [:contacts/contacts public-key] merge contact-props) - :data-store/save-contact contact-props} + {:db (update-in db [:contacts/contacts public-key] + merge contact-props) + :data-store/tx [(contacts-store/save-contact-tx + contact-props)]} (chat.models/upsert-chat chat-props))))) (defn- update-contact [{:keys [whisper-identity] :as contact} {:keys [db]}] (when (get-in db [:contacts/contacts whisper-identity]) - {:db (update-in db [:contacts/contacts whisper-identity] merge contact) - :data-store/save-contact contact})) + {:db (update-in db [:contacts/contacts whisper-identity] merge contact) + :data-store/tx [(contacts-store/save-contact-tx contact)]})) (defn receive-contact-update [chat-id public-key {:keys [name profile-image]} {:keys [db now] :as cofx}] (let [{:keys [chats current-public-key]} db] diff --git a/src/status_im/ui/screens/contacts/events.cljs b/src/status_im/ui/screens/contacts/events.cljs index 25b0a988a7..22e2b6d75d 100644 --- a/src/status_im/ui/screens/contacts/events.cljs +++ b/src/status_im/ui/screens/contacts/events.cljs @@ -16,14 +16,15 @@ [status-im.chat.models :as chat.models] [status-im.transport.message.core :as transport] [status-im.transport.message.v1.contact :as message.v1.contact] - [status-im.ui.screens.add-new.new-chat.db :as new-chat.db])) + [status-im.ui.screens.add-new.new-chat.db :as new-chat.db] + [status-im.data-store.contacts :as contacts-store])) ;;;; Handlers (defn- update-contact [{:keys [whisper-identity] :as contact} {:keys [db]}] (when (get-in db [:contacts/contacts whisper-identity]) - {:db (update-in db [:contacts/contacts whisper-identity] merge contact) - :data-store/save-contact contact})) + {:db (update-in db [:contacts/contacts whisper-identity] merge contact) + :data-store/tx [(contacts-store/save-contact-tx contact)]})) (handlers/register-handler-fx :load-contacts @@ -35,10 +36,11 @@ (defn- add-new-contact [{:keys [whisper-identity] :as contact} {:keys [db]}] (let [new-contact (assoc contact :pending? false)] - {:db (-> db - (update-in [:contacts/contacts whisper-identity] merge new-contact) - (assoc-in [:contacts/new-identity] "")) - :data-store/save-contact new-contact})) + {:db (-> db + (update-in [:contacts/contacts whisper-identity] + merge new-contact) + (assoc-in [:contacts/new-identity] "")) + :data-store/tx [(contacts-store/save-contact-tx new-contact)]})) (defn- own-info [db] (let [{:keys [name photo-path address]} (:account/account db) @@ -103,8 +105,8 @@ :remove-contact (fn [{:keys [db]} [_ whisper-identity]] (when-let [contact (get-in db [:contacts/contacts whisper-identity])] - {:db (update db :contacts/contacts dissoc whisper-identity) - :data-store/delete-contact contact}))) + {:db (update db :contacts/contacts dissoc whisper-identity) + :data-store/tx [(contacts-store/delete-contact-tx whisper-identity)]}))) (handlers/register-handler-db :open-contact-toggle-list diff --git a/src/status_im/ui/screens/group/events.cljs b/src/status_im/ui/screens/group/events.cljs index 4c021ef7da..f36d2a8fd3 100644 --- a/src/status_im/ui/screens/group/events.cljs +++ b/src/status_im/ui/screens/group/events.cljs @@ -3,7 +3,8 @@ [re-frame.core :as re-frame] [status-im.utils.js-resources :as js-res] [status-im.utils.handlers :as handlers] - [status-im.ui.screens.group.navigation])) + [status-im.ui.screens.group.navigation] + [status-im.data-store.contact-groups :as contact-groups-store])) ;;;; COFX @@ -45,8 +46,8 @@ :order (count contact-groups) :timestamp now :contacts selected-contacts}] - {:db (assoc-in db [:group/contact-groups group-id] new-group) - :data-store/save-contact-group new-group}))) + {:db (assoc-in db [:group/contact-groups group-id] new-group) + :data-store/tx [(contact-groups-store/save-contact-group-tx new-group)]}))) (defn add-default-groups [{:keys [db now default-groups]}] @@ -62,8 +63,9 @@ existing-groups (:group/contact-groups db) groups-to-add (select-keys new-groups (set/difference (set (keys new-groups)) (set (keys existing-groups))))] - {:db (update db :group/contact-groups merge groups-to-add) - :data-store/save-contact-groups (vals groups-to-add)})) + {:db (update db :group/contact-groups merge groups-to-add) + :data-store/tx [(contact-groups-store/save-contact-groups-tx + (vals groups-to-add))]})) (handlers/register-handler-fx :load-contact-groups @@ -74,18 +76,21 @@ (handlers/register-handler-fx :set-contact-group-name (fn [{{:keys [new-chat-name] :group/keys [contact-group-id] :as db} :db} _] - {:db (assoc-in db - [:group/contact-groups contact-group-id :name] - new-chat-name) - :data-store/save-contact-group-property [contact-group-id :name new-chat-name]})) + {:db (assoc-in db + [:group/contact-groups contact-group-id :name] + new-chat-name) + :data-store/tx [(contact-groups-store/save-contact-group-tx + {:group-id contact-group-id + :name new-chat-name})]})) (handlers/register-handler-fx :add-selected-contacts-to-group (fn [{{:group/keys [contact-groups contact-group-id selected-contacts] :as db} :db} _] - {:db (update-in db - [:group/contact-groups contact-group-id :contacts] - #(into [] (set (concat % selected-contacts)))) - :data-store/add-contacts-to-contact-group [contact-group-id selected-contacts]})) + {:db (update-in db + [:group/contact-groups contact-group-id :contacts] + #(into [] (set (concat % selected-contacts)))) + :data-store/tx [(contact-groups-store/add-contacts-to-contact-group-tx + contact-group-id selected-contacts)]})) (handlers/register-handler-fx :remove-contact-from-group @@ -94,5 +99,5 @@ (let [group (-> db (get-in [:group/contact-groups group-id]) (update :contacts (partial remove #(= whisper-identity %))))] - {:db (assoc-in db [:group/contact-groups group-id] group) - :data-store/save-contact-group group}))) + {:db (assoc-in db [:group/contact-groups group-id] group) + :data-store/tx [(contact-groups-store/save-contact-group-tx group)]})))