From 8f37885e18dcaef6a0276e2175d1dd87185f6282 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 14 Jun 2016 17:36:39 +0300 Subject: [PATCH] staged command preview Former-commit-id: 02f37e6c976db010c219bb1ba81e54d036f6ab0e --- resources/commands.js | 12 ++++++++ resources/status.js | 1 + src/status_im/chat/handlers.cljs | 31 +++++++++++++++----- src/status_im/chat/views/staged_command.cljs | 15 +++++----- src/status_im/commands/handlers/jail.cljs | 17 +++++++++-- src/status_im/commands/utils.cljs | 2 ++ src/status_im/components/jail.cljs | 1 + 7 files changed, 60 insertions(+), 19 deletions(-) diff --git a/resources/commands.js b/resources/commands.js index 552d036ce5..b4820adf51 100644 --- a/resources/commands.js +++ b/resources/commands.js @@ -160,6 +160,18 @@ status.response({ event: "save-password", params: [params.value] }; + }, + preview: function (params) { + return status.components.text( + { + style: { + marginTop: 5, + marginHorizontal: 0, + fontSize: 14, + fontFamily: "font", + color: "black" + } + }, "*****"); } }); diff --git a/resources/status.js b/resources/status.js index e62559fae6..c25535b159 100644 --- a/resources/status.js +++ b/resources/status.js @@ -25,6 +25,7 @@ Command.prototype.create = function (com) { this.color = com.color; this.icon = com.icon; this.params = com.params || []; + this.preview = com.preview; this.addToCatalog(); return this; diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index f6686ff3b9..2c5892a579 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -45,18 +45,18 @@ (defn invoke-suggestions-handler! [{:keys [current-chat-id] :as db} _] (let [commands (get-in db [:chats current-chat-id :commands]) - {:keys [command content]} (get-in db [:chats current-chat-id :command-input])] - (let [path [(if (commands command) :commands :responses) + {:keys [command content]} (get-in db [:chats current-chat-id :command-input]) + path [(if (commands command) :commands :responses) (:name command) :params 0 :suggestions] - params {:value content}] - (j/call current-chat-id - path - params - #(dispatch [:suggestions-handler {:command command - :content content} %]))))) + params {:value content}] + (j/call current-chat-id + path + params + #(dispatch [:suggestions-handler {:command command + :content content} %])))) (register-handler :set-chat-command-content (after invoke-suggestions-handler!) @@ -67,7 +67,22 @@ [{:keys [current-chat-id] :as db} text] (assoc-in db [:chats current-chat-id :input-text] text)) + +(defn invoke-command-preview! + [{:keys [current-chat-id staged-command] :as db} _] + (let [commands (get-in db [:chats current-chat-id :commands]) + {:keys [command content]} staged-command + path [(if (commands command) :commands :responses) + (:name command) + :preview] + params {:value content}] + (j/call current-chat-id + path + params + #(dispatch [:command-preview current-chat-id %])))) + (register-handler :stage-command + (after invoke-command-preview!) (fn [{:keys [current-chat-id] :as db} _] (let [db (update-input-text db nil) {:keys [command content]} diff --git a/src/status_im/chat/views/staged_command.cljs b/src/status_im/chat/views/staged_command.cljs index 92f72b0508..424367c56e 100644 --- a/src/status_im/chat/views/staged_command.cljs +++ b/src/status_im/chat/views/staged_command.cljs @@ -1,9 +1,9 @@ (ns status-im.chat.views.staged-command (:require [re-frame.core :refer [subscribe dispatch]] [status-im.components.react :refer [view - image - text - touchable-highlight]] + image + text + touchable-highlight]] [status-im.resources :as res] [status-im.chat.styles.input :as st])) @@ -21,8 +21,7 @@ :onPress #(cancel-command-input staged-command)} [image {:source res/icon-close-gray :style st/staged-command-cancel-icon}]]] - [text {:style st/staged-command-content} - ;; TODO isn't smart - (if (= (:command command) :keypair-password) - "******" - (:content staged-command))]]])) + (if-let [preview (:preview staged-command)] + preview + [text {:style st/staged-command-content} + (:content staged-command)])]])) diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index 26192ec502..436f1d75cd 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -5,8 +5,6 @@ [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]])) @@ -40,13 +38,25 @@ (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]) + ;; todo show error? + nil)) + +(defn command-preview + [db [chat-id response-json]] + (if-let [response (json->cljs response-json)] + (let [path [:chats chat-id :staged-commands] + commands-cnt (count (get-in db path))] + ;; todo (dec commands-cnt) looks like hack have to find better way to + ;; do this + (update-in db (conj path (dec commands-cnt)) assoc + :preview (generate-hiccup response) + :preview-string (str response))) db)) (reg-handler :init-render-command! init-render-command!) @@ -55,3 +65,4 @@ (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!)) +(reg-handler :command-preview command-preview) diff --git a/src/status_im/commands/utils.cljs b/src/status_im/commands/utils.cljs index aa79a29eb4..bf4639f5dc 100644 --- a/src/status_im/commands/utils.cljs +++ b/src/status_im/commands/utils.cljs @@ -1,6 +1,8 @@ (ns status-im.commands.utils (:require [clojure.set :as set] [clojure.walk :as w] + [status-im.components.react :refer [text scroll-view view + image touchable-highlight]] [re-frame.core :refer [register-handler dispatch trim-v debug]])) (defn json->cljs [json] diff --git a/src/status_im/components/jail.cljs b/src/status_im/components/jail.cljs index 416968912f..d58cf9e2c1 100644 --- a/src/status_im/components/jail.cljs +++ b/src/status_im/components/jail.cljs @@ -29,6 +29,7 @@ Command.prototype.create = function (com) { this.color = com.color; this.icon = com.icon; this.params = com.params || []; + this.preview = com.preview; this.addToCatalog(); return this;