From a0b37c4211ed8fdfc241ee7f8217a45e80757ea9 Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Fri, 4 Nov 2016 01:03:27 +0200 Subject: [PATCH] prefill response parameters with request data Former-commit-id: 648a46029429ce7656d3f2cc3c2e9630862f5a1e --- src/status_im/chat/handlers/commands.cljs | 19 +++---- .../chat/handlers/webview_bridge.cljs | 3 -- src/status_im/chat/subs.cljs | 10 ++-- src/status_im/chat/views/message.cljs | 3 -- src/status_im/chat/views/message_input.cljs | 3 +- src/status_im/chat/views/request_message.cljs | 13 +++-- src/status_im/models/commands.cljs | 51 +++++++++++++++---- 7 files changed, 59 insertions(+), 43 deletions(-) diff --git a/src/status_im/chat/handlers/commands.cljs b/src/status_im/chat/handlers/commands.cljs index 331363d0cb..7ab4dfbedc 100644 --- a/src/status_im/chat/handlers/commands.cljs +++ b/src/status_im/chat/handlers/commands.cljs @@ -9,8 +9,7 @@ [status-im.i18n :as i18n] [status-im.utils.datetime :as time] [status-im.utils.random :as random] - [status-im.utils.platform :as platform] - [taoensso.timbre :as log])) + [status-im.utils.platform :as platform])) (defn content-by-command [{:keys [type]} content] @@ -109,7 +108,6 @@ (fn [{:keys [current-chat-id current-account-id] :as db} [_ command-input command]] (let [command-input (or command-input (commands/get-command-input db)) command (or command (commands/get-chat-command db))] - (log/debug "Staging command 1: " command-input command) (dispatch [::start-command-validation! {:command-input command-input :command command :chat-id current-chat-id @@ -150,10 +148,9 @@ (after #(dispatch [:command-edit-mode]))] set-chat-command) -(defn set-response-command [db [_ to-message-id command-key]] - (log/debug "set-response-command: " to-message-id command-key) +(defn set-response-command [db [_ to-message-id command-key params]] (-> db - (commands/set-command-input :responses to-message-id command-key) + (commands/set-command-input :responses to-message-id command-key params) (assoc :canceled-command false))) (register-handler ::set-response-chat-command @@ -165,9 +162,9 @@ (register-handler :set-response-chat-command (u/side-effect! (fn [{:keys [current-chat-id] :as db} - [_ to-message-id command-key]] + [_ to-message-id command-key params]] (when (get-in db [:chats current-chat-id :responses command-key]) - (dispatch [::set-response-chat-command to-message-id command-key]))))) + (dispatch [::set-response-chat-command to-message-id command-key params]))))) (register-handler ::add-validation-errors (after #(dispatch [:fix-response-height])) @@ -261,10 +258,8 @@ (commands/set-command-parameter db name value)))) (register-handler :next-command-parameter - (fn [{:keys [current-chat-id] :as db}] - (-> db - (update-in [:chats current-chat-id :command-input :parameter-idx] inc) - (commands/set-chat-command-content nil)))) + (fn [db _] + (commands/next-command-parameter db))) (register-handler :check-suggestions-trigger! (u/side-effect! diff --git a/src/status_im/chat/handlers/webview_bridge.cljs b/src/status_im/chat/handlers/webview_bridge.cljs index a3ec25dcd9..6fb6ecea7d 100644 --- a/src/status_im/chat/handlers/webview_bridge.cljs +++ b/src/status_im/chat/handlers/webview_bridge.cljs @@ -19,7 +19,6 @@ (defn scan-qr-handler [{:keys [contacts]} [_ _ data]] - (log/debug "scaned qr" data) (let [data' (read-string data) data'' (cond (map? data') data' @@ -39,7 +38,6 @@ (assoc db :webview-bridge bridge))) (defn contacts-click-handler [whisper-identity action params] - (log/debug "Contact clicked: " whisper-identity action params) (dispatch [:navigate-back]) (when action (if (= whisper-identity :qr-scan) @@ -57,7 +55,6 @@ :parameter-idx 0 :params {"amount" (:amount params)} :to-message-id nil}] - (log/debug "Staging command: " command-key command command-input) (dispatch [:stage-command command-input command]))))) diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index 0f1da993a7..a8a5c4ff06 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -9,10 +9,7 @@ [status-im.chat.views.plain-message :as plain-message] [status-im.chat.views.command :as command] [status-im.constants :refer [content-type-status]] - [status-im.chat.utils :as cu] - [status-im.utils.datetime :as time] - [status-im.utils.platform :refer [platform-specific]] - [taoensso.timbre :as log])) + [status-im.utils.platform :refer [platform-specific]])) (register-sub :chat-properties (fn [db [_ properties]] @@ -116,9 +113,8 @@ (let [command (subscribe [:get-chat-command]) chat-id (subscribe [:get-current-chat-id])] (reaction - (let [path [:chats @chat-id :command-input :parameter-idx] - n (get-in @db path)] - (when n (nth (:params @command) n))))))) + (let [parameter-index (commands/get-command-parameter-index @db @chat-id)] + (when parameter-index (nth (:params @command) parameter-index))))))) (register-sub :get-chat-command-content (fn [db _] diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index 0dd95842bb..1ad87feedb 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -130,9 +130,6 @@ :contact-address (if outgoing to from) :current-chat-id current-chat-id}]])) -(defn set-chat-command [message-id command] - (dispatch [:set-response-chat-command message-id (keyword (:name command))])) - (defn message-view [message content] [view (st/message-view message) diff --git a/src/status_im/chat/views/message_input.cljs b/src/status_im/chat/views/message_input.cljs index 9460afa57b..235bd52cb2 100644 --- a/src/status_im/chat/views/message_input.cljs +++ b/src/status_im/chat/views/message_input.cljs @@ -13,8 +13,7 @@ [status-im.chat.styles.plain-message :as st-message] [status-im.chat.styles.response :as st-response] [reagent.core :as r] - [clojure.string :as str] - [taoensso.timbre :as log])) + [clojure.string :as str])) (defn send-button [{:keys [on-press accessibility-label]}] [touchable-highlight {:on-press on-press diff --git a/src/status_im/chat/views/request_message.cljs b/src/status_im/chat/views/request_message.cljs index 3af3e9fb9b..681e1ce79c 100644 --- a/src/status_im/chat/views/request_message.cljs +++ b/src/status_im/chat/views/request_message.cljs @@ -9,14 +9,14 @@ touchable-highlight]] [status-im.chat.styles.message :as st] [status-im.models.commands :refer [parse-command-request]] - [status-im.components.animation :as anim] - [taoensso.timbre :as log])) + [status-im.components.animation :as anim])) (def request-message-icon-scale-delay 600) (defn set-chat-command [message-id command] - (log/debug "set-chat-command: " message-id command) - (dispatch [:set-response-chat-command message-id (keyword (:name command))])) + (let [command-key (keyword (:name command)) + params (:set-params command)] + (dispatch [:set-response-chat-command message-id command-key params]))) (defn label [command] (when command @@ -78,8 +78,11 @@ status-initialized? (subscribe [:get :status-module-initialized?])] (fn [{:keys [message-id content from incoming-group]}] (let [commands @commands-atom + params (:params content) {:keys [command content]} (parse-command-request commands content) - _ (log/debug "message-content: " command content)] + command (if (and params command) + (merge command {:set-params params}) + command)] [view st/comand-request-view [touchable-highlight {:on-press (when (and (not @answered?) @status-initialized?) diff --git a/src/status_im/models/commands.cljs b/src/status_im/models/commands.cljs index 264af071bc..75d3239435 100644 --- a/src/status_im/models/commands.cljs +++ b/src/status_im/models/commands.cljs @@ -1,7 +1,6 @@ (ns status-im.models.commands (:require [status-im.db :as db] - [tailrecursion.priority-map :refer [priority-map-by]] - [taoensso.timbre :as log])) + [tailrecursion.priority-map :refer [priority-map-by]])) (defn get-commands [{:keys [current-chat-id] :as db}] (or (get-in db [:chats current-chat-id :commands]) {})) @@ -16,7 +15,7 @@ (defn set-chat-command-content [{:keys [current-chat-id] :as db} content] - (assoc-in db [:chats current-chat-id :command-input :content] content)) + (assoc-in db (db/chat-command-content-path current-chat-id) content)) (defn set-command-parameter [{:keys [current-chat-id] :as db} name value] @@ -29,16 +28,47 @@ (defn get-command-input [{:keys [current-chat-id] :as db}] (get-in db [:chats current-chat-id :command-input])) +(defn add-params [command params] + (let [command-params (:params command) + command-params (vec (map (fn [param] + (let [param-key (keyword (:name param)) + value (get params param-key)] + (assoc param :value value))) command-params))] + (assoc command :params command-params))) + (defn set-command-input ([db type command-key] (set-command-input db type nil command-key)) - ([{:keys [current-chat-id] :as db} type message-id command-key] - (update-in db [:chats current-chat-id :command-input] merge - {:content nil - :command (get-response-or-command type db command-key) - :parameter-idx 0 - :params nil - :to-message-id message-id}))) + ([db type message-id command-key] + (set-command-input db type message-id command-key nil)) + ([{:keys [current-chat-id] :as db} type message-id command-key params] + (let [command (-> (get-response-or-command type db command-key) + (add-params params)) + first-parameter (get (:params command) 0) + value (:value first-parameter)] + (update-in db [:chats current-chat-id :command-input] merge + {:content value + :command command + :parameter-idx 0 + :params params + :to-message-id message-id})))) + +(defn get-command-parameter-index + ([{:keys [current-chat-id] :as db}] + (get-command-parameter-index db current-chat-id)) + ([db chat-id] + (get-in db [:chats chat-id :command-input :parameter-idx]))) + +(defn next-command-parameter + [{:keys [current-chat-id] :as db}] + (let [parameter-index (get-command-parameter-index db) + command (get-chat-command db) + next-parameter (get (:params command) (inc parameter-index)) + value (:value next-parameter)] + (-> db + (update-in [:chats current-chat-id :command-input :parameter-idx] inc) + (set-chat-command-content value)))) + (defn get-chat-command-to-message-id [{:keys [current-chat-id] :as db}] @@ -70,5 +100,4 @@ (update content :command #((keyword %) commands))) (defn parse-command-request [commands content] - (log/debug "parse-command-request: " commands content) (update content :command #((keyword %) commands)))