From c67d94c56e154be4b2cf91572cdc2d8d2da7f8e4 Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Sat, 11 Mar 2023 18:19:38 -0300
Subject: fix: #7753

---
 .../src/pages/PaytoWireTransferForm.tsx            | 35 ++++++++++++++--------
 1 file changed, 22 insertions(+), 13 deletions(-)

(limited to 'packages/demobank-ui/src/pages/PaytoWireTransferForm.tsx')

diff --git a/packages/demobank-ui/src/pages/PaytoWireTransferForm.tsx b/packages/demobank-ui/src/pages/PaytoWireTransferForm.tsx
index f25680481..027f8e25a 100644
--- a/packages/demobank-ui/src/pages/PaytoWireTransferForm.tsx
+++ b/packages/demobank-ui/src/pages/PaytoWireTransferForm.tsx
@@ -15,6 +15,7 @@
  */
 
 import {
+  AmountJson,
   Amounts,
   buildPayto,
   HttpStatusCode,
@@ -30,7 +31,11 @@ import { h, VNode } from "preact";
 import { useEffect, useRef, useState } from "preact/hooks";
 import { PageStateType } from "../context/pageState.js";
 import { useAccessAPI } from "../hooks/access.js";
-import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js";
+import {
+  buildRequestErrorMessage,
+  undefinedIfEmpty,
+  validateIBAN,
+} from "../utils.js";
 import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
 
 const logger = new Logger("PaytoWireTransferForm");
@@ -39,12 +44,12 @@ export function PaytoWireTransferForm({
   focus,
   onError,
   onSuccess,
-  currency,
+  limit,
 }: {
   focus?: boolean;
   onError: (e: PageStateType["error"]) => void;
   onSuccess: () => void;
-  currency: string;
+  limit: AmountJson;
 }): VNode {
   // const backend = useBackendContext();
   // const { pageState, pageStateSetter } = usePageContext(); // NOTE: used for go-back button?
@@ -65,7 +70,8 @@ export function PaytoWireTransferForm({
     if (focus) ref.current?.focus();
   }, [focus, isRawPayto]);
 
-  let parsedAmount = undefined;
+  const trimmedAmountStr = amount?.trim();
+  const parsedAmount = Amounts.parse(`${limit.currency}:${trimmedAmountStr}`);
   const IBAN_REGEX = /^[A-Z][A-Z0-9]*$/;
 
   const errorsWire = undefinedIfEmpty({
@@ -73,14 +79,16 @@ export function PaytoWireTransferForm({
       ? i18n.str`Missing IBAN`
       : !IBAN_REGEX.test(iban)
       ? i18n.str`IBAN should have just uppercased letters and numbers`
-      : undefined,
+      : validateIBAN(iban, i18n),
     subject: !subject ? i18n.str`Missing subject` : undefined,
-    amount: !amount
+    amount: !trimmedAmountStr
       ? i18n.str`Missing amount`
-      : !(parsedAmount = Amounts.parse(`${currency}:${amount}`))
+      : !parsedAmount
       ? i18n.str`Amount is not valid`
       : Amounts.isZero(parsedAmount)
       ? i18n.str`Should be greater than 0`
+      : Amounts.cmp(limit, parsedAmount) === -1
+      ? i18n.str`balance is not enough`
       : undefined,
   });
 
@@ -143,10 +151,10 @@ export function PaytoWireTransferForm({
                 type="text"
                 readonly
                 class="currency-indicator"
-                size={currency?.length}
-                maxLength={currency?.length}
+                size={limit.currency.length}
+                maxLength={limit.currency.length}
                 tabIndex={-1}
-                value={currency}
+                value={limit.currency}
               />
               &nbsp;
               <input
@@ -185,7 +193,7 @@ export function PaytoWireTransferForm({
                 try {
                   await createTransaction({
                     paytoUri,
-                    amount: `${currency}:${amount}`,
+                    amount: `${limit.currency}:${amount}`,
                   });
                   onSuccess();
                   setAmount(undefined);
@@ -257,7 +265,7 @@ export function PaytoWireTransferForm({
       ? i18n.str`only "IBAN" target are supported`
       : !IBAN_REGEX.test(parsed.iban)
       ? i18n.str`IBAN should have just uppercased letters and numbers`
-      : undefined,
+      : validateIBAN(parsed.iban, i18n),
   });
 
   return (
@@ -296,7 +304,8 @@ export function PaytoWireTransferForm({
           <div style={{ fontSize: "small", marginTop: 4 }}>
             Hint:
             <code>
-              payto://iban/[receiver-iban]?message=[subject]&amount=[{currency}
+              payto://iban/[receiver-iban]?message=[subject]&amount=[
+              {limit.currency}
               :X.Y]
             </code>
           </div>
-- 
cgit v1.2.3

