From 4c4cf7a5e4fabbc952598f939830eb9b1a884a1b Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 19 Oct 2017 22:05:44 +0200 Subject: [PATCH] fix /request preview fix duplication of /send command in suggestions --- src/status_im/chat/subs.cljs | 25 +++++-- src/status_im/chat/views/input/input.cljs | 12 ++-- .../chat/views/message/request_message.cljs | 65 +++++++++---------- 3 files changed, 57 insertions(+), 45 deletions(-) diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index c9c5dd22f6..3d19e63e25 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -53,6 +53,19 @@ (fn [[chats id] [_ k chat-id]] (get-in chats [(or chat-id id) k]))) +(defn get-suggested-commands + [[commands requests]] + (let [vec->map-by-name #(into {} (map vector (map :name %) %)) + commands-map (vec->map-by-name commands) + requests-map (vec->map-by-name requests)] + (vals (merge commands-map requests-map)))) + +(reg-sub + :get-suggested-commands + :<- [:chat :possible-commands] + :<- [:chat :possible-requests] + get-suggested-commands) + (reg-sub :get-current-chat-id (fn [db] @@ -219,8 +232,10 @@ (fn [db [_ message-id]] (get-in db [:message-data :preview message-id :markup]))) -(reg-sub :get-message-preview - (fn [db [_ message-id]] - (let [preview (subscribe [:get-message-preview-markup message-id])] - (when-let [markup @preview] - (commands-utils/generate-hiccup markup))))) +(reg-sub + :get-message-preview + (fn [[_ message-id]] + [(subscribe [:get-message-preview-markup message-id])]) + (fn [[markup]] + (when markup + (commands-utils/generate-hiccup markup)))) diff --git a/src/status_im/chat/views/input/input.cljs b/src/status_im/chat/views/input/input.cljs index 24c33e3b1e..3311f98699 100644 --- a/src/status_im/chat/views/input/input.cljs +++ b/src/status_im/chat/views/input/input.cljs @@ -35,8 +35,7 @@ (chat-utils/command-name command)]]]) (defview commands-view [] - [commands [:chat :possible-commands] - requests [:chat :possible-requests] + [all-commands [:get-suggested-commands] show-suggestions? [:show-suggestions?]] [view style/commands-root [view style/command-list-icon-container @@ -48,11 +47,10 @@ [scroll-view {:horizontal true :showsHorizontalScrollIndicator false :keyboardShouldPersistTaps :always} - (let [all-commands (apply conj commands requests)] - [view style/commands - (for [[index command] (map-indexed vector all-commands)] - ^{:key (str "command-" index)} - [command-view (= index 0) command])])]]) + [view style/commands + (for [[index command] (map-indexed vector all-commands)] + ^{:key (str "command-" index)} + [command-view (= index 0) command])]]]) (defn- basic-text-input [_] (let [input-text (subscribe [:chat :input-text]) diff --git a/src/status_im/chat/views/message/request_message.cljs b/src/status_im/chat/views/message/request_message.cljs index 41777a5908..ea85066889 100644 --- a/src/status_im/chat/views/message/request_message.cljs +++ b/src/status_im/chat/views/message/request_message.cljs @@ -72,37 +72,36 @@ [icon command-icon st/command-request-image])]]))}))) (defview message-content-command-request - [{:keys [message-id chat-id]}] - (letsubs [commands [:get-commands-for-chat chat-id] - requests [:chat-actions :possible-requests] - answered? [:is-request-answered? message-id] + [{:keys [message-id chat-id content from incoming-group] :as message}] + (letsubs [commands [:get-commands-for-chat chat-id] + requests [:chat-actions :possible-requests] + answered? [:is-request-answered? message-id] status-initialized? [:get :status-module-initialized?] - markup [:get-message-preview message-id]] - (fn [{:keys [message-id content from incoming-group] :as message}] - (let [{:keys [prefill prefill-bot-db prefillBotDb params] - text-content :text} content - {:keys [command content]} (commands/replace-name-with-request message commands requests) - command (if (and params command) - (merge command {:prefill prefill - :prefill-bot-db (or prefill-bot-db prefillBotDb)}) - command) - on-press-handler (if (:execute-immediately? command) - #(dispatch [:execute-command-immediately command]) - (when (and (not answered?) status-initialized?) - #(set-chat-command message-id command)))] - [view st/command-request-view - [touchable-highlight - {:on-press on-press-handler} - [view st/command-request-message-view - (if (and markup - (not (string? markup))) - [view markup] - [text {:style st/style-message-text - :font :default} - (or text-content markup content)])]] - (when (:request-text command) - [view st/command-request-text-view - [text {:style st/style-sub-text - :font :default} - (:request-text command)]]) - [request-button message-id command on-press-handler]])))) + markup [:get-message-preview message-id]] + (let [{:keys [prefill prefill-bot-db prefillBotDb params] + text-content :text} content + {:keys [command content]} (commands/replace-name-with-request message commands requests) + command (if (and params command) + (merge command {:prefill prefill + :prefill-bot-db (or prefill-bot-db prefillBotDb)}) + command) + on-press-handler (if (:execute-immediately? command) + #(dispatch [:execute-command-immediately command]) + (when (and (not answered?) status-initialized?) + #(set-chat-command message-id command)))] + [view st/command-request-view + [touchable-highlight + {:on-press on-press-handler} + [view st/command-request-message-view + (if (and markup + (not (string? markup))) + [view markup] + [text {:style st/style-message-text + :font :default} + (or text-content markup content)])]] + (when (:request-text command) + [view st/command-request-text-view + [text {:style st/style-sub-text + :font :default} + (:request-text command)]]) + [request-button message-id command on-press-handler]])))