From 6a9d1af886d547ce2c96160c4a5f34a845180535 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 9 Jun 2016 14:41:25 +0300 Subject: [PATCH] store commands.js Former-commit-id: 72768fc7ddb430782b17b44b2b956f4828a3d2e9 --- src/status_im/commands/handlers.cljs | 31 ++++++++++++++++---------- src/status_im/navigation/handlers.cljs | 3 +-- src/status_im/persistence/realm.cljs | 12 +++++++++- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index fc9e2a8ad3..f7c713f5e5 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -4,7 +4,8 @@ [status-im.utils.handlers :as u] [status-im.components.react :as r] [status-im.utils.utils :refer [http-get toast]] - [clojure.string :as s])) + [clojure.string :as s] + [status-im.persistence.realm :as realm])) ;; commands loading flow ; ┌────────────────────────────┐ @@ -44,8 +45,8 @@ ; │ │ │ │ ; │ │ │ ▼ ; │ │ │ ┌────────────────────────────┐ -; │ │ ▼ │ save commands │ -; │ │ ┌────────────────────────┐ │ save js ? │ +; │ │ ▼ │ │ +; │ │ ┌────────────────────────┐ │ save commands-js │ ; │ │ │the dapp emit a message │ │ add some API object form │ ; │ └─▶│ saying js is broken │ │ otto to app-db │ ; │ └────────────────────────┘ └────────────────────────────┘ @@ -68,6 +69,12 @@ (def commands-js "commands.js") +(defn load-commands! + [_ [identity]] + (if-let [{:keys [file]} (realm/get-one-by-field :commands :chat-id identity)] + (dispatch [::parse-commands! identity file]) + (dispatch [::fetch-commands! identity]))) + (defn fetch-commands! [db [identity]] (when-let [url (:dapp-url (get-in db [:chats identity]))] @@ -103,27 +110,26 @@ (parse file (fn [result] (let [commands (json->clj result)] - (dispatch [::add-commands identity commands]))) + (dispatch [::add-commands identity file commands]))) #(dispatch [::loading-failed! identity ::error-in-jail %]))) (defn validate-hash [db [identity file]] (let [valid? (= (get-hash-by-dentity db identity) (get-hash-by-file file))] - (println "hsh " (hash file)) (assoc db ::valid-hash valid?))) -(defn save-commands! - [db] - #_(realm/save (::new-commands db))) - (defn add-commands - [db [id commands-obj]] + [db [id _ commands-obj]] (let [commands (:commands commands-obj)] (-> db (update-in [:chats id :commands] merge commands) (assoc db ::new-commands commands)))) +(defn save-commands-js! + [_ [id file]] + (realm/create-object :commands {:chat-id id :file file})) + (defn loading-failed [db [id reason details]] (let [url (get-in db [:chats id :dapp-url])] @@ -133,7 +139,8 @@ (name reason) details])))) -(reg-handler :load-commands! (u/side-effect! fetch-commands!)) +(reg-handler :load-commands! (u/side-effect! load-commands!)) +(reg-handler ::fetch-commands! (u/side-effect! fetch-commands!)) (reg-handler ::validate-hash (after dispatch-loaded!) @@ -142,7 +149,7 @@ (reg-handler ::parse-commands! (u/side-effect! parse-commands!)) (reg-handler ::add-commands - (after save-commands!) + (after save-commands-js!) add-commands) (reg-handler ::loading-failed! (u/side-effect! loading-failed)) diff --git a/src/status_im/navigation/handlers.cljs b/src/status_im/navigation/handlers.cljs index c47134e429..9e249c6186 100644 --- a/src/status_im/navigation/handlers.cljs +++ b/src/status_im/navigation/handlers.cljs @@ -1,6 +1,5 @@ (ns status-im.navigation.handlers - (:require [re-frame.core :refer [register-handler dispatch debug enrich - after]])) + (:require [re-frame.core :refer [register-handler dispatch debug enrich after]])) (defn push-view [db view-id] (-> db diff --git a/src/status_im/persistence/realm.cljs b/src/status_im/persistence/realm.cljs index fa471355b1..fd6e9857ad 100644 --- a/src/status_im/persistence/realm.cljs +++ b/src/status_im/persistence/realm.cljs @@ -1,7 +1,6 @@ (ns status-im.persistence.realm (:require [cljs.reader :refer [read-string]] [status-im.components.styles :refer [default-chat-color]] - [status-im.utils.logging :as log] [status-im.utils.types :refer [to-string]] [status-im.utils.utils :as u]) (:refer-clojure :exclude [exists?])) @@ -56,6 +55,10 @@ :dapp-hash {:type :int :optional true} :last-msg-id "string"}} + {:name :commands + :primaryKey :chat-id + :properties {:chat-id "string" + :file "string"}} {:name :tag :primaryKey :name :properties {:name "string" @@ -98,6 +101,10 @@ ([schema-name obj update?] (.create realm (to-string schema-name) (clj->js obj) update?))) +(defn create-object + [schema-name obj] + (write (fn [] (create schema-name obj true)))) + (defmulti to-query (fn [schema-name operator field value] operator)) @@ -162,3 +169,6 @@ (defn collection->map [collection] (-> (.map collection (fn [object _ _] object)) (js->clj :keywordize-keys true))) + +(defn get-one-by-field [schema-name field value] + (single-cljs (get-by-field schema-name field value)))