gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-core] branch master updated: demobank-ui: remove wrong and


From: gnunet
Subject: [taler-wallet-core] branch master updated: demobank-ui: remove wrong and dangerous usage of float
Date: Sun, 26 Feb 2023 23:28:32 +0100

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository wallet-core.

The following commit(s) were added to refs/heads/master by this push:
     new 93dc842e9 demobank-ui: remove wrong and dangerous usage of float
93dc842e9 is described below

commit 93dc842e9729c5bbd0eca8ed17359da9326f6dde
Author: Florian Dold <florian@dold.me>
AuthorDate: Sun Feb 26 19:16:42 2023 +0100

    demobank-ui: remove wrong and dangerous usage of float
---
 .../demobank-ui/src/pages/WalletWithdrawForm.tsx   | 37 ++++++++++++----------
 1 file changed, 20 insertions(+), 17 deletions(-)

diff --git a/packages/demobank-ui/src/pages/WalletWithdrawForm.tsx 
b/packages/demobank-ui/src/pages/WalletWithdrawForm.tsx
index 2b2df3baa..02b389c6c 100644
--- a/packages/demobank-ui/src/pages/WalletWithdrawForm.tsx
+++ b/packages/demobank-ui/src/pages/WalletWithdrawForm.tsx
@@ -46,21 +46,27 @@ export function WalletWithdrawForm({
   const { i18n } = useTranslationContext();
   const { createWithdrawal } = useAccessAPI();
 
-  const [amount, setAmount] = useState<string | undefined>("5.00");
+  const [amountStr, setAmountStr] = useState<string | undefined>("5.00");
   const ref = useRef<HTMLInputElement>(null);
   useEffect(() => {
     if (focus) ref.current?.focus();
   }, [focus]);
 
-  const amountFloat = amount ? parseFloat(amount) : undefined;
+  // Beware: We never ever want to treat the amount as a float!
+
+  const trimmedAmountStr = amountStr?.trim();
+
+  const parsedAmount = trimmedAmountStr
+    ? Amounts.parse(`${currency}:${trimmedAmountStr}`)
+    : undefined;
+
   const errors = undefinedIfEmpty({
-    amount: !amountFloat
-      ? i18n.str`required`
-      : Number.isNaN(amountFloat)
-      ? i18n.str`should be a number`
-      : amountFloat < 0
-      ? i18n.str`should be positive`
-      : undefined,
+    amount:
+      trimmedAmountStr == null
+        ? i18n.str`required`
+        : parsedAmount == null
+        ? i18n.str`invalid`
+        : undefined,
   });
   return (
     <form
@@ -92,14 +98,14 @@ export function WalletWithdrawForm({
             ref={ref}
             id="withdraw-amount"
             name="withdraw-amount"
-            value={amount ?? ""}
+            value={amountStr ?? ""}
             onChange={(e): void => {
-              setAmount(e.currentTarget.value);
+              setAmountStr(e.currentTarget.value);
             }}
           />
           <ShowInputErrorLabel
             message={errors?.amount}
-            isDirty={amount !== undefined}
+            isDirty={amountStr !== undefined}
           />
         </div>
       </p>
@@ -113,14 +119,11 @@ export function WalletWithdrawForm({
             value={i18n.str`Withdraw`}
             onClick={async (e) => {
               e.preventDefault();
-              if (!amountFloat) return;
+              if (!parsedAmount) return;
               try {
                 const result = await createWithdrawal({
-                  amount: Amounts.stringify(
-                    Amounts.fromFloat(amountFloat, currency),
-                  ),
+                  amount: Amounts.stringify(parsedAmount),
                 });
-
                 onSuccess(result.data);
               } catch (error) {
                 if (error instanceof RequestError) {

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]