[#4361] Don't autocorrect incomplete and erroneous entries for transaction amounts.

Signed-off-by: Igor Mandrigin <i@mandrigin.ru>
This commit is contained in:
Igor Mandrigin 2018-05-22 14:06:50 +02:00
parent dcefb0795d
commit cc24ff1b82
No known key found for this signature in database
GPG Key ID: 4A0EDDE26E66BC8B
6 changed files with 28 additions and 10 deletions

View File

@ -5,7 +5,8 @@
(spec/def ::amount (spec/nilable money/valid?))
(spec/def ::amount-error (spec/nilable string?))
(spec/def ::amount-text (spec/nilable string?))
(spec/def ::symbol (spec/nilable keyword?))
(spec/def :wallet/request-transaction (allowed-keys
:opt-un [::amount ::amount-error ::symbol]))
:opt-un [::amount ::amount-error ::amount-text ::symbol]))

View File

@ -38,4 +38,5 @@
(let [{:keys [value error]} (wallet-db/parse-amount amount)]
{:db (-> db
(assoc-in [:wallet :request-transaction :amount] (money/ether->wei value))
(assoc-in [:wallet :request-transaction :amount-text] amount)
(assoc-in [:wallet :request-transaction :amount-error] error))})))

View File

@ -27,7 +27,7 @@
(views/defview send-transaction-request []
;; TODO(jeluard) both send and request flows should be merged
(views/letsubs [{:keys [to to-name whisper-identity]} [:wallet.send/transaction]
{:keys [amount amount-error]} [:wallet.request/transaction]
{:keys [amount amount-error amount-text]} [:wallet.request/transaction]
scroll (atom nil)]
[comp/simple-screen {:avoid-keyboard? true}
[comp/toolbar (i18n/label :t/new-request)]
@ -42,10 +42,17 @@
[components/asset-selector {:disabled? true
:symbol :ETH}]
[components/amount-selector {:error amount-error
:input-options {:default-value (str (money/to-fixed (money/wei->ether amount)))
:max-length 21
:input-options {:max-length 21
:on-focus (fn [] (when @scroll (utils/set-timeout #(.scrollToEnd @scroll) 100)))
:on-change-text #(re-frame/dispatch [:wallet.request/set-and-validate-amount %])}}]]]
:on-change-text #(re-frame/dispatch [:wallet.request/set-and-validate-amount %])
;; (similarly to status-im.ui.screens.wallet.send.views `send-transaction-panel`)
;; We only auto-correct and prettify user's input when it is valid and positive.
;; Otherwise, user might want to fix his input and autocorrection will give more harm than good.
;; Positive check is because we don't want to replace unfinished 0.000 with just plain 0, that is annoying and
;; potentially dangerous on this screen (e.g. sending 7 ETH instead of 0.0007)
:default-value (if (pos? amount)
(str (money/to-fixed (money/wei->ether amount)))
amount-text)}}]]]
[bottom-buttons/bottom-buttons styles/bottom-buttons
nil ;; Force a phantom button to ensure consistency with other transaction screens which define 2 buttons
[button/button {:disabled? (not (and to amount))

View File

@ -7,6 +7,7 @@
(spec/def ::to (spec/nilable string?))
(spec/def ::to-name (spec/nilable string?))
(spec/def ::amount-error (spec/nilable string?))
(spec/def ::amount-text (spec/nilable string?))
(spec/def ::password (spec/nilable string?))
(spec/def ::wrong-password? (spec/nilable boolean?))
(spec/def ::id (spec/nilable string?))
@ -26,7 +27,7 @@
(spec/def ::method (spec/nilable string?))
(spec/def :wallet/send-transaction (allowed-keys
:opt-un [::amount ::to ::to-name ::amount-error ::password
:opt-un [::amount ::to ::to-name ::amount-error ::amount-text ::password
::waiting-signal? ::signing? ::id ::later?
::camera-flashlight ::in-progress?
::wrong-password? ::from-chat? ::symbol ::advanced?

View File

@ -74,6 +74,7 @@
(let [{:keys [value error]} (wallet.db/parse-amount amount)]
{:db (-> db
(assoc-in [:wallet :send-transaction :amount] (money/ether->wei value))
(assoc-in [:wallet :send-transaction :amount-text] amount)
(assoc-in [:wallet :send-transaction :amount-error] error))})))
(handlers/register-handler-fx

View File

@ -199,7 +199,7 @@
(reset! timeout (utils/set-timeout #(re-frame/dispatch [:wallet.send/set-and-validate-amount amount]) 500))))
(defn- send-transaction-panel [{:keys [modal? transaction scroll advanced? symbol]}]
(let [{:keys [amount amount-error signing? to to-name sufficient-funds? in-progress? from-chat?]} transaction
(let [{:keys [amount amount-text amount-error signing? to to-name sufficient-funds? in-progress? from-chat?]} transaction
timeout (atom nil)]
[wallet.components/simple-screen {:avoid-keyboard? (not modal?)
:status-bar-type (if modal? :modal-wallet :wallet)}
@ -221,10 +221,17 @@
[components/amount-selector {:disabled? modal?
:error (or amount-error
(when-not sufficient-funds? (i18n/label :t/wallet-insufficient-funds)))
:input-options {:default-value (str (money/to-fixed (money/wei->ether amount)))
:max-length 21
:input-options {:max-length 21
:on-focus (fn [] (when (and scroll @scroll) (utils/set-timeout #(.scrollToEnd @scroll) 100)))
:on-change-text (update-amount-fn timeout)}}]
:on-change-text (update-amount-fn timeout)
;; (similarly to status-im.ui.screens.wallet.request.views `send-transaction-request` view)
;; We only auto-correct and prettify user's input when it is valid and positive.
;; Otherwise, user might want to fix his input and autocorrection will give more harm than good.
;; Positive check is because we don't want to replace unfinished 0.000 with just plain 0, that is annoying and
;; potentially dangerous on this screen (e.g. sending 7 ETH instead of 0.0007)
:default-value (if (pos? amount)
(str (money/to-fixed (money/wei->ether amount)))
amount-text)}}]
[advanced-options advanced? transaction modal? scroll]]]
(if signing?
[signing-buttons