Skip to content

Commit

Permalink
Merge pull request #80 from openware/fix/withdraw
Browse files Browse the repository at this point in the history
Fix: withdraw amount field validation
  • Loading branch information
josadcha committed Jan 20, 2020
2 parents 9a28021 + 4a02920 commit 9524334
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
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';

0 comments on commit 9524334

Please sign in to comment.