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: withdraw amount field validation #80

Merged
merged 3 commits into from
Jan 20, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<title>Cryptobase</title>
</head>
<body>
<script src="%PUBLIC_URL%/config/env.js" defer async></script>
<script src="%PUBLIC_URL%/config/env.js"></script>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
Expand Down
16 changes: 1 addition & 15 deletions src/components/OrderForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import classnames from 'classnames';
import * as React from 'react';
import { Button } from 'react-bootstrap';
import { Decimal } from '../Decimal';
import { getAmount, getTotalPrice } from '../../helpers/getTotalPrice';
import { cleanPositiveFloatInput, getAmount, getTotalPrice } from '../../helpers';
import { DropdownComponent } from '../Dropdown';
import { OrderProps } from '../Order';

Expand Down Expand Up @@ -122,20 +122,6 @@ const handleSetValue = (value: string | number | undefined, defaultValue: string
value || defaultValue
);

const cleanPositiveFloatInput = (text: string) => {
let cleanInput = text
.replace(',', '.')
.replace(/-+/, '')
.replace(/^0+/, '0')
.replace(/\.+/, '.')
.replace(/^0+([1-9])/, '$1');

if (cleanInput[0] === '.') {
cleanInput = `0${cleanInput}`;
}
return cleanInput;
};

const checkButtonIsDisabled = (safeAmount: number, safePrice: number, price: string, props: OrderFormProps, state: OrderFormState) => {
const invalidAmount = safeAmount <= 0;
const invalidLimitPrice = Number(price) <= 0 && state.orderType === 'Limit';
Expand Down
26 changes: 18 additions & 8 deletions src/containers/Withdraw/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../components';
import { Decimal } from '../../components/Decimal';
import { Beneficiary } from '../../modules';
import { cleanPositiveFloatInput } from '../../helpers';

export interface WithdrawProps {
currency: string;
Expand Down Expand Up @@ -219,16 +220,25 @@ export class Withdraw extends React.Component<WithdrawProps, WithdrawState> {
}
};

private handleChangeInputAmount = (text: string) => {
private handleChangeInputAmount = (value: string) => {
const { fixed } = this.props;
const value = (text !== '') ? Number(parseFloat(text).toFixed(fixed)) : '';
const total = (value !== '') ? value - this.props.fee : 0;
if (total < 0) {
this.setTotal(0);
} else {
this.setTotal(total);

const convertedValue = cleanPositiveFloatInput(String(value));
const condition = new RegExp(`^(?:[\\d-]*\\.?[\\d-]{0,${fixed}}|[\\d-]*\\.[\\d-])$`);
if (convertedValue.match(condition)) {
const amount = (convertedValue !== '') ? Number(parseFloat(convertedValue).toFixed(fixed)) : '';
const total = (amount !== '') ? amount - this.props.fee : 0;

if (total < 0) {
this.setTotal(0);
} else {
this.setTotal(total);
}

this.setState({
amount: convertedValue,
});
}
this.setState({ amount: value });
};

private setTotal = (value: number) => {
Expand Down
13 changes: 13 additions & 0 deletions src/helpers/cleanPositiveFloatInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const cleanPositiveFloatInput = (text: string) => {
let cleanInput = text
.replace(',', '.')
.replace(/-+/, '')
.replace(/^0+/, '0')
.replace(/\.+/, '.')
.replace(/^0+([1-9])/, '$1');

if (cleanInput[0] === '.') {
cleanInput = `0${cleanInput}`;
}
return cleanInput;
};
12 changes: 12 additions & 0 deletions src/helpers/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,16 @@ describe('Helpers', () => {
expect(helpers.monthNameToNumber('December')).toBe('12');
});
});

// cleanPositiveFloatInput.ts
describe('cleanPositiveFloatInput', () => {
it('return a positive float number', () => {
expect(helpers.cleanPositiveFloatInput('.0')).toBe('0.0');
expect(helpers.cleanPositiveFloatInput(',0')).toBe('0.0');
expect(helpers.cleanPositiveFloatInput('+')).toBe('+');
expect(helpers.cleanPositiveFloatInput('-')).toBe('');
expect(helpers.cleanPositiveFloatInput('000')).toBe('0');
expect(helpers.cleanPositiveFloatInput('00.0')).toBe('0.0');
});
});
});
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export * from './timezone';
export * from './toggleColorTheme';
export * from './uppercase';
export * from './getCsrfToken';
export * from './cleanPositiveFloatInput';