input-state moved to controller
This commit is contained in:
parent
7812b393ce
commit
a6c346ea7d
|
@ -1,7 +1,18 @@
|
||||||
(ns status-im.contexts.wallet.send.input-amount.controller
|
(ns status-im.contexts.wallet.send.input-amount.controller
|
||||||
(:require
|
(:require
|
||||||
|
[status-im.common.controlled-input.utils :as controlled-input]
|
||||||
|
[status-im.contexts.wallet.common.utils :as utils]
|
||||||
|
[utils.money :as money]
|
||||||
[utils.re-frame :as rf]))
|
[utils.re-frame :as rf]))
|
||||||
|
|
||||||
|
;; notes
|
||||||
|
;; token-by-symbol and token looks very similar but they have difference in market values data
|
||||||
|
;; inside token structure :total-balance and :available-balance are same, not sure if they have
|
||||||
|
;; different
|
||||||
|
;; meaning
|
||||||
|
|
||||||
|
;; subs
|
||||||
|
|
||||||
(rf/reg-sub
|
(rf/reg-sub
|
||||||
:layers/ui
|
:layers/ui
|
||||||
:<- [:layers]
|
:<- [:layers]
|
||||||
|
@ -18,25 +29,100 @@
|
||||||
:-> :input-amount-screen)
|
:-> :input-amount-screen)
|
||||||
|
|
||||||
(rf/reg-sub
|
(rf/reg-sub
|
||||||
:controller/send-input-amount-screen
|
:send-input-amount-screen/controller
|
||||||
:<- [:ui/send-input-amount-screen]
|
:<- [:ui/send-input-amount-screen]
|
||||||
:-> :controller)
|
:-> :controller)
|
||||||
|
|
||||||
(rf/reg-sub
|
(rf/reg-sub
|
||||||
:screen-data/send-input-amount-screen
|
:send-input-amount-screen/currency-information
|
||||||
:<- [:controller/send-input-amount-screen]
|
:<- [:wallet/wallet-send-token]
|
||||||
(fn [controller]
|
:<- [:profile/currency]
|
||||||
{:crypto-currency? (:crypto-currency? controller)}))
|
:<- [:profile/currency-symbol]
|
||||||
|
(fn [[{token-symbol :symbol
|
||||||
|
total-balance :total-balance
|
||||||
|
:as
|
||||||
|
token}
|
||||||
|
currency currency-symbol]]
|
||||||
|
{:usd-conversion-rate (utils/token-usd-price token)
|
||||||
|
:currency currency
|
||||||
|
:currency-symbol currency-symbol
|
||||||
|
:token-symbol token-symbol
|
||||||
|
:conversion-rate (-> token
|
||||||
|
:market-values-per-currency
|
||||||
|
currency
|
||||||
|
:price)
|
||||||
|
:token-decimals (-> token
|
||||||
|
utils/token-usd-price
|
||||||
|
utils/one-cent-value
|
||||||
|
utils/calc-max-crypto-decimals)
|
||||||
|
:token token
|
||||||
|
:total-balance total-balance}))
|
||||||
|
|
||||||
|
(rf/reg-sub
|
||||||
|
:send-input-amount-screen/token-by-symbol
|
||||||
|
:<- [:wallet/wallet-send-enabled-from-chain-ids]
|
||||||
|
:<- [:send-input-amount-screen/currency-information]
|
||||||
|
(fn [[enabled-from-chain-ids {:keys [token-symbol]}]]
|
||||||
|
(rf/sub [:wallet/token-by-symbol
|
||||||
|
(str token-symbol)
|
||||||
|
enabled-from-chain-ids])))
|
||||||
|
|
||||||
|
(rf/reg-sub
|
||||||
|
:send-input-amount-screen/max-limit
|
||||||
|
:<- [:send-input-amount-screen/controller]
|
||||||
|
:<- [:send-input-amount-screen/currency-information]
|
||||||
|
(fn [[{:keys [crypto-currency?]}
|
||||||
|
{:keys [total-balance usd-conversion-rate conversion-rate]}]]
|
||||||
|
(if crypto-currency?
|
||||||
|
(utils/cut-crypto-decimals-to-fit-usd-cents
|
||||||
|
total-balance
|
||||||
|
usd-conversion-rate)
|
||||||
|
(-> (money/crypto->fiat
|
||||||
|
total-balance
|
||||||
|
conversion-rate)
|
||||||
|
utils/cut-fiat-balance-to-two-decimals))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(rf/reg-sub
|
||||||
|
:send-input-amount-screen/data
|
||||||
|
:<- [:send-input-amount-screen/controller]
|
||||||
|
:<- [:send-input-amount-screen/max-limit]
|
||||||
|
(fn [[{:keys [crypto-currency? input-state] :as controller} max-limit]]
|
||||||
|
{:crypto-currency? crypto-currency?
|
||||||
|
:max-limit max-limit
|
||||||
|
:input-state input-state
|
||||||
|
}))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
;; events
|
||||||
|
|
||||||
(rf/reg-event-fx
|
(rf/reg-event-fx
|
||||||
:send-input-amount-screen/swap-between-fiat-and-crypto
|
:send-input-amount-screen/swap-between-fiat-and-crypto
|
||||||
(fn [{:keys [db]}]
|
(fn [{:keys [db]}]
|
||||||
{:db (update-in db [:layers :ui :send :input-amount-screen :controller :crypto-currency?] not)}))
|
{:db (update-in db [:layers :ui :send :input-amount-screen :controller :crypto-currency?] not)}))
|
||||||
|
|
||||||
|
(rf/reg-event-fx
|
||||||
|
:send-input-amount-screen/set-input-state
|
||||||
|
(fn [{:keys [db]} [f]]
|
||||||
|
{:db (update-in db [:layers :ui :send :input-amount-screen :controller :input-state] f)}))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(rf/dispatch [:send-input-amount-screen/swap-between-fiat-and-crypto])
|
(rf/dispatch [:send-input-amount-screen/swap-between-fiat-and-crypto])
|
||||||
(rf/sub [:screen-data/send-input-amount-screen])
|
(rf/sub [:send-input-amount-screen/max-limit])
|
||||||
(rf/sub [:controller/send-input-amount-screen])
|
(rf/sub [:send-input-amount-screen/data])
|
||||||
|
(rf/sub [:send-input-amount-screen/controller])
|
||||||
|
|
||||||
|
(rf/dispatch [:send-input-amount-screen/set-input-state #(controlled-input/add-character % "1")])
|
||||||
|
(rf/dispatch [:send-input-amount-screen/set-input-state])
|
||||||
|
|
||||||
|
(tap> {:token-by-symbol (rf/sub [:send-input-amount-screen/token-by-symbol])
|
||||||
|
:token (:token (rf/sub [:send-input-amount-screen/currency-information]))})
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
#_(rf/reg-sub
|
#_(rf/reg-sub
|
||||||
|
|
|
@ -11,7 +11,6 @@
|
||||||
[status-im.contexts.wallet.common.account-switcher.view :as account-switcher]
|
[status-im.contexts.wallet.common.account-switcher.view :as account-switcher]
|
||||||
[status-im.contexts.wallet.common.asset-list.view :as asset-list]
|
[status-im.contexts.wallet.common.asset-list.view :as asset-list]
|
||||||
[status-im.contexts.wallet.common.utils :as utils]
|
[status-im.contexts.wallet.common.utils :as utils]
|
||||||
[status-im.contexts.wallet.send.input-amount.controller :as c]
|
|
||||||
[status-im.contexts.wallet.send.input-amount.style :as style]
|
[status-im.contexts.wallet.send.input-amount.style :as style]
|
||||||
[status-im.contexts.wallet.send.routes.view :as routes]
|
[status-im.contexts.wallet.send.routes.view :as routes]
|
||||||
[status-im.contexts.wallet.sheets.buy-token.view :as buy-token]
|
[status-im.contexts.wallet.sheets.buy-token.view :as buy-token]
|
||||||
|
@ -219,18 +218,17 @@
|
||||||
(defn view
|
(defn view
|
||||||
;; crypto-decimals, limit-crypto and initial-crypto-currency? args are needed
|
;; crypto-decimals, limit-crypto and initial-crypto-currency? args are needed
|
||||||
;; for component tests only
|
;; for component tests only
|
||||||
[{default-on-confirm :on-confirm
|
[{default-on-confirm :on-confirm
|
||||||
default-limit-crypto :limit-crypto
|
on-navigate-back :on-navigate-back
|
||||||
default-crypto-decimals :crypto-decimals
|
button-one-label :button-one-label
|
||||||
on-navigate-back :on-navigate-back
|
button-one-props :button-one-props
|
||||||
button-one-label :button-one-label
|
current-screen-id :current-screen-id
|
||||||
button-one-props :button-one-props
|
enabled-from-chain-ids :enabled-from-chain-ids
|
||||||
current-screen-id :current-screen-id
|
from-enabled-networks :from-enabled-networks}]
|
||||||
initial-crypto-currency? :initial-crypto-currency?
|
(let [{:keys [crypto-currency?
|
||||||
enabled-from-chain-ids :enabled-from-chain-ids
|
max-limit
|
||||||
from-enabled-networks :from-enabled-networks
|
input-state]
|
||||||
:or {initial-crypto-currency? true}}]
|
:as state} (rf/sub [:send-input-amount-screen/data])
|
||||||
(let [{:keys [crypto-currency?] :as state} (rf/sub [:screen-data/send-input-amount-screen])
|
|
||||||
view-id (rf/sub [:view-id])
|
view-id (rf/sub [:view-id])
|
||||||
active-screen? (= view-id current-screen-id)
|
active-screen? (= view-id current-screen-id)
|
||||||
bottom (safe-area/get-bottom)
|
bottom (safe-area/get-bottom)
|
||||||
|
@ -245,12 +243,9 @@
|
||||||
:as
|
:as
|
||||||
token} (rf/sub [:wallet/wallet-send-token])
|
token} (rf/sub [:wallet/wallet-send-token])
|
||||||
send-from-locked-amounts (rf/sub [:wallet/wallet-send-from-locked-amounts])
|
send-from-locked-amounts (rf/sub [:wallet/wallet-send-from-locked-amounts])
|
||||||
{:keys [total-balance]
|
token-by-symbol (rf/sub [:wallet/token-by-symbol
|
||||||
:as token-by-symbol} (rf/sub [:wallet/token-by-symbol
|
(str token-symbol)
|
||||||
(str token-symbol)
|
enabled-from-chain-ids])
|
||||||
enabled-from-chain-ids])
|
|
||||||
token-balance (or default-limit-crypto total-balance)
|
|
||||||
usd-conversion-rate (utils/token-usd-price token)
|
|
||||||
currency (rf/sub [:profile/currency])
|
currency (rf/sub [:profile/currency])
|
||||||
conversion-rate (-> token
|
conversion-rate (-> token
|
||||||
:market-values-per-currency
|
:market-values-per-currency
|
||||||
|
@ -260,22 +255,13 @@
|
||||||
utils/token-usd-price
|
utils/token-usd-price
|
||||||
utils/one-cent-value
|
utils/one-cent-value
|
||||||
utils/calc-max-crypto-decimals)
|
utils/calc-max-crypto-decimals)
|
||||||
[input-state set-input-state] (rn/use-state controlled-input/init-state)
|
clear-input! #(rf/dispatch [:send-input-amount-screen/set-input-state
|
||||||
clear-input! #(set-input-state controlled-input/delete-all)
|
controlled-input/delete-all])
|
||||||
currency-symbol (rf/sub [:profile/currency-symbol])
|
currency-symbol (rf/sub [:profile/currency-symbol])
|
||||||
loading-routes? (rf/sub
|
loading-routes? (rf/sub
|
||||||
[:wallet/wallet-send-loading-suggested-routes?])
|
[:wallet/wallet-send-loading-suggested-routes?])
|
||||||
route (rf/sub [:wallet/wallet-send-route])
|
route (rf/sub [:wallet/wallet-send-route])
|
||||||
on-confirm (or default-on-confirm handle-on-confirm)
|
on-confirm (or default-on-confirm handle-on-confirm)
|
||||||
crypto-decimals (or token-decimals default-crypto-decimals)
|
|
||||||
max-limit (if crypto-currency?
|
|
||||||
(utils/cut-crypto-decimals-to-fit-usd-cents
|
|
||||||
token-balance
|
|
||||||
usd-conversion-rate)
|
|
||||||
(-> (money/crypto->fiat
|
|
||||||
token-balance
|
|
||||||
conversion-rate)
|
|
||||||
utils/cut-fiat-balance-to-two-decimals))
|
|
||||||
input-value (controlled-input/input-value input-state)
|
input-value (controlled-input/input-value input-state)
|
||||||
valid-input? (not (or (controlled-input/empty-value? input-state)
|
valid-input? (not (or (controlled-input/empty-value? input-state)
|
||||||
(controlled-input/input-error input-state)))
|
(controlled-input/input-error input-state)))
|
||||||
|
@ -286,7 +272,7 @@
|
||||||
input-value
|
input-value
|
||||||
(number/remove-trailing-zeroes
|
(number/remove-trailing-zeroes
|
||||||
(.toFixed (/ input-value conversion-rate)
|
(.toFixed (/ input-value conversion-rate)
|
||||||
crypto-decimals)))
|
token-decimals)))
|
||||||
total-amount-receiver (rf/sub [:wallet/total-amount true])
|
total-amount-receiver (rf/sub [:wallet/total-amount true])
|
||||||
amount-text (str (number/remove-trailing-zeroes
|
amount-text (str (number/remove-trailing-zeroes
|
||||||
(.toFixed total-amount-receiver
|
(.toFixed total-amount-receiver
|
||||||
|
@ -366,10 +352,11 @@
|
||||||
(some? routes))}))
|
(some? routes))}))
|
||||||
swap-between-fiat-and-crypto (fn []
|
swap-between-fiat-and-crypto (fn []
|
||||||
(if crypto-currency?
|
(if crypto-currency?
|
||||||
(set-input-state
|
(rf/dispatch [:send-input-amount-screen/set-input-state
|
||||||
#(controlled-input/->fiat % conversion-rate))
|
#(controlled-input/->fiat % conversion-rate)])
|
||||||
(set-input-state
|
(rf/dispatch [:send-input-amount-screen/set-input-state
|
||||||
#(controlled-input/->crypto % conversion-rate)))
|
#(controlled-input/->crypto % conversion-rate)])
|
||||||
|
)
|
||||||
(rf/dispatch
|
(rf/dispatch
|
||||||
[:send-input-amount-screen/swap-between-fiat-and-crypto]))]
|
[:send-input-amount-screen/swap-between-fiat-and-crypto]))]
|
||||||
(rn/use-effect
|
(rn/use-effect
|
||||||
|
@ -385,7 +372,8 @@
|
||||||
(hot-reload/use-safe-unmount on-navigate-back)
|
(hot-reload/use-safe-unmount on-navigate-back)
|
||||||
(rn/use-effect
|
(rn/use-effect
|
||||||
(fn []
|
(fn []
|
||||||
(set-input-state #(controlled-input/set-upper-limit % max-limit)))
|
(rf/dispatch [:send-input-amount-screen/set-input-state
|
||||||
|
#(controlled-input/set-upper-limit % max-limit)]))
|
||||||
[max-limit])
|
[max-limit])
|
||||||
(rn/use-effect
|
(rn/use-effect
|
||||||
(fn []
|
(fn []
|
||||||
|
@ -486,18 +474,21 @@
|
||||||
:delete-key? true
|
:delete-key? true
|
||||||
:on-press (fn [c]
|
:on-press (fn [c]
|
||||||
(let [new-text (str input-value c)
|
(let [new-text (str input-value c)
|
||||||
max-decimals (if crypto-currency? crypto-decimals 2)
|
max-decimals (if crypto-currency? token-decimals 2)
|
||||||
regex-pattern (str "^\\d*\\.?\\d{0," max-decimals "}$")
|
regex-pattern (str "^\\d*\\.?\\d{0," max-decimals "}$")
|
||||||
regex (re-pattern regex-pattern)]
|
regex (re-pattern regex-pattern)]
|
||||||
(when (re-matches regex new-text)
|
(when (re-matches regex new-text)
|
||||||
(debounce/clear-all)
|
(debounce/clear-all)
|
||||||
(set-input-state #(controlled-input/add-character % c)))))
|
(rf/dispatch [:send-input-amount-screen/set-input-state
|
||||||
|
#(controlled-input/add-character % c)]))))
|
||||||
:on-delete (fn []
|
:on-delete (fn []
|
||||||
(debounce/clear-all)
|
(debounce/clear-all)
|
||||||
(set-input-state controlled-input/delete-last)
|
(rf/dispatch [:send-input-amount-screen/set-input-state
|
||||||
|
controlled-input/delete-last])
|
||||||
(rf/dispatch-sync [:wallet/stop-and-clean-suggested-routes]))
|
(rf/dispatch-sync [:wallet/stop-and-clean-suggested-routes]))
|
||||||
:on-long-press-delete (fn []
|
:on-long-press-delete (fn []
|
||||||
(debounce/clear-all)
|
(debounce/clear-all)
|
||||||
(set-input-state controlled-input/delete-all)
|
(rf/dispatch [:send-input-amount-screen/set-input-state
|
||||||
|
controlled-input/delete-all])
|
||||||
(rf/dispatch-sync [:wallet/stop-and-clean-suggested-routes]))}]]))
|
(rf/dispatch-sync [:wallet/stop-and-clean-suggested-routes]))}]]))
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
(:require
|
(:require
|
||||||
[legacy.status-im.fleet.core :as fleet]
|
[legacy.status-im.fleet.core :as fleet]
|
||||||
[react-native.core :as rn]
|
[react-native.core :as rn]
|
||||||
|
[status-im.common.controlled-input.utils :as controlled-input]
|
||||||
[status-im.contexts.shell.activity-center.events :as activity-center]
|
[status-im.contexts.shell.activity-center.events :as activity-center]
|
||||||
[status-im.contexts.wallet.db :as wallet]))
|
[status-im.contexts.wallet.db :as wallet]))
|
||||||
|
|
||||||
|
@ -48,4 +49,5 @@
|
||||||
{:send
|
{:send
|
||||||
{:input-amount-screen
|
{:input-amount-screen
|
||||||
{:controller
|
{:controller
|
||||||
{:crypto-currency? true}}}}}})
|
{:crypto-currency? true
|
||||||
|
:input-state controlled-input/init-state}}}}}})
|
||||||
|
|
Loading…
Reference in New Issue