Skip to content

Commit

Permalink
♻️ Make fees be formatted based on locale
Browse files Browse the repository at this point in the history
  • Loading branch information
massao committed Aug 15, 2019
1 parent 0be3922 commit 6614ce1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/components/send/form/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import AutoSuggest from '../autoSuggest';
import Spinner from '../../spinner/spinner';
import Tooltip from '../../toolbox/tooltip/tooltip';
import links from '../../../constants/externalLinks';
import { fromRawLsk, toRawLsk } from '../../../utils/lsk';
import { fromRawLsk, toRawLsk, formatBasedOnLocale } from '../../../utils/lsk';
import fees from '../../../constants/fees';
import Feedback from '../../toolbox/feedback/feedback';
import CircularProgress from '../../toolbox/circularProgress/circularProgress';
Expand All @@ -26,6 +26,7 @@ import { tokenMap } from '../../../constants/tokens';
import * as btcTransactionsAPI from '../../../utils/api/btc/transactions';
import Icon from '../../toolbox/icon';
import Box from '../../toolbox/box';
import i18n from '../../../i18n';

class Form extends React.Component {
// eslint-disable-next-line max-statements
Expand Down Expand Up @@ -449,10 +450,15 @@ class Form extends React.Component {
</span>
);
}
const fee = formatBasedOnLocale({
value: fromRawLsk(fields.processingSpeed.txFee),
locale: i18n.language,
});

return (
<span>
{!this.validateAmountField(value)
? `${fromRawLsk(fields.processingSpeed.txFee)} ${token}`
? `${fee} ${token}`
: t('Invalid amount')}
</span>
);
Expand Down Expand Up @@ -522,7 +528,12 @@ class Form extends React.Component {

{ token !== 'BTC' ? (
<span className={styles.amountHint}>
{t('+ Transaction fee {{fee}} LSK', { fee: fromRawLsk(fees.send) })}
{t('+ Transaction fee {{fee}} LSK', {
fee: formatBasedOnLocale({
value: fromRawLsk(fees.send),
locale: i18n.language,
}),
})}
<Tooltip
className="showOnTop"
title={t('Transaction fee')}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/lsk.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import BigNumber from 'bignumber.js';
import numeral from 'numeral';
import 'numeral/locales';

BigNumber.config({ ERRORS: false });

Expand All @@ -9,3 +11,12 @@ export const fromRawLsk = value => (
export const toRawLsk = value => (
new BigNumber(value * new BigNumber(10).pow(8)).decimalPlaces(0).toNumber()
);

export const formatBasedOnLocale = ({
value,
locale = 'en',
format = '0,0.[0000000000000000]',
}) => {
numeral.locale(locale);
return numeral(parseFloat(value, 10)).format(format);
};
17 changes: 16 additions & 1 deletion src/utils/lsk.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { fromRawLsk, toRawLsk } from './lsk';
import { fromRawLsk, toRawLsk, formatBasedOnLocale } from './lsk';

describe('lsk', () => {
describe('fromRawLsk', () => {
Expand All @@ -21,4 +21,19 @@ describe('lsk', () => {
expect(toRawLsk(0)).to.be.equal(0);
});
});

describe('formatBasedOnLocale', () => {
it('should format to EN by default', () => {
const data = { value: 1.23 };
expect(formatBasedOnLocale(data)).to.be.equal('1.23');
});

it('should format correctly to DE', () => {
const data = {
value: 1.23,
locale: 'de',
};
expect(formatBasedOnLocale(data)).to.be.equal('1,23');
});
});
});

0 comments on commit 6614ce1

Please sign in to comment.