demobank-ui: remove wrong and dangerous usage of float
This commit is contained in:
parent
3e8562456d
commit
93dc842e97
@ -46,21 +46,27 @@ export function WalletWithdrawForm({
|
|||||||
const { i18n } = useTranslationContext();
|
const { i18n } = useTranslationContext();
|
||||||
const { createWithdrawal } = useAccessAPI();
|
const { createWithdrawal } = useAccessAPI();
|
||||||
|
|
||||||
const [amount, setAmount] = useState<string | undefined>("5.00");
|
const [amountStr, setAmountStr] = useState<string | undefined>("5.00");
|
||||||
const ref = useRef<HTMLInputElement>(null);
|
const ref = useRef<HTMLInputElement>(null);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (focus) ref.current?.focus();
|
if (focus) ref.current?.focus();
|
||||||
}, [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({
|
const errors = undefinedIfEmpty({
|
||||||
amount: !amountFloat
|
amount:
|
||||||
? i18n.str`required`
|
trimmedAmountStr == null
|
||||||
: Number.isNaN(amountFloat)
|
? i18n.str`required`
|
||||||
? i18n.str`should be a number`
|
: parsedAmount == null
|
||||||
: amountFloat < 0
|
? i18n.str`invalid`
|
||||||
? i18n.str`should be positive`
|
: undefined,
|
||||||
: undefined,
|
|
||||||
});
|
});
|
||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
@ -92,14 +98,14 @@ export function WalletWithdrawForm({
|
|||||||
ref={ref}
|
ref={ref}
|
||||||
id="withdraw-amount"
|
id="withdraw-amount"
|
||||||
name="withdraw-amount"
|
name="withdraw-amount"
|
||||||
value={amount ?? ""}
|
value={amountStr ?? ""}
|
||||||
onChange={(e): void => {
|
onChange={(e): void => {
|
||||||
setAmount(e.currentTarget.value);
|
setAmountStr(e.currentTarget.value);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<ShowInputErrorLabel
|
<ShowInputErrorLabel
|
||||||
message={errors?.amount}
|
message={errors?.amount}
|
||||||
isDirty={amount !== undefined}
|
isDirty={amountStr !== undefined}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</p>
|
</p>
|
||||||
@ -113,14 +119,11 @@ export function WalletWithdrawForm({
|
|||||||
value={i18n.str`Withdraw`}
|
value={i18n.str`Withdraw`}
|
||||||
onClick={async (e) => {
|
onClick={async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (!amountFloat) return;
|
if (!parsedAmount) return;
|
||||||
try {
|
try {
|
||||||
const result = await createWithdrawal({
|
const result = await createWithdrawal({
|
||||||
amount: Amounts.stringify(
|
amount: Amounts.stringify(parsedAmount),
|
||||||
Amounts.fromFloat(amountFloat, currency),
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
onSuccess(result.data);
|
onSuccess(result.data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof RequestError) {
|
if (error instanceof RequestError) {
|
||||||
|
Loading…
Reference in New Issue
Block a user