diff --git a/src/status_im/contexts/wallet/events.cljs b/src/status_im/contexts/wallet/events.cljs index 60d7da0c9c..ccee381ab4 100644 --- a/src/status_im/contexts/wallet/events.cljs +++ b/src/status_im/contexts/wallet/events.cljs @@ -282,10 +282,11 @@ (filter #(string/starts-with? (or (:ens-name %) "") input) contacts))] (if (and input (empty? result)) (rf/dispatch [:wallet/search-ens input chain-id cb ".stateofus.eth"]) - {:db (assoc db - :wallet/local-suggestions - (map #(assoc % :type item-types/saved-address) result) - :wallet/valid-ens-or-address? (not-empty result))})))) + {:db (-> db + (assoc-in [:wallet :ui :search-address :local-suggestions] + (map #(assoc % :type item-types/saved-address) result)) + (assoc-in [:wallet :ui :search-address :valid-ens-or-address?] + (not-empty result)))})))) (rf/reg-event-fx :wallet/search-ens (fn [_ [input chain-id cb domain]] @@ -303,62 +304,67 @@ (rf/reg-event-fx :wallet/set-ens-address (fn [{:keys [db]} [result ens]] - {:db (assoc db - :wallet/local-suggestions (if result - [{:type item-types/address - :ens ens - :address (eip55/address->checksum result) - :networks [:ethereum :optimism]}] - []) - :wallet/valid-ens-or-address? (boolean result))})) + {:db + (-> db + (assoc-in [:wallet :ui :search-address :local-suggestions] + (if result + [{:type item-types/address + :ens ens + :address (eip55/address->checksum result) + :networks [:ethereum :optimism]}] + [])) + (assoc-in [:wallet :ui :search-address :valid-ens-or-address?] + (boolean result)))})) (rf/reg-event-fx :wallet/fetch-address-suggestions (fn [{:keys [db]} [_address]] - {:db (assoc db - :wallet/local-suggestions nil - :wallet/valid-ens-or-address? false)})) + {:db (-> db + (assoc-in [:wallet :ui :search-address :local-suggestions] nil) + (assoc-in [:wallet :ui :search-address :valid-ens-or-address?] false))})) (rf/reg-event-fx :wallet/ens-validation-success (fn [{:keys [db]} [_ens]] - {:db (assoc db - :wallet/local-suggestions nil - :wallet/valid-ens-or-address? true)})) + {:db (-> db + (assoc-in [:wallet :ui :search-address :local-suggestions] nil) + (assoc-in [:wallet :ui :search-address :valid-ens-or-address?] true))})) (rf/reg-event-fx :wallet/address-validation-success (fn [{:keys [db]} [_]] - {:db (assoc db :wallet/valid-ens-or-address? true)})) + {:db (assoc-in db [:wallet :ui :search-address :valid-ens-or-address?] true)})) (rf/reg-event-fx :wallet/validate-address (fn [{:keys [db]} [address]] - (let [current-timeout (get db :wallet/search-timeout) + (let [current-timeout (get-in db [:wallet :ui :search-address :search-timeout]) timeout (background-timer/set-timeout #(rf/dispatch [:wallet/address-validation-success address]) 2000)] (background-timer/clear-timeout current-timeout) - {:db (assoc db - :wallet/valid-ens-or-address? false - :wallet/search-timeout timeout)}))) + {:db (-> db + (assoc-in [:wallet :ui :search-address :search-timeout] timeout) + (assoc-in [:wallet :ui :search-address :valid-ens-or-address?] false))}))) (rf/reg-event-fx :wallet/validate-ens (fn [{:keys [db]} [ens]] - (let [current-timeout (get db :wallet/search-timeout) + (let [current-timeout (get-in db [:wallet :ui :search-address :search-timeout]) timeout (background-timer/set-timeout #(rf/dispatch [:wallet/ens-validation-success ens]) 2000)] (background-timer/clear-timeout current-timeout) - {:db (assoc db - :wallet/valid-ens-or-address? false - :wallet/search-timeout timeout)}))) + {:db (-> db + (assoc-in [:wallet :ui :search-address :search-timeout] timeout) + (assoc-in [:wallet :ui :search-address :valid-ens-or-address?] false))}))) (rf/reg-event-fx :wallet/clean-local-suggestions (fn [{:keys [db]}] - (let [current-timeout (get db :wallet/search-timeout)] + (let [current-timeout (get-in db [:wallet :ui :search-address :search-timeout])] (background-timer/clear-timeout current-timeout) - {:db (assoc db :wallet/local-suggestions [] :wallet/valid-ens-or-address? false)}))) + {:db (-> db + (assoc-in [:wallet :ui :search-address :local-suggestions] []) + (assoc-in [:wallet :ui :search-address :valid-ens-or-address?] false))}))) (rf/reg-event-fx :wallet/clean-ens-or-address-validation (fn [{:keys [db]}] - {:db (assoc db :wallet/valid-ens-or-address? false)})) + {:db (assoc-in db [:wallet :ui :search-address :valid-ens-or-address?] false)})) (rf/reg-event-fx :wallet/navigate-to-chain-explorer-from-bottom-sheet diff --git a/src/status_im/subs/root.cljs b/src/status_im/subs/root.cljs index 4198c0772b..fa0293a2ba 100644 --- a/src/status_im/subs/root.cljs +++ b/src/status_im/subs/root.cljs @@ -156,8 +156,6 @@ ;;wallet (reg-root-key-sub :wallet :wallet) -(reg-root-key-sub :wallet/local-suggestions :wallet/local-suggestions) -(reg-root-key-sub :wallet/valid-ens-or-address? :wallet/valid-ens-or-address?) ;;debug (when js/goog.DEBUG diff --git a/src/status_im/subs/wallet/wallet.cljs b/src/status_im/subs/wallet/wallet.cljs index 30d845891a..8640b67d50 100644 --- a/src/status_im/subs/wallet/wallet.cljs +++ b/src/status_im/subs/wallet/wallet.cljs @@ -267,3 +267,18 @@ :wallet/transactions :<- [:wallet] :-> :transactions) + +(rf/reg-sub + :wallet/search-address + :<- [:wallet/ui] + :-> :search-address) + +(rf/reg-sub + :wallet/local-suggestions + :<- [:wallet/search-address] + :-> :local-suggestions) + +(rf/reg-sub + :wallet/valid-ens-or-address? + :<- [:wallet/search-address] + :-> :valid-ens-or-address?) diff --git a/src/status_im/subs/wallet/wallet_test.cljs b/src/status_im/subs/wallet/wallet_test.cljs index f0ff32bd73..03712741ea 100644 --- a/src/status_im/subs/wallet/wallet_test.cljs +++ b/src/status_im/subs/wallet/wallet_test.cljs @@ -516,3 +516,22 @@ (is (= keypairs (rf/sub [sub-name]))))) + +(def local-suggestions ["a" "b"]) + +(h/deftest-sub :wallet/local-suggestions + [sub-name] + (testing "returns local suggestions:" + (swap! rf-db/app-db + #(assoc-in % [:wallet :ui :search-address :local-suggestions] local-suggestions)) + (is + (= local-suggestions + (rf/sub [sub-name]))))) + +(h/deftest-sub :wallet/valid-ens-or-address? + [sub-name] + (testing "returns local suggestions:" + (swap! rf-db/app-db + #(assoc-in % [:wallet :ui :search-address :valid-ens-or-address?] true)) + (is + (rf/sub [sub-name]))))