Merge pull request #461 from status-im/feature/prefill-response-parameters-#417

prefill response parameters with request data
This commit is contained in:
Roman Volosovskyi 2016-11-17 09:55:35 +02:00 committed by GitHub
commit 23c1a1ae30
8 changed files with 60 additions and 44 deletions

View File

@ -9,8 +9,7 @@
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.utils.datetime :as time] [status-im.utils.datetime :as time]
[status-im.utils.random :as random] [status-im.utils.random :as random]
[status-im.utils.platform :as platform] [status-im.utils.platform :as platform]))
[taoensso.timbre :as log]))
(defn content-by-command (defn content-by-command
[{:keys [type]} content] [{:keys [type]} content]
@ -109,7 +108,6 @@
(fn [{:keys [current-chat-id current-account-id] :as db} [_ command-input command]] (fn [{:keys [current-chat-id current-account-id] :as db} [_ command-input command]]
(let [command-input (or command-input (commands/get-command-input db)) (let [command-input (or command-input (commands/get-command-input db))
command (or command (commands/get-chat-command 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 (dispatch [::start-command-validation! {:command-input command-input
:command command :command command
:chat-id current-chat-id :chat-id current-chat-id
@ -150,10 +148,9 @@
(after #(dispatch [:command-edit-mode]))] (after #(dispatch [:command-edit-mode]))]
set-chat-command) set-chat-command)
(defn set-response-command [db [_ to-message-id command-key]] (defn set-response-command [db [_ to-message-id command-key params]]
(log/debug "set-response-command: " to-message-id command-key)
(-> db (-> 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))) (assoc :canceled-command false)))
(register-handler ::set-response-chat-command (register-handler ::set-response-chat-command
@ -165,9 +162,9 @@
(register-handler :set-response-chat-command (register-handler :set-response-chat-command
(u/side-effect! (u/side-effect!
(fn [{:keys [current-chat-id] :as db} (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]) (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 (register-handler ::add-validation-errors
(after #(dispatch [:fix-response-height])) (after #(dispatch [:fix-response-height]))
@ -263,10 +260,8 @@
(commands/set-command-parameter db name value)))) (commands/set-command-parameter db name value))))
(register-handler :next-command-parameter (register-handler :next-command-parameter
(fn [{:keys [current-chat-id] :as db}] (fn [db _]
(-> db (commands/next-command-parameter db)))
(update-in [:chats current-chat-id :command-input :parameter-idx] inc)
(commands/set-chat-command-content nil))))
(register-handler :check-suggestions-trigger! (register-handler :check-suggestions-trigger!
(u/side-effect! (u/side-effect!

View File

@ -20,7 +20,6 @@
(defn scan-qr-handler (defn scan-qr-handler
[{:keys [contacts]} [_ _ data]] [{:keys [contacts]} [_ _ data]]
(log/debug "scaned qr" data)
(let [data' (read-string data) (let [data' (read-string data)
data'' (cond data'' (cond
(map? data') data' (map? data') data'
@ -40,7 +39,6 @@
(assoc db :webview-bridge bridge))) (assoc db :webview-bridge bridge)))
(defn contacts-click-handler [whisper-identity action params] (defn contacts-click-handler [whisper-identity action params]
(log/debug "Contact clicked: " whisper-identity action params)
(dispatch [:navigate-back]) (dispatch [:navigate-back])
(when action (when action
(if (= whisper-identity :qr-scan) (if (= whisper-identity :qr-scan)
@ -58,7 +56,6 @@
:parameter-idx 0 :parameter-idx 0
:params {"amount" (:amount params)} :params {"amount" (:amount params)}
:to-message-id nil}] :to-message-id nil}]
(log/debug "Staging command: " command-key command command-input)
(dispatch [:stage-command command-input command]))))) (dispatch [:stage-command command-input command])))))

View File

@ -9,10 +9,7 @@
[status-im.chat.views.plain-message :as plain-message] [status-im.chat.views.plain-message :as plain-message]
[status-im.chat.views.command :as command] [status-im.chat.views.command :as command]
[status-im.constants :refer [content-type-status]] [status-im.constants :refer [content-type-status]]
[status-im.chat.utils :as cu] [status-im.utils.platform :refer [platform-specific]]))
[status-im.utils.datetime :as time]
[status-im.utils.platform :refer [platform-specific]]
[taoensso.timbre :as log]))
(register-sub :chat-properties (register-sub :chat-properties
(fn [db [_ properties]] (fn [db [_ properties]]
@ -116,9 +113,8 @@
(let [command (subscribe [:get-chat-command]) (let [command (subscribe [:get-chat-command])
chat-id (subscribe [:get-current-chat-id])] chat-id (subscribe [:get-current-chat-id])]
(reaction (reaction
(let [path [:chats @chat-id :command-input :parameter-idx] (let [parameter-index (commands/get-command-parameter-index @db @chat-id)]
n (get-in @db path)] (when parameter-index (nth (:params @command) parameter-index)))))))
(when n (nth (:params @command) n)))))))
(register-sub :get-chat-command-content (register-sub :get-chat-command-content
(fn [db _] (fn [db _]

View File

@ -130,9 +130,6 @@
:contact-address (if outgoing to from) :contact-address (if outgoing to from)
:current-chat-id current-chat-id}]])) :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 (defn message-view
[message content] [message content]
[view (st/message-view message) [view (st/message-view message)

View File

@ -13,8 +13,7 @@
[status-im.chat.styles.plain-message :as st-message] [status-im.chat.styles.plain-message :as st-message]
[status-im.chat.styles.response :as st-response] [status-im.chat.styles.response :as st-response]
[reagent.core :as r] [reagent.core :as r]
[clojure.string :as str] [clojure.string :as str]))
[taoensso.timbre :as log]))
(defn send-button [{:keys [on-press accessibility-label]}] (defn send-button [{:keys [on-press accessibility-label]}]
[touchable-highlight {:on-press on-press [touchable-highlight {:on-press on-press

View File

@ -9,14 +9,14 @@
touchable-highlight]] touchable-highlight]]
[status-im.chat.styles.message :as st] [status-im.chat.styles.message :as st]
[status-im.models.commands :refer [parse-command-request]] [status-im.models.commands :refer [parse-command-request]]
[status-im.components.animation :as anim] [status-im.components.animation :as anim]))
[taoensso.timbre :as log]))
(def request-message-icon-scale-delay 600) (def request-message-icon-scale-delay 600)
(defn set-chat-command [message-id command] (defn set-chat-command [message-id command]
(log/debug "set-chat-command: " message-id command) (let [command-key (keyword (:name command))
(dispatch [:set-response-chat-command message-id (keyword (:name command))])) params (:set-params command)]
(dispatch [:set-response-chat-command message-id command-key params])))
(defn label [command] (defn label [command]
(when command (when command
@ -78,8 +78,11 @@
status-initialized? (subscribe [:get :status-module-initialized?])] status-initialized? (subscribe [:get :status-module-initialized?])]
(fn [{:keys [message-id content from incoming-group]}] (fn [{:keys [message-id content from incoming-group]}]
(let [commands @commands-atom (let [commands @commands-atom
params (:params content)
{:keys [command content]} (parse-command-request commands 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 [view st/comand-request-view
[touchable-highlight [touchable-highlight
{:on-press (when (and (not @answered?) @status-initialized?) {:on-press (when (and (not @answered?) @status-initialized?)

View File

@ -18,7 +18,7 @@
(->> (keywordize-keys (apply hash-map (split s #"[;=]"))) (->> (keywordize-keys (apply hash-map (split s #"[;=]")))
(map (fn [[k v]] (map (fn [[k v]]
[k (if (= k :params) [k (if (= k :params)
(read-string v) (keywordize-keys (read-string v))
v)])) v)]))
(into {}))) (into {})))

View File

@ -1,7 +1,6 @@
(ns status-im.models.commands (ns status-im.models.commands
(:require [status-im.db :as db] (:require [status-im.db :as db]
[tailrecursion.priority-map :refer [priority-map-by]] [tailrecursion.priority-map :refer [priority-map-by]]))
[taoensso.timbre :as log]))
(defn get-commands [{:keys [current-chat-id] :as db}] (defn get-commands [{:keys [current-chat-id] :as db}]
(or (get-in db [:chats current-chat-id :commands]) {})) (or (get-in db [:chats current-chat-id :commands]) {}))
@ -16,7 +15,7 @@
(defn set-chat-command-content (defn set-chat-command-content
[{:keys [current-chat-id] :as db} 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 (defn set-command-parameter
[{:keys [current-chat-id] :as db} name value] [{:keys [current-chat-id] :as db} name value]
@ -29,16 +28,47 @@
(defn get-command-input [{:keys [current-chat-id] :as db}] (defn get-command-input [{:keys [current-chat-id] :as db}]
(get-in db [:chats current-chat-id :command-input])) (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 (defn set-command-input
([db type command-key] ([db type command-key]
(set-command-input db type nil command-key)) (set-command-input db type nil command-key))
([{:keys [current-chat-id] :as db} type message-id command-key] ([db type message-id command-key]
(update-in db [:chats current-chat-id :command-input] merge (set-command-input db type message-id command-key nil))
{:content nil ([{:keys [current-chat-id] :as db} type message-id command-key params]
:command (get-response-or-command type db command-key) (let [command (-> (get-response-or-command type db command-key)
:parameter-idx 0 (add-params params))
:params nil first-parameter (get (:params command) 0)
:to-message-id message-id}))) 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 (defn get-chat-command-to-message-id
[{:keys [current-chat-id] :as db}] [{:keys [current-chat-id] :as db}]
@ -70,5 +100,4 @@
(update content :command #((keyword %) commands))) (update content :command #((keyword %) commands)))
(defn parse-command-request [commands content] (defn parse-command-request [commands content]
(log/debug "parse-command-request: " commands content)
(update content :command #((keyword %) commands))) (update content :command #((keyword %) commands)))