Skip to content

Commit

Permalink
refactor(wallet, dashboard): remove isPayAllIota from FormValues (#…
Browse files Browse the repository at this point in the history
…4184)

* refactor(wallet, dashboard): remove isPayAllIota from FormValues

* fix: use supporting label to show correct gas label

* fix: add missing isPayALlIota indicator

* fix: avoid parsing an unformatted value

* fix: format gas

---------

Co-authored-by: Begoña Álvarez de la Cruz <[email protected]>
  • Loading branch information
VmMad and begonaalvarezd authored Nov 25, 2024
1 parent ec66e09 commit 3742d78
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 71 deletions.
10 changes: 6 additions & 4 deletions apps/core/src/components/Inputs/SendTokenFormInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface SendTokenInputProps {
onActionClick: () => Promise<void>;
isMaxActionDisabled?: boolean;
name: string;
isPayAllIota: boolean;
}

export function SendTokenFormInput({
Expand All @@ -30,6 +31,7 @@ export function SendTokenFormInput({
onActionClick,
isMaxActionDisabled,
name,
isPayAllIota,
}: SendTokenInputProps) {
const { values, setFieldValue, isSubmitting, validateField } = useFormikContext<TokenForm>();
const { data: gasBudgetEstimation } = useGasBudgetEstimation({
Expand All @@ -38,7 +40,7 @@ export function SendTokenFormInput({
activeAddress,
to: to,
amount: values.amount,
isPayAllIota: values.isPayAllIota,
isPayAllIota,
});
const [formattedGasBudgetEstimation, gasToken] = useFormatCoin(
gasBudgetEstimation,
Expand All @@ -61,8 +63,8 @@ export function SendTokenFormInput({

// gasBudgetEstimation should change when the amount above changes
useEffect(() => {
setFieldValue('gasBudgetEst', formattedGasBudgetEstimation, false);
}, [formattedGasBudgetEstimation, setFieldValue, values.amount]);
setFieldValue('gasBudgetEst', gasBudgetEstimation, false);
}, [gasBudgetEstimation, setFieldValue, values.amount]);

return (
<Input
Expand All @@ -74,7 +76,7 @@ export function SendTokenFormInput({
placeholder="0.00"
label="Send Amount"
suffix={` ${symbol}`}
prefix={values.isPayAllIota ? '~ ' : undefined}
prefix={isPayAllIota ? '~ ' : undefined}
allowNegative={false}
errorMessage={errorMessage}
amountCounter={!errorMessage ? (coins ? gasAmount : '--') : undefined}
Expand Down
1 change: 0 additions & 1 deletion apps/core/src/forms/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
export type TokenForm = {
amount: string;
to: string;
isPayAllIota: boolean;
gasBudgetEst: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useGetAllCoins } from '@iota/core';
import { Dialog, DialogBody, DialogContent, DialogPosition, Header } from '@iota/apps-ui-kit';
import { FormDataValues } from './interfaces';
import { INITIAL_VALUES } from './constants';
import { IOTA_TYPE_ARG } from '@iota/iota-sdk/utils';

interface SendCoinPopupProps {
coin: CoinBalance;
Expand Down Expand Up @@ -44,7 +45,7 @@ function SendTokenDialogBody({
activeAddress,
formData.to,
formData.formattedAmount,
selectedCoin?.totalBalance === formData.amount,
selectedCoin?.totalBalance === formData.amount && selectedCoin.coinType === IOTA_TYPE_ARG,
);

function handleTransfer() {
Expand Down Expand Up @@ -102,6 +103,10 @@ function SendTokenDialogBody({
senderAddress={activeAddress}
isPending={isPending}
coinType={selectedCoin.coinType}
isPayAllIota={
selectedCoin.totalBalance === formData.amount &&
selectedCoin.coinType === IOTA_TYPE_ARG
}
/>
)}
</DialogBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ export const INITIAL_VALUES: FormDataValues = {
to: '',
amount: '',
formattedAmount: '',
isPayAllIota: false,
gasBudgetEst: '',
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ export interface FormDataValues {
amount: string;
formattedAmount: string;
to: string;
isPayAllIota: boolean;
gasBudgetEst: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { IOTA_TYPE_ARG } from '@iota/iota-sdk/utils';
import { Form, Formik, FormikProps } from 'formik';
import { Exclamation } from '@iota/ui-icons';
import { UseQueryResult } from '@tanstack/react-query';
import { useEffect } from 'react';
import { FormDataValues } from '../interfaces';
import { INITIAL_VALUES } from '../constants';

Expand Down Expand Up @@ -78,13 +77,13 @@ function FormInputs({
coins,
queryResult,
}: FormInputsProps): React.JSX.Element {
const newPayIotaAll =
const isPayAllIota =
parseAmount(values.amount, coinDecimals) === coinBalance && coinType === IOTA_TYPE_ARG;

const hasEnoughBalance =
values.isPayAllIota ||
isPayAllIota ||
iotaBalance >
parseAmount(values.gasBudgetEst, coinDecimals) +
BigInt(values.gasBudgetEst ?? '0') +
parseAmount(coinType === IOTA_TYPE_ARG ? values.amount : '0', coinDecimals);

async function onMaxTokenButtonClick() {
Expand All @@ -96,12 +95,6 @@ function FormInputs({
queryResult.isPending ||
!coinBalance;

useEffect(() => {
if (values.isPayAllIota !== newPayIotaAll) {
setFieldValue('isPayAllIota', newPayIotaAll);
}
}, [values.isPayAllIota, newPayIotaAll, setFieldValue]);

return (
<div className="flex h-full w-full flex-col">
<Form autoComplete="off" noValidate className="flex-1">
Expand All @@ -124,6 +117,7 @@ function FormInputs({
activeAddress={activeAddress}
onActionClick={onMaxTokenButtonClick}
isMaxActionDisabled={isMaxActionDisabled}
isPayAllIota={isPayAllIota}
/>
<AddressInput name="to" placeholder="Enter Address" />
</div>
Expand Down Expand Up @@ -195,7 +189,6 @@ export function EnterValuesFormView({
);

const formattedTokenBalance = tokenBalance.replace(/,/g, '');
const initAmountBig = parseAmount('', coinDecimals);

if (coinsBalanceIsPending || coinsIsPending || iotaCoinsIsPending) {
return (
Expand All @@ -205,7 +198,7 @@ export function EnterValuesFormView({
);
}

async function handleFormSubmit({ to, amount, isPayAllIota, gasBudgetEst }: FormDataValues) {
async function handleFormSubmit({ to, amount, gasBudgetEst }: FormDataValues) {
if (!coins || !iotaCoins) return;
const coinsIDs = [...coins]
.sort((a, b) => Number(b.balance) - Number(a.balance))
Expand All @@ -217,7 +210,6 @@ export function EnterValuesFormView({
to,
amount,
formattedAmount,
isPayAllIota,
coins,
coinIds: coinsIDs,
gasBudgetEst,
Expand All @@ -239,17 +231,7 @@ export function EnterValuesFormView({
/>

<Formik
initialValues={{
amount: initialFormValues.amount ?? '',
to: initialFormValues.to ?? '',
formattedAmount: initialFormValues.formattedAmount ?? '',
isPayAllIota:
initialFormValues.isPayAllIota ??
(!!initAmountBig &&
initAmountBig === coinBalance &&
coin.coinType === IOTA_TYPE_ARG),
gasBudgetEst: initialFormValues.gasBudgetEst ?? '',
}}
initialValues={initialFormValues}
validationSchema={validationSchemaStepOne}
enableReinitialize
validateOnChange={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
Divider,
ButtonType,
} from '@iota/apps-ui-kit';
import { formatAddress } from '@iota/iota-sdk/utils';
import { GAS_SYMBOL, CoinIcon, ImageIconSize, useFormatCoin, ExplorerLinkType } from '@iota/core';
import { formatAddress, IOTA_TYPE_ARG } from '@iota/iota-sdk/utils';
import { CoinIcon, ImageIconSize, useFormatCoin, ExplorerLinkType } from '@iota/core';
import { Loader } from '@iota/ui-icons';
import { ExplorerLink } from '@/components';

Expand All @@ -28,16 +28,19 @@ interface ReviewValuesFormProps {
isPending: boolean;
executeTransfer: () => void;
coinType: string;
isPayAllIota?: boolean;
}

export function ReviewValuesFormView({
formData: { amount, to, formattedAmount, isPayAllIota, gasBudgetEst },
formData: { amount, to, formattedAmount, gasBudgetEst },
senderAddress,
isPending,
executeTransfer,
coinType,
isPayAllIota,
}: ReviewValuesFormProps): JSX.Element {
const [formatAmount, symbol] = useFormatCoin(formattedAmount, coinType);
const [gasEstimated, gasSymbol] = useFormatCoin(gasBudgetEst, IOTA_TYPE_ARG);
return (
<div className="flex h-full flex-col">
<div className="flex h-full w-full flex-col gap-md">
Expand Down Expand Up @@ -89,8 +92,8 @@ export function ReviewValuesFormView({
<Divider />
<KeyValueInfo
keyText={'Est. Gas Fees'}
value={gasBudgetEst}
supportingLabel={GAS_SYMBOL}
value={gasEstimated}
supportingLabel={gasSymbol}
fullwidth
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { ExplorerLink, ExplorerLinkType, TxnAmount } from '_components';
import { useActiveAddress } from '_src/ui/app/hooks/useActiveAddress';
import { GAS_SYMBOL, parseAmount, useCoinMetadata } from '@iota/core';
import { parseAmount, useCoinMetadata, useFormatCoin } from '@iota/core';
import { Divider, KeyValueInfo } from '@iota/apps-ui-kit';
import { formatAddress } from '@iota/iota-sdk/utils';

Expand All @@ -26,6 +26,7 @@ export function PreviewTransfer({
const accountAddress = useActiveAddress();
const { data: metadata } = useCoinMetadata(coinType);
const amountWithoutDecimals = parseAmount(amount, metadata?.decimals ?? 0);
const [formattedGasBudgetEstimation, gasToken] = useFormatCoin(gasBudget, coinType);

return (
<div className="flex w-full flex-col gap-md">
Expand Down Expand Up @@ -63,7 +64,8 @@ export function PreviewTransfer({
<Divider />
<KeyValueInfo
keyText={'Est. Gas Fees'}
value={`${gasBudget} ${GAS_SYMBOL}`}
value={formattedGasBudgetEstimation}
supportingLabel={gasToken}
fullwidth
/>
</div>
Expand Down
25 changes: 6 additions & 19 deletions apps/wallet/src/ui/app/pages/home/transfer-coin/SendTokenForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { type CoinStruct } from '@iota/iota-sdk/client';
import { IOTA_TYPE_ARG } from '@iota/iota-sdk/utils';
import { Form, Formik } from 'formik';
import { useEffect, useMemo } from 'react';
import { useMemo } from 'react';

import {
InfoBox,
Expand All @@ -32,7 +32,6 @@ import { Exclamation } from '@iota/ui-icons';
const INITIAL_VALUES = {
to: '',
amount: '',
isPayAllIota: false,
gasBudgetEst: '',
};

Expand All @@ -41,7 +40,6 @@ export type FormValues = typeof INITIAL_VALUES;
export type SubmitProps = {
to: string;
amount: string;
isPayAllIota: boolean;
coinIds: string[];
coins: CoinStruct[];
gasBudgetEst: string;
Expand Down Expand Up @@ -100,9 +98,8 @@ export function SendTokenForm({

// remove the comma from the token balance
const formattedTokenBalance = tokenBalance.replace(/,/g, '');
const initAmountBig = parseAmount(initialAmount, coinDecimals);

async function handleFormSubmit({ to, amount, isPayAllIota, gasBudgetEst }: FormValues) {
async function handleFormSubmit({ to, amount, gasBudgetEst }: FormValues) {
if (!coins || !iotaCoins) return;
const coinsIDs = [...coins]
.sort((a, b) => Number(b.balance) - Number(a.balance))
Expand All @@ -111,7 +108,6 @@ export function SendTokenForm({
const data = {
to,
amount,
isPayAllIota,
coins,
coinIds: coinsIDs,
gasBudgetEst,
Expand All @@ -132,10 +128,6 @@ export function SendTokenForm({
initialValues={{
amount: initialAmount,
to: initialTo,
isPayAllIota:
!!initAmountBig &&
initAmountBig === coinBalance &&
coinType === IOTA_TYPE_ARG,
gasBudgetEst: '',
}}
validationSchema={validationSchemaStepOne}
Expand All @@ -145,14 +137,14 @@ export function SendTokenForm({
onSubmit={handleFormSubmit}
>
{({ isValid, isSubmitting, setFieldValue, values, submitForm }) => {
const newPayIotaAll =
const isPayAllIota =
parseAmount(values.amount, coinDecimals) === coinBalance &&
coinType === IOTA_TYPE_ARG;

const hasEnoughBalance =
values.isPayAllIota ||
isPayAllIota ||
iotaBalance >
parseAmount(values.gasBudgetEst, coinDecimals) +
BigInt(values.gasBudgetEst ?? '0') +
parseAmount(
coinType === IOTA_TYPE_ARG ? values.amount : '0',
coinDecimals,
Expand All @@ -167,12 +159,6 @@ export function SendTokenForm({
queryResult.isPending ||
!coinBalance;

useEffect(() => {
if (values.isPayAllIota !== newPayIotaAll) {
setFieldValue('isPayAllIota', newPayIotaAll);
}
}, [values.isPayAllIota, newPayIotaAll, setFieldValue]);

return (
<div className="flex h-full w-full flex-col">
<Form autoComplete="off" noValidate className="flex-1">
Expand All @@ -194,6 +180,7 @@ export function SendTokenForm({
coins={coins ?? []}
onActionClick={onMaxTokenButtonClick}
isMaxActionDisabled={isMaxActionDisabled}
isPayAllIota={isPayAllIota}
/>
<AddressInput name="to" placeholder="Enter Address" />
</div>
Expand Down
Loading

0 comments on commit 3742d78

Please sign in to comment.