fix: address is lost on re-render (#18593)

Signed-off-by: Brian Sztamfater <brian@status.im>
This commit is contained in:
Brian Sztamfater 2024-02-08 16:29:05 -03:00 committed by GitHub
parent 523a5809d0
commit 025c38ae88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 110 additions and 55 deletions

View File

@ -149,7 +149,9 @@
(not hidden-screen?) (not hidden-screen?)
(:current-chat-id db)) (:current-chat-id db))
(conj [:chat/close]))}) (conj [:chat/close]))})
{:db (assoc db :view-id go-to-view-id) {:db (-> db
(assoc :view-id go-to-view-id)
(dissoc :modal-view-ids))
:navigate-to go-to-view-id})) :navigate-to go-to-view-id}))
(rf/defn shell-navigate-back (rf/defn shell-navigate-back
@ -174,7 +176,8 @@
shell.constants/close-screen-with-slide-to-right-animation))} shell.constants/close-screen-with-slide-to-right-animation))}
(when (and current-chat-id community-id) (when (and current-chat-id community-id)
{:dispatch [:shell/add-switcher-card shell.constants/community-screen community-id]})) {:dispatch [:shell/add-switcher-card shell.constants/community-screen community-id]}))
{:navigate-back nil}))) {:navigate-back nil
:db (dissoc db :modal-view-ids)})))
(rf/defn floating-screen-opened (rf/defn floating-screen-opened
{:events [:shell/floating-screen-opened]} {:events [:shell/floating-screen-opened]}

View File

@ -52,7 +52,7 @@
{:db (-> db {:db (-> db
(assoc-in [:wallet :ui :send :send-account-address] address) (assoc-in [:wallet :ui :send :send-account-address] address)
(update-in [:wallet :ui :send] dissoc :to-address)) (update-in [:wallet :ui :send] dissoc :to-address))
:fx [[:navigate-to-within-stack [:wallet-select-asset stack-id]]]})) :fx [[:dispatch [:navigate-to-within-stack [:wallet-select-asset stack-id]]]]}))
(rf/reg-event-fx :wallet/clean-send-address (rf/reg-event-fx :wallet/clean-send-address
(fn [{:keys [db]}] (fn [{:keys [db]}]
@ -72,10 +72,11 @@
(assoc-in [:wallet :ui :send :to-address] to-address) (assoc-in [:wallet :ui :send :to-address] to-address)
(assoc-in [:wallet :ui :send :address-prefix] prefix) (assoc-in [:wallet :ui :send :address-prefix] prefix)
(assoc-in [:wallet :ui :send :selected-networks] selected-networks)) (assoc-in [:wallet :ui :send :selected-networks] selected-networks))
:fx [[:navigate-to-within-stack :fx [[:dispatch
(if token [:navigate-to-within-stack
[:wallet-send-input-amount stack-id] (if token
[:wallet-select-asset stack-id])]]}))) [:wallet-send-input-amount stack-id]
[:wallet-select-asset stack-id])]]]})))
(rf/reg-event-fx (rf/reg-event-fx
:wallet/update-receiver-networks :wallet/update-receiver-networks
@ -87,7 +88,8 @@
{:db (-> db {:db (-> db
(update-in [:wallet :ui :send] dissoc :collectible) (update-in [:wallet :ui :send] dissoc :collectible)
(assoc-in [:wallet :ui :send :token] token)) (assoc-in [:wallet :ui :send :token] token))
:fx [[:navigate-to-within-stack [:wallet-send-input-amount stack-id]]]})) :fx [[:dispatch [:wallet/clean-suggested-routes]]
[:dispatch [:navigate-to-within-stack [:wallet-send-input-amount stack-id]]]]}))
(rf/reg-event-fx (rf/reg-event-fx
:wallet/send-select-token-drawer :wallet/send-select-token-drawer
@ -110,7 +112,7 @@
(rf/reg-event-fx :wallet/send-select-amount (rf/reg-event-fx :wallet/send-select-amount
(fn [{:keys [db]} [{:keys [amount stack-id]}]] (fn [{:keys [db]} [{:keys [amount stack-id]}]]
{:db (assoc-in db [:wallet :ui :send :amount] amount) {:db (assoc-in db [:wallet :ui :send :amount] amount)
:fx [[:navigate-to-within-stack [:wallet-transaction-confirmation stack-id]]]})) :fx [[:dispatch [:navigate-to-within-stack [:wallet-transaction-confirmation stack-id]]]]}))
(rf/reg-event-fx :wallet/get-suggested-routes (rf/reg-event-fx :wallet/get-suggested-routes
(fn [{:keys [db now]} [amount]] (fn [{:keys [db now]} [amount]]

View File

@ -45,7 +45,8 @@
:wallet/wallet-send-loading-suggested-routes? false :wallet/wallet-send-loading-suggested-routes? false
:wallet/wallet-send-route {:route []} :wallet/wallet-send-route {:route []}
:wallet/wallet-send-suggested-routes {:candidates []} :wallet/wallet-send-suggested-routes {:candidates []}
:wallet/wallet-send-selected-networks []}) :wallet/wallet-send-selected-networks []
:navigation/current-screen-id :wallet-send-input-amount})
(defn- render (defn- render
[component] [component]

View File

@ -135,13 +135,18 @@
(rf/dispatch [:wallet/clean-selected-token]) (rf/dispatch [:wallet/clean-selected-token])
(rf/dispatch [:navigate-back-within-stack :wallet-send-input-amount])) (rf/dispatch [:navigate-back-within-stack :wallet-send-input-amount]))
fetch-routes (fn [input-num-value current-limit-amount] fetch-routes (fn [input-num-value current-limit-amount]
(rf/dispatch [:wallet/clean-suggested-routes]) (let [current-screen-id (rf/sub [:navigation/current-screen-id])]
(when-not (or (empty? @input-value) ; this check is to prevent effect being triggered when screen is
(<= input-num-value 0) ; loaded but not being shown to the user (deep in the navigation
(> input-num-value current-limit-amount)) ; stack) and avoid undesired behaviors
(debounce/debounce-and-dispatch (when (= current-screen-id :wallet-send-input-amount)
[:wallet/get-suggested-routes @input-value] (if-not (or (empty? @input-value)
100))) (<= input-num-value 0)
(> input-num-value current-limit-amount))
(debounce/debounce-and-dispatch
[:wallet/get-suggested-routes @input-value]
100)
(rf/dispatch [:wallet/clean-suggested-routes])))))
handle-on-confirm (fn [] handle-on-confirm (fn []
(rf/dispatch [:wallet/send-select-amount (rf/dispatch [:wallet/send-select-amount
{:amount @input-value {:amount @input-value

View File

@ -24,7 +24,8 @@
(defn- address-input (defn- address-input
[input-value input-focused?] [input-value input-focused?]
(fn [] (fn []
(let [scanned-address (rf/sub [:wallet/scanned-address]) (let [current-screen-id (rf/sub [:navigation/current-screen-id])
scanned-address (rf/sub [:wallet/scanned-address])
send-address (rf/sub [:wallet/wallet-send-to-address]) send-address (rf/sub [:wallet/wallet-send-to-address])
recipient (rf/sub [:wallet/wallet-send-recipient]) recipient (rf/sub [:wallet/wallet-send-recipient])
recipient-plain-address? (= send-address recipient) recipient-plain-address? (= send-address recipient)
@ -32,29 +33,37 @@
chain-id (rf/sub [:chain-id]) chain-id (rf/sub [:chain-id])
contacts (rf/sub [:contacts/active])] contacts (rf/sub [:contacts/active])]
[quo/address-input [quo/address-input
{:on-focus #(reset! input-focused? true) {:on-focus #(reset! input-focused? true)
:on-blur #(reset! input-focused? false) :on-blur #(reset! input-focused? false)
:on-scan (fn [] :on-scan (fn []
(rn/dismiss-keyboard!) (rn/dismiss-keyboard!)
(rf/dispatch [:wallet/clean-scanned-address]) (rf/dispatch [:wallet/clean-scanned-address])
(rf/dispatch [:open-modal :scan-address])) (rf/dispatch [:open-modal :scan-address]))
:ens-regex constants/regx-ens :ens-regex constants/regx-ens
:scanned-value (or (when recipient-plain-address? send-address) scanned-address) :scanned-value (or (when recipient-plain-address? send-address) scanned-address)
:address-regex constants/regx-multichain-address :address-regex constants/regx-multichain-address
:on-detect-address #(debounce/debounce-and-dispatch :on-detect-address #(when (or (= current-screen-id :wallet-select-address)
[:wallet/validate-address %] (= current-screen-id :scan-address))
300) ; ^ this check is to prevent effect being triggered when screen is
:on-detect-ens (fn [text cb] ; loaded but not being shown to the user (deep in the navigation
; stack) and avoid undesired behaviors
(debounce/debounce-and-dispatch (debounce/debounce-and-dispatch
[:wallet/find-ens text contacts chain-id cb] [:wallet/validate-address %]
300)) 300))
:on-detect-unclassified #(when valid-ens-or-address? :on-detect-ens (fn [text cb]
(rf/dispatch [:wallet/clean-ens-or-address-validation])) (when (or (= current-screen-id :wallet-select-address)
:on-change-text (fn [text] (= current-screen-id :scan-address))
(when (empty? text) ; ^ this check is to prevent effect being triggered when screen
(rf/dispatch [:wallet/clean-local-suggestions])) ; is loaded but not being shown to the user (deep in the
(reset! input-value text)) ; navigation stack) and avoid undesired behaviors
:valid-ens-or-address? valid-ens-or-address?}]))) (debounce/debounce-and-dispatch
[:wallet/find-ens text contacts chain-id cb]
300)))
:on-change-text (fn [text]
(when (empty? text)
(rf/dispatch [:wallet/clean-local-suggestions]))
(reset! input-value text))
:valid-ens-or-address? valid-ens-or-address?}])))
(defn- ens-linked-address (defn- ens-linked-address
[{:keys [address networks theme]}] [{:keys [address networks theme]}]
@ -133,10 +142,6 @@
token (rf/sub [:wallet/wallet-send-token]) token (rf/sub [:wallet/wallet-send-token])
valid-ens-or-address? (boolean (rf/sub [:wallet/valid-ens-or-address?])) valid-ens-or-address? (boolean (rf/sub [:wallet/valid-ens-or-address?]))
{:keys [color]} (rf/sub [:wallet/current-viewing-account])] {:keys [color]} (rf/sub [:wallet/current-viewing-account])]
(rn/use-effect (fn []
(fn []
(rf/dispatch [:wallet/clean-scanned-address])
(rf/dispatch [:wallet/clean-local-suggestions]))))
[floating-button-page/view [floating-button-page/view
{:footer-container-padding 0 {:footer-container-padding 0
:header [account-switcher/view :header [account-switcher/view

View File

@ -137,10 +137,7 @@
(defn- view-internal (defn- view-internal
[_] [_]
(let [on-close (fn [] (let [on-close #(rf/dispatch [:navigate-back-within-stack :wallet-select-asset])]
(rf/dispatch [:wallet/clean-suggested-routes])
(rf/dispatch [:navigate-back-within-stack :wallet-select-asset]))]
(fn [{:keys [theme]}] (fn [{:keys [theme]}]
(let [send-transaction-data (rf/sub [:wallet/wallet-send]) (let [send-transaction-data (rf/sub [:wallet/wallet-send])
token (:token send-transaction-data) token (:token send-transaction-data)

View File

@ -24,38 +24,67 @@
:dispatch-n [[:hide-bottom-sheet]]} :dispatch-n [[:hide-bottom-sheet]]}
(shell.events/shell-navigate-to go-to-view-id screen-params nil nil))) (shell.events/shell-navigate-to go-to-view-id screen-params nil nil)))
(defn- add-view-to-modals
[modal-view-ids new-id]
(if (seq modal-view-ids)
(conj modal-view-ids new-id)
modal-view-ids))
(rf/defn navigate-to-within-stack (rf/defn navigate-to-within-stack
{:events [:navigate-to-within-stack]} {:events [:navigate-to-within-stack]}
[_ comp-id] [{:keys [db]} comp-id]
{:navigate-to-within-stack comp-id}) {:db (update db :modal-view-ids add-view-to-modals (first comp-id))
:fx [[:navigate-to-within-stack comp-id]]})
(re-frame/reg-event-fx :open-modal (re-frame/reg-event-fx :open-modal
(fn [{:keys [db]} [component screen-params]] (fn [{:keys [db]} [component screen-params]]
{:db (-> db {:db (-> db
(assoc :view-id component) (assoc :view-id component)
(assoc :modal-view-ids [component])
(all-screens-params component screen-params)) (all-screens-params component screen-params))
:fx [[:dispatch [:hide-bottom-sheet]] :fx [[:dispatch [:hide-bottom-sheet]]
[:open-modal-fx component]]})) [:open-modal-fx component]]}))
(rf/defn dismiss-modal (rf/defn dismiss-modal
{:events [:dismiss-modal]} {:events [:dismiss-modal]}
[_ comp-id] [{:keys [db]} comp-id]
{:dismiss-modal comp-id}) {:db (dissoc db :modal-view-ids)
:dismiss-modal comp-id})
(rf/defn navigate-back (rf/defn navigate-back
{:events [:navigate-back]} {:events [:navigate-back]}
[cofx] [cofx]
(shell.events/shell-navigate-back cofx nil)) (shell.events/shell-navigate-back cofx nil))
(defn- remove-last-view-to-modals
[modal-view-ids]
(if (seq modal-view-ids)
(pop modal-view-ids)
modal-view-ids))
(rf/defn navigate-back-within-stack (rf/defn navigate-back-within-stack
{:events [:navigate-back-within-stack]} {:events [:navigate-back-within-stack]}
[_ comp-id] [{:keys [db]} comp-id]
{:navigate-back-within-stack comp-id}) {:db (update db :modal-view-ids remove-last-view-to-modals)
:fx [[:navigate-back-within-stack comp-id]]})
(defn- remove-modal-views-until-comp-id
[modal-view-ids comp-id]
(let [comp-id-index (.indexOf (or modal-view-ids []) comp-id)
modal-view-ids (if (> comp-id-index -1)
(subvec modal-view-ids 0 (inc comp-id-index))
modal-view-ids)]
modal-view-ids))
(rf/defn navigate-back-to (rf/defn navigate-back-to
{:events [:navigate-back-to]} {:events [:navigate-back-to]}
[_ comp-id] [{:keys [db]} comp-id]
{:navigate-back-to comp-id}) (let [modal-view-ids (remove-modal-views-until-comp-id (:modal-view-ids db) comp-id)]
(assoc {:navigate-back-to comp-id}
:db
(if modal-view-ids
(assoc db :modal-view-ids modal-view-ids)
(dissoc db :modal-view-ids)))))
(rf/defn pop-to-root (rf/defn pop-to-root
{:events [:pop-to-root]} {:events [:pop-to-root]}
@ -64,6 +93,7 @@
:db (-> db :db (-> db
(dissoc :shell/floating-screens) (dissoc :shell/floating-screens)
(dissoc :shell/loaded-screens) (dissoc :shell/loaded-screens)
(dissoc :modal-view-ids)
(assoc :view-id (or @shell.state/selected-stack-id :shell))) (assoc :view-id (or @shell.state/selected-stack-id :shell)))
:effects.shell/pop-to-root nil}) :effects.shell/pop-to-root nil})

View File

@ -0,0 +1,10 @@
(ns status-im.subs.navigation
(:require
[re-frame.core :as re-frame]))
(re-frame/reg-sub
:navigation/current-screen-id
:<- [:view-id]
:<- [:modal-view-ids]
(fn [[view-id modal-view-ids]]
(or (peek modal-view-ids) view-id)))

View File

@ -7,6 +7,7 @@
status-im.subs.contact status-im.subs.contact
status-im.subs.general status-im.subs.general
status-im.subs.messages status-im.subs.messages
status-im.subs.navigation
status-im.subs.onboarding status-im.subs.onboarding
status-im.subs.pairing status-im.subs.pairing
status-im.subs.profile status-im.subs.profile
@ -22,6 +23,7 @@
;;view ;;view
(reg-root-key-sub :view-id :view-id) (reg-root-key-sub :view-id :view-id)
(reg-root-key-sub :modal-view-ids :modal-view-ids)
(reg-root-key-sub :screen-params :navigation/screen-params) (reg-root-key-sub :screen-params :navigation/screen-params)
(reg-root-key-sub :animation-shared-element-id :animation-shared-element-id) (reg-root-key-sub :animation-shared-element-id :animation-shared-element-id)