From 2ffdeb1fa285cc56f98993adf0f686c12c498fa1 Mon Sep 17 00:00:00 2001 From: BalogunofAfrica <45393944+BalogunofAfrica@users.noreply.github.com> Date: Thu, 23 May 2024 12:33:33 +0100 Subject: [PATCH] fix(wallets): no routes render on input amount (#20112) * fix: no routes found error when disabling chains and current amount exceeds new max amount * fix: disable button on routes loading * add no-routes-found component --------- Co-authored-by: Brian Sztamfater --- .../contexts/wallet/send/events.cljs | 32 +++++++++++++++---- .../wallet/send/input_amount/view.cljs | 30 ++++++++++------- src/status_im/contexts/wallet/send/utils.cljs | 16 +++++++++- .../contexts/wallet/send/utils_test.cljs | 12 +++---- 4 files changed, 65 insertions(+), 25 deletions(-) diff --git a/src/status_im/contexts/wallet/send/events.cljs b/src/status_im/contexts/wallet/send/events.cljs index 3a95683b8c..960544fcc5 100644 --- a/src/status_im/contexts/wallet/send/events.cljs +++ b/src/status_im/contexts/wallet/send/events.cljs @@ -64,7 +64,7 @@ :token-networks-ids token-networks-ids :tx-type tx-type :receiver? false}) - (send-utils/reset-network-amounts-to-zero + (send-utils/reset-loading-network-amounts-to-zero sender-network-values)) receiver-network-values (if routes-available? (send-utils/network-amounts @@ -74,7 +74,7 @@ :token-networks-ids token-networks-ids :tx-type tx-type :receiver? true}) - (send-utils/reset-network-amounts-to-zero + (send-utils/reset-loading-network-amounts-to-zero receiver-network-values)) network-links (when routes-available? (send-utils/network-links chosen-route @@ -93,9 +93,9 @@ (rf/reg-event-fx :wallet/suggested-routes-error (fn [{:keys [db]} [error]] (let [cleaned-sender-network-values (-> (get-in db [:wallet :ui :send :sender-network-values]) - (send-utils/reset-network-amounts-to-zero)) + (send-utils/reset-loading-network-amounts-to-zero)) cleaned-receiver-network-values (-> (get-in db [:wallet :ui :send :receiver-network-values]) - (send-utils/reset-network-amounts-to-zero))] + (send-utils/reset-loading-network-amounts-to-zero))] {:db (-> db (update-in [:wallet :ui :send] dissoc @@ -299,9 +299,27 @@ (rf/reg-event-fx :wallet/disable-from-networks (fn [{:keys [db]} [chain-ids]] - {:db (-> db - (assoc-in [:wallet :ui :send :disabled-from-chain-ids] chain-ids) - (assoc-in [:wallet :ui :send :loading-suggested-routes?] true))})) + {:db (assoc-in db [:wallet :ui :send :disabled-from-chain-ids] chain-ids)})) + +(rf/reg-event-fx :wallet/reset-network-amounts-to-zero + (fn [{:keys [db]}] + (let [sender-network-values (get-in db [:wallet :ui :send :sender-network-values]) + receiver-network-values (get-in db [:wallet :ui :send :receiver-network-values]) + disabled-from-chain-ids (get-in db [:wallet :ui :send :disabled-from-chain-ids]) + sender-network-values (send-utils/reset-network-amounts-to-zero + {:network-amounts sender-network-values + :disabled-chain-ids disabled-from-chain-ids}) + receiver-network-values (send-utils/reset-network-amounts-to-zero + {:network-amounts receiver-network-values + :disabled-chain-ids []})] + {:db (-> db + (assoc-in [:wallet :ui :send :sender-network-values] sender-network-values) + (assoc-in [:wallet :ui :send :receiver-network-values] receiver-network-values) + (update-in [:wallet :ui :send] + dissoc + :network-links + (when (empty? sender-network-values) :sender-network-values) + (when (empty? receiver-network-values) :receiver-network-values)))}))) (rf/reg-event-fx :wallet/get-suggested-routes (fn [{:keys [db now]} [{:keys [amount]}]] diff --git a/src/status_im/contexts/wallet/send/input_amount/view.cljs b/src/status_im/contexts/wallet/send/input_amount/view.cljs index 9bc148844c..c103444f47 100644 --- a/src/status_im/contexts/wallet/send/input_amount/view.cljs +++ b/src/status_im/contexts/wallet/send/input_amount/view.cljs @@ -116,6 +116,17 @@ {:content (fn [] [unpreferred-networks-alert/view {:on-confirm on-confirm}])}])) + +(defn- no-routes-found + [] + [rn/view {:style style/no-routes-found-container} + [quo/info-message + {:type :error + :icon :i/alert + :size :default + :style {:margin-top 15}} + (i18n/label :t/no-routes-found)]]) + (defn view ;; crypto-decimals, limit-crypto and initial-crypto-currency? args are needed ;; for component tests only @@ -274,7 +285,9 @@ (set-input-state #(controlled-input/set-upper-limit % current-limit))) [current-limit]) (rn/use-effect - #(when input-error (debounce/clear-all)) + (fn [] + (when input-error (debounce/clear-all)) + (when (and limit-insufficient? routes) (rf/dispatch [:wallet/reset-network-amounts-to-zero]))) [input-error]) [rn/view {:style style/screen @@ -326,26 +339,21 @@ sender-network-values token-not-supported-in-receiver-networks?) [token-not-available token-symbol receiver-networks token-networks]) - (when (or loading-routes? (seq route)) + (when (or loading-routes? fee-formatted) [estimated-fees {:loading-routes? loading-routes? :fees fee-formatted :amount amount-text}]) - (when (or no-routes-found? limit-insufficient?) - [rn/view {:style style/no-routes-found-container} - [quo/info-message - {:type :error - :icon :i/alert - :size :default - :style {:margin-top 15}} - (i18n/label :t/no-routes-found)]]) + (when (and (or no-routes-found? limit-insufficient?) (not-empty sender-network-values)) + [no-routes-found]) [quo/bottom-actions {:actions :one-action :button-one-label (if should-try-again? (i18n/label :t/try-again) button-one-label) :button-one-props (merge (when-not should-try-again? button-one-props) - {:disabled? (and (not should-try-again?) confirm-disabled?) + {:disabled? (or loading-routes? + (and (not should-try-again?) confirm-disabled?)) :on-press (cond should-try-again? #(let [input-amount (controlled-input/input-value diff --git a/src/status_im/contexts/wallet/send/utils.cljs b/src/status_im/contexts/wallet/send/utils.cljs index fff924c4d0..ae0ae17625 100644 --- a/src/status_im/contexts/wallet/send/utils.cljs +++ b/src/status_im/contexts/wallet/send/utils.cljs @@ -83,7 +83,7 @@ (def ^:private available-networks-count (count (set (keys network-priority-score)))) -(defn reset-network-amounts-to-zero +(defn reset-loading-network-amounts-to-zero [network-amounts] (map (fn [network-amount] @@ -93,6 +93,20 @@ :type :default))) network-amounts)) +(defn reset-network-amounts-to-zero + [{:keys [network-amounts disabled-chain-ids]}] + (map + (fn [network-amount] + (let [disabled-chain-ids-set (set disabled-chain-ids) + disabled? (contains? disabled-chain-ids-set + (:chain-id network-amount))] + (cond-> network-amount + (and (not= (:type network-amount) :add) + (not= (:type network-amount) :not-available)) + (assoc :total-amount (money/bignumber "0") + :type (if disabled? :disabled :default))))) + network-amounts)) + (defn network-amounts [{:keys [network-values disabled-chain-ids receiver-networks token-networks-ids tx-type receiver?]}] (let [disabled-set (set disabled-chain-ids) diff --git a/src/status_im/contexts/wallet/send/utils_test.cljs b/src/status_im/contexts/wallet/send/utils_test.cljs index e9f734764b..2dd032e6aa 100644 --- a/src/status_im/contexts/wallet/send/utils_test.cljs +++ b/src/status_im/contexts/wallet/send/utils_test.cljs @@ -209,13 +209,13 @@ :disabled-chain-ids disabled-chain-ids})))))) -(deftest test-reset-network-amounts-to-zero +(deftest test-reset-loading-network-amounts-to-zero (testing "Correctly resets loading network amounts to zero and changes type to default" (let [network-amounts [{:chain-id 1 :total-amount (money/bignumber "100") :type :loading} {:chain-id 10 :total-amount (money/bignumber "200") :type :default}] expected [{:chain-id 1 :total-amount (money/bignumber "0") :type :default} {:chain-id 10 :total-amount (money/bignumber "200") :type :default}] - result (utils/reset-network-amounts-to-zero network-amounts) + result (utils/reset-loading-network-amounts-to-zero network-amounts) comparisons (map #(map/deep-compare %1 %2) expected result)] @@ -226,7 +226,7 @@ {:chain-id 10 :total-amount (money/bignumber "0") :type :disabled}] expected [{:chain-id 1 :total-amount (money/bignumber "100") :type :default} {:chain-id 10 :total-amount (money/bignumber "0") :type :disabled}] - result (utils/reset-network-amounts-to-zero network-amounts) + result (utils/reset-loading-network-amounts-to-zero network-amounts) comparisons (map #(map/deep-compare %1 %2) expected result)] @@ -235,7 +235,7 @@ (testing "Processes an empty list without error" (let [network-amounts [] expected [] - result (utils/reset-network-amounts-to-zero network-amounts) + result (utils/reset-loading-network-amounts-to-zero network-amounts) comparisons (map #(map/deep-compare %1 %2) expected result)] @@ -246,7 +246,7 @@ {:chain-id 10 :total-amount (money/bignumber "200") :type :loading}] expected [{:chain-id 1 :total-amount (money/bignumber "0") :type :default} {:chain-id 10 :total-amount (money/bignumber "0") :type :default}] - result (utils/reset-network-amounts-to-zero network-amounts) + result (utils/reset-loading-network-amounts-to-zero network-amounts) comparisons (map #(map/deep-compare %1 %2) expected result)] @@ -261,7 +261,7 @@ {:chain-id 10 :total-amount (money/bignumber "200") :type :default} {:chain-id 42161 :total-amount (money/bignumber "0") :type :default} {:chain-id 59144 :total-amount (money/bignumber "0") :type :disabled}] - result (utils/reset-network-amounts-to-zero network-amounts) + result (utils/reset-loading-network-amounts-to-zero network-amounts) comparisons (map #(map/deep-compare %1 %2) expected result)]