From db7bbb12b10109fc8868795e46250420b0faf326 Mon Sep 17 00:00:00 2001 From: Goran Jovic Date: Mon, 25 Feb 2019 16:40:28 +0100 Subject: [PATCH] refactor #7571 - decoupled request transaction from send transaction flow --- .../chat/commands/impl/transactions.cljs | 2 +- .../wallet/choose_recipient/events.cljs | 38 ++++++++++--------- .../ui/screens/wallet/components/views.cljs | 15 ++++---- .../ui/screens/wallet/request/views.cljs | 10 +++-- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/status_im/chat/commands/impl/transactions.cljs b/src/status_im/chat/commands/impl/transactions.cljs index f3923837fca..85d6d011f52 100644 --- a/src/status_im/chat/commands/impl/transactions.cljs +++ b/src/status_im/chat/commands/impl/transactions.cljs @@ -318,7 +318,7 @@ :amount-text amount :amount-error error) (choose-recipient.events/fill-request-details - (transaction-details recipient-contact symbol)) + (transaction-details recipient-contact symbol) false) (update-in [:wallet :send-transaction] dissoc :id :password :wrong-password?)) ;; TODO(janherich) - refactor wallet send events, updating gas price diff --git a/src/status_im/ui/screens/wallet/choose_recipient/events.cljs b/src/status_im/ui/screens/wallet/choose_recipient/events.cljs index 7c54340a6a9..41f39573bd2 100644 --- a/src/status_im/ui/screens/wallet/choose_recipient/events.cljs +++ b/src/status_im/ui/screens/wallet/choose_recipient/events.cljs @@ -20,22 +20,24 @@ (defn- find-address-name [db address] (:name (contact.db/find-contact-by-address (:contacts/contacts db) address))) -(defn- fill-request-details [db {:keys [address name value symbol gas gasPrice public-key from-chat?]}] +(defn- fill-request-details [db {:keys [address name value symbol gas gasPrice public-key from-chat?]} request?] {:pre [(not (nil? address))]} - (let [name (or name (find-address-name db address))] - (update-in - db [:wallet :send-transaction] - (fn [{old-symbol :symbol :as old-transaction}] - (let [symbol-changed? (not= old-symbol symbol)] - (cond-> (assoc old-transaction :to address :to-name name :public-key public-key) - value (assoc :amount value) - symbol (assoc :symbol symbol) - (and gas symbol-changed?) (assoc :gas (money/bignumber gas)) - from-chat? (assoc :from-chat? from-chat?) - (and gasPrice symbol-changed?) - (assoc :gas-price (money/bignumber gasPrice)) - (and symbol (not gasPrice) symbol-changed?) - (assoc :gas-price (ethereum/estimate-gas symbol)))))))) + (let [name (or name (find-address-name db address)) + data-path (if request? + [:wallet :request-transaction] + [:wallet :send-transaction])] + (update-in db data-path + (fn [{old-symbol :symbol :as old-transaction}] + (let [symbol-changed? (not= old-symbol symbol)] + (cond-> (assoc old-transaction :to address :to-name name :public-key public-key) + value (assoc :amount value) + symbol (assoc :symbol symbol) + (and gas symbol-changed?) (assoc :gas (money/bignumber gas)) + from-chat? (assoc :from-chat? from-chat?) + (and gasPrice symbol-changed?) + (assoc :gas-price (money/bignumber gasPrice)) + (and symbol (not gasPrice) symbol-changed?) + (assoc :gas-price (ethereum/estimate-gas symbol)))))))) (defn- extract-details "First try to parse as EIP681 URI, if not assume this is an address directly. @@ -105,7 +107,7 @@ symbol-changed? (and old-symbol new-symbol (not= old-symbol new-symbol))] (cond-> {:db db :dispatch [:navigate-back]} - (and address valid-network?) (update :db #(fill-request-details % details)) + (and address valid-network?) (update :db #(fill-request-details % details false)) symbol-changed? (changed-asset old-symbol new-symbol) (and old-amount new-amount (not= old-amount new-amount)) (changed-amount-warning old-amount new-amount) ;; NOTE(goranjovic) - the next line is there is because QR code scanning switches the amount to ETH @@ -119,6 +121,6 @@ (handlers/register-handler-fx :wallet/fill-request-from-contact - (fn [{db :db} [_ {:keys [address name public-key]}]] - {:db (fill-request-details db {:address address :name name :public-key public-key}) + (fn [{db :db} [_ {:keys [address name public-key]} request?]] + {:db (fill-request-details db {:address address :name name :public-key public-key} request?) :dispatch [:navigate-back]})) diff --git a/src/status_im/ui/screens/wallet/components/views.cljs b/src/status_im/ui/screens/wallet/components/views.cljs index d4a6488823c..7106c067399 100644 --- a/src/status_im/ui/screens/wallet/components/views.cljs +++ b/src/status_im/ui/screens/wallet/components/views.cljs @@ -198,8 +198,8 @@ :accessibility-label (if request? :contact-address-text :recipient-address-text)} (ethereum/normalized-address address)]]]))) -(defn render-contact [contact] - [list/touchable-item #(re-frame/dispatch [:wallet/fill-request-from-contact contact]) +(defn render-contact [contact request?] + [list/touchable-item #(re-frame/dispatch [:wallet/fill-request-from-contact contact request?]) [list/item [photos/photo (:photo-path contact) {:size list.styles/image-size}] [list/item-content @@ -210,13 +210,14 @@ (ethereum/normalized-address (:address contact))]]]]) (views/defview recent-recipients [] - (views/letsubs [contacts [:contacts/active]] + (views/letsubs [contacts [:contacts/active] + {:keys [request?]} [:get-screen-params :recent-recipients]] [simple-screen [toolbar (i18n/label :t/recipient)] [react/view styles/recent-recipients [list/flat-list {:data contacts :key-fn :address - :render-fn render-contact}]]])) + :render-fn #(render-contact % request?)}]]])) (defn contact-code [] (let [content (reagent/atom nil)] @@ -251,11 +252,11 @@ (i18n/label :t/camera-access-error))) 50)}])) -(defn- on-choose-recipient [contact-only?] +(defn- on-choose-recipient [contact-only? request?] (list-selection/show {:title (i18n/label :t/wallet-choose-recipient) :options (concat [{:label (i18n/label :t/recent-recipients) - :action #(re-frame/dispatch [:navigate-to :recent-recipients])}] + :action #(re-frame/dispatch [:navigate-to :recent-recipients {:request? request?}])}] (when-not contact-only? [{:label (i18n/label :t/scan-qr) :action request-camera-permissions} @@ -263,7 +264,7 @@ :action #(re-frame/dispatch [:navigate-to :contact-code])}]))})) (defn recipient-selector [{:keys [name address disabled? contact-only? request? modal?]}] - [cartouche {:on-press #(on-choose-recipient contact-only?) + [cartouche {:on-press #(on-choose-recipient contact-only? request?) :disabled? disabled? :icon :main-icons/more :icon-opts {:accessibility-label :choose-contact-button}} diff --git a/src/status_im/ui/screens/wallet/request/views.cljs b/src/status_im/ui/screens/wallet/request/views.cljs index 4f8d15b970c..15abef4cb1c 100644 --- a/src/status_im/ui/screens/wallet/request/views.cljs +++ b/src/status_im/ui/screens/wallet/request/views.cljs @@ -21,7 +21,9 @@ [status-im.utils.ethereum.eip681 :as eip681] [status-im.utils.utils :as utils] [status-im.utils.ethereum.tokens :as tokens] - [status-im.ui.screens.wallet.utils :as wallet.utils])) + [status-im.ui.screens.wallet.utils :as wallet.utils] + [status-im.ui.screens.chat.photos :as photos] + [status-im.ui.components.list.styles :as list.styles])) ;; Request screen @@ -29,7 +31,8 @@ ;; TODO(jeluard) both send and request flows should be merged (views/letsubs [network [:account/network] {:keys [to to-name public-key]} [:wallet.send/transaction] - {:keys [amount amount-error amount-text symbol]} [:wallet.request/transaction] + {:keys [amount amount-error amount-text symbol + to to-name public-key]} [:wallet.request/transaction] network-status [:network-status] all-tokens [:wallet/all-tokens] scroll (atom nil)] @@ -43,8 +46,7 @@ [components/recipient-selector {:contact-only? true :address to :name to-name - :request? true - :modal? false}] + :request? true}] [components/asset-selector {:disabled? false :type :request :symbol symbol}]