diff --git a/resources/commands.js b/resources/commands.js index bc0a456671..552d036ce5 100644 --- a/resources/commands.js +++ b/resources/commands.js @@ -7,26 +7,6 @@ status.command({ type: status.types.STRING }); -function text(options, s) { - return ["text", options, s]; -} - -function view(options, elements) { - return ["view", options].concat(elements); -} - -function image(options) { - return ["image", options]; -} - -function touchable(options, element) { - return ["touchable", options, element]; -} - -function scrollView(options, elements) { - return ["scroll-view", options].concat(elements); -} - var phones = [ { number: "89171111111", @@ -100,18 +80,27 @@ function phoneSuggestions(params) { } suggestions = ph.map(function (phone) { - return touchable( + return status.components.touchable( {onPress: [status.events.SET_VALUE, phone.number]}, - view(suggestionContainerStyle, - [view(suggestionSubContainerStyle, + status.components.view(suggestionContainerStyle, + [status.components.view(suggestionSubContainerStyle, [ - text({style: valueStyle}, phone.number), - text({style: descriptionStyle}, phone.description) + status.components.text( + {style: valueStyle}, + phone.number + ), + status.components.text( + {style: descriptionStyle}, + phone.description + ) ])]) ); }); - return scrollView(suggestionsContainerStyle(ph.length), suggestions); + return status.components.scrollView( + suggestionsContainerStyle(ph.length), + suggestions + ); } status.response({ diff --git a/resources/status.js b/resources/status.js index e53cb7e174..e62559fae6 100644 --- a/resources/status.js +++ b/resources/status.js @@ -57,6 +57,26 @@ function call(pathStr, paramsStr) { return JSON.stringify(res); } +function text(options, s) { + return ['text', options, s]; +} + +function view(options, elements) { + return ['view', options].concat(elements); +} + +function image(options) { + return ['image', options]; +} + +function touchable(options, element) { + return ['touchable', options, element]; +} + +function scrollView(options, elements) { + return ['scroll-view', options].concat(elements); +} + var status = { command: function (n, d, h) { var command = new Command(); @@ -73,5 +93,12 @@ var status = { }, events: { SET_VALUE: 'set-value' + }, + components: { + view: view, + text: text, + image: image, + touchable: touchable, + scrollView: scrollView } }; diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs new file mode 100644 index 0000000000..26192ec502 --- /dev/null +++ b/src/status_im/commands/handlers/jail.cljs @@ -0,0 +1,57 @@ +(ns status-im.commands.handlers.jail + (:require [re-frame.core :refer [register-handler after dispatch subscribe + trim-v debug]] + [status-im.utils.handlers :as u] + [status-im.utils.utils :refer [http-get toast]] + [status-im.components.jail :as j] + + [status-im.components.react :refer [text scroll-view view + image touchable-highlight]] + [status-im.commands.utils :refer [json->cljs generate-hiccup + reg-handler]])) + +(defn init-render-command! + [_ [chat-id command message-id data]] + (j/call chat-id [command :render] data + (fn [res] + (dispatch [::render-command chat-id message-id (json->cljs res)])))) + +(defn render-command + [db [chat-id message-id markup]] + (let [hiccup (generate-hiccup markup)] + (assoc-in db [:rendered-commands chat-id message-id] hiccup))) + +(def console-events + {:save-password #(dispatch [:save-password %]) + :sign-up #(dispatch [:sign-up %]) + :confirm-sign-up #(dispatch [:sign-up-confirm %])}) + +(def regular-events {}) + +(defn command-nadler! + [_ [{:keys [to]} response]] + (let [{:keys [event params]} (json->cljs response) + events (if (= "console" to) + (merge regular-events console-events) + regular-events)] + (when-let [handler (events (keyword event))] + (apply handler params)))) + +(defn suggestions-handler + [db [_ response-json]] + (let [response (json->cljs response-json)] + (println response) + (assoc db :current-suggestion (generate-hiccup response)))) + +(defn suggestions-events-handler! + [db [[n data]]] + (case (keyword n) + :set-value (dispatch [:set-chat-command-content data]) + db)) + +(reg-handler :init-render-command! init-render-command!) +(reg-handler ::render-command render-command) + +(reg-handler :command-handler! (u/side-effect! command-nadler!)) +(reg-handler :suggestions-handler suggestions-handler) +(reg-handler :suggestions-event! (u/side-effect! suggestions-events-handler!)) diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers/loading.cljs similarity index 52% rename from src/status_im/commands/handlers.cljs rename to src/status_im/commands/handlers/loading.cljs index 8d63368e00..3f8ce95762 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -1,21 +1,12 @@ -(ns status-im.commands.handlers +(ns status-im.commands.handlers.loading (:require [re-frame.core :refer [register-handler after dispatch subscribe trim-v debug]] [status-im.utils.handlers :as u] - [status-im.components.react :as r] [status-im.utils.utils :refer [http-get toast]] [clojure.string :as s] [status-im.persistence.realm :as realm] [status-im.components.jail :as j] - [clojure.walk :as w] - [status-im.components.react :refer [text scroll-view view - image touchable-highlight]] - [clojure.set :as set])) - -(defn reg-handler - ([name handler] (reg-handler name nil handler)) - ([name middleware handler] - (register-handler name [debug trim-v middleware] handler))) + [status-im.commands.utils :refer [json->cljs reg-handler]])) (def commands-js "commands.js") @@ -50,11 +41,6 @@ ;; todo tbd hashing algorithm (hash file)) -(defn json->cljs [json] - (if (= json "undefined") - nil - (js->clj (.parse js/JSON json) :keywordize-keys true))) - (defn parse-commands! [_ [identity file]] (j/parse identity file (fn [result] @@ -93,76 +79,6 @@ (name reason) details])))) -(defn init-render-command! - [_ [chat-id command message-id data]] - (j/call chat-id [command :render] data - (fn [res] - (dispatch [::render-command chat-id message-id (json->cljs res)])))) - -(def elements - {:text text - :view view - :scroll-view scroll-view - :image image - :touchable touchable-highlight}) - -(defn get-element [n] - (elements (keyword (.toLowerCase n)))) - -(def events #{:onPress}) - -(defn wrap-event [event] - #(dispatch [:suggestions-event! event])) - -(defn check-events [m] - (let [ks (set (keys m)) - evs (set/intersection ks events)] - (reduce #(update %1 %2 wrap-event) m evs))) - -(defn generate-hiccup [markup] - ;; todo implement validation - (w/prewalk - (fn [el] - (if (and (vector? el) (string? (first el))) - (-> el - (update 0 get-element) - (update 1 check-events)) - el)) - markup)) - -(defn render-command - [db [chat-id message-id markup]] - (let [hiccup (generate-hiccup markup)] - (assoc-in db [:rendered-commands chat-id message-id] hiccup))) - -(def console-events - {:save-password #(dispatch [:save-password %]) - :sign-up #(dispatch [:sign-up %]) - :confirm-sign-up #(dispatch [:sign-up-confirm %])}) - -(def regular-events {}) - -(defn command-nadler! - [_ [{:keys [to]} response]] - (let [{:keys [event params]} (json->cljs response) - events (if (= "console" to) - (merge regular-events console-events) - regular-events)] - (when-let [handler (events (keyword event))] - (apply handler params)))) - -(defn suggestions-handler - [db [_ response-json]] - (let [response (json->cljs response-json)] - (println response) - (assoc db :current-suggestion (generate-hiccup response)))) - -(defn suggestions-events-handler! - [db [[n data]]] - (case (keyword n) - :set-value (dispatch [:set-chat-command-content data]) - db)) - (reg-handler :load-commands! (u/side-effect! load-commands!)) (reg-handler ::fetch-commands! (u/side-effect! fetch-commands!)) @@ -177,10 +93,3 @@ add-commands) (reg-handler ::loading-failed! (u/side-effect! loading-failed!)) - -(reg-handler :init-render-command! init-render-command!) -(reg-handler ::render-command render-command) - -(reg-handler :command-handler! (u/side-effect! command-nadler!)) -(reg-handler :suggestions-handler suggestions-handler) -(reg-handler :suggestions-event! (u/side-effect! suggestions-events-handler!)) diff --git a/src/status_im/commands/utils.cljs b/src/status_im/commands/utils.cljs new file mode 100644 index 0000000000..aa79a29eb4 --- /dev/null +++ b/src/status_im/commands/utils.cljs @@ -0,0 +1,46 @@ +(ns status-im.commands.utils + (:require [clojure.set :as set] + [clojure.walk :as w] + [re-frame.core :refer [register-handler dispatch trim-v debug]])) + +(defn json->cljs [json] + (if (= json "undefined") + nil + (js->clj (.parse js/JSON json) :keywordize-keys true))) + + +(def elements + {:text text + :view view + :scroll-view scroll-view + :image image + :touchable touchable-highlight}) + +(defn get-element [n] + (elements (keyword (.toLowerCase n)))) + +(def events #{:onPress}) + +(defn wrap-event [event] + #(dispatch [:suggestions-event! event])) + +(defn check-events [m] + (let [ks (set (keys m)) + evs (set/intersection ks events)] + (reduce #(update %1 %2 wrap-event) m evs))) + +(defn generate-hiccup [markup] + ;; todo implement validation + (w/prewalk + (fn [el] + (if (and (vector? el) (string? (first el))) + (-> el + (update 0 get-element) + (update 1 check-events)) + el)) + markup)) + +(defn reg-handler + ([name handler] (reg-handler name nil handler)) + ([name middleware handler] + (register-handler name [debug trim-v middleware] handler))) diff --git a/src/status_im/components/jail.cljs b/src/status_im/components/jail.cljs index 66b596619d..416968912f 100644 --- a/src/status_im/components/jail.cljs +++ b/src/status_im/components/jail.cljs @@ -61,6 +61,26 @@ function call(pathStr, paramsStr) { return JSON.stringify(res); } +function text(options, s) { + return ['text', options, s]; +} + +function view(options, elements) { + return ['view', options].concat(elements); +} + +function image(options) { + return ['image', options]; +} + +function touchable(options, element) { + return ['touchable', options, element]; +} + +function scrollView(options, elements) { + return ['scroll-view', options].concat(elements); +} + var status = { command: function (n, d, h) { var command = new Command(); @@ -77,11 +97,22 @@ var status = { }, events: { SET_VALUE: 'set-value' + }, + components: { + view: view, + text: text, + image: image, + touchable: touchable, + scrollView: scrollView } };") -(def jail (.-Jail (.-NativeModules r/react))) -(.init jail status-js) +(def jail + (when (exists? (.-NativeModules r/react)) + (.-Jail (.-NativeModules r/react)))) + +(when jail + (.init jail status-js)) (defn parse [chat-id file callback] (.parse jail chat-id file callback)) diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index 02d7d9e760..bb48cec2d5 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -16,7 +16,8 @@ status-im.new-group.handlers status-im.participants.handlers status-im.protocol.handlers - status-im.commands.handlers)) + status-im.commands.handlers.loading + status-im.commands.handlers.jail)) ;; -- Middleware ------------------------------------------------------------ ;; diff --git a/test/cljs/status_im/test/commands/handlers.cljs b/test/cljs/status_im/test/commands/handlers.cljs index 92225b5f9e..cf356be4e2 100644 --- a/test/cljs/status_im/test/commands/handlers.cljs +++ b/test/cljs/status_im/test/commands/handlers.cljs @@ -1,6 +1,6 @@ (ns status-im.test.commands.handlers (:require [cljs.test :refer-macros [deftest is]] - [status-im.commands.handlers :as h])) + [status-im.commands.handlers.loading :as h])) (deftest test-validate-hash (let [file "some-js"