Skip to content

Commit

Permalink
feat(neuron-ui): add verification on updating amounts
Browse files Browse the repository at this point in the history
disable the amount to be updated if the value is not a number or has more than 8 digits decimal
  • Loading branch information
Keith-CY committed Aug 21, 2019
1 parent a03bc44 commit 46ae8c1
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/neuron-ui/src/components/Send/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { TransactionOutput } from '.'

let cyclesTimer: ReturnType<typeof setTimeout>

const MAX_DECIMAL_DIGITS = 8

const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutput[]; dispatch?: StateDispatch }) => {
const errorAction = {
type: AppActions.AddNotification,
Expand Down Expand Up @@ -44,7 +46,7 @@ const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutp
return true
}
const [, decimal = ''] = item.amount.split('.')
if (decimal.length > 8) {
if (decimal.length > MAX_DECIMAL_DIGITS) {
errorAction.payload.content = Message.InvalidAmount
return true
}
Expand Down Expand Up @@ -155,6 +157,15 @@ const useOnItemChange = (updateTransactionOutput: Function) =>
if (undefined !== value) {
if (field === 'amount') {
const amount = value.replace(/[^\d.]/g, '')
if (Number.isNaN(+amount)) {
return
}

const [, decimal] = amount.split('.')
if (typeof decimal === 'string' && decimal.length > MAX_DECIMAL_DIGITS) {
return
}

updateTransactionOutput(field)(idx)(amount)
} else {
updateTransactionOutput(field)(idx)(value)
Expand Down

0 comments on commit 46ae8c1

Please sign in to comment.