Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Limit amount of decimals on spending cap modal #27672

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { getMockApproveConfirmState } from '../../../../../../../../test/data/confirmations/helper';
import { renderWithConfirmContextProvider } from '../../../../../../../../test/lib/confirmations/render-helpers';
import { EditSpendingCapModal } from './edit-spending-cap-modal';
import {
countDecimalDigits,
EditSpendingCapModal,
} from './edit-spending-cap-modal';

jest.mock('react-dom', () => ({
...jest.requireActual('react-dom'),
Expand Down Expand Up @@ -78,3 +81,26 @@ describe('<EditSpendingCapModal />', () => {
expect(container).toMatchSnapshot();
});
});

describe('countDecimalDigits()', () => {
// @ts-expect-error This is missing from the Mocha type definitions
it.each([
{ numberString: '0', expectedDecimals: 0 },
{ numberString: '100', expectedDecimals: 0 },
{ numberString: '100.123', expectedDecimals: 3 },
{ numberString: '3.141592654', expectedDecimals: 9 },
])(
'should return $expectedDecimals decimals for `$numberString`',
({
numberString,
expectedDecimals,
}: {
numberString: string;
expectedDecimals: number;
}) => {
const actual = countDecimalDigits(numberString);

expect(actual).toEqual(expectedDecimals);
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import { useConfirmContext } from '../../../../../context/confirm';
import { useAssetDetails } from '../../../../../hooks/useAssetDetails';
import { useApproveTokenSimulation } from '../hooks/use-approve-token-simulation';

export function countDecimalDigits(numberString: string) {
return numberString.split('.')[1]?.length || 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return numberString.split(".")[1].length || 0;


export const EditSpendingCapModal = ({
isOpenEditSpendingCapModal,
setIsOpenEditSpendingCapModal,
Expand Down Expand Up @@ -116,10 +120,14 @@ export const EditSpendingCapModal = ({
setCustomSpendingCapInputValue(formattedSpendingCap.toString());
}, [customSpendingCapInputValue, formattedSpendingCap]);

const showDecimalError =
decimals &&
parseInt(decimals, 10) < countDecimalDigits(customSpendingCapInputValue);

return (
<Modal
isOpen={isOpenEditSpendingCapModal}
onClose={() => setIsOpenEditSpendingCapModal(false)}
onClose={handleCancel}
isClosedOnEscapeKey
isClosedOnOutsideClick
className="edit-spending-cap-modal"
Expand Down Expand Up @@ -154,6 +162,15 @@ export const EditSpendingCapModal = ({
style={{ width: '100%' }}
inputProps={{ 'data-testid': 'custom-spending-cap-input' }}
/>
{showDecimalError && (
<Text
variant={TextVariant.bodySm}
color={TextColor.errorDefault}
paddingTop={1}
>
{t('editSpendingCapError', [decimals])}
</Text>
)}
<Text
variant={TextVariant.bodySm}
color={TextColor.textAlternative}
Expand All @@ -168,7 +185,11 @@ export const EditSpendingCapModal = ({
<ModalFooter
onSubmit={handleSubmit}
onCancel={handleCancel}
submitButtonProps={{ children: t('save'), loading: isModalSaving }}
submitButtonProps={{
children: t('save'),
loading: isModalSaving,
disabled: showDecimalError,
}}
/>
</ModalContent>
</Modal>
Expand Down