Skip to content

Commit a2503ff

Browse files
committed
feat(neuron-ui): add more detailed error messages of the amount field
1 parent 46ae8c1 commit a2503ff

4 files changed

Lines changed: 26 additions & 28 deletions

File tree

packages/neuron-ui/src/components/Send/hooks.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import { IDropdownOption } from 'office-ui-fabric-react'
44
import { AppActions, StateDispatch } from 'states/stateProvider/reducer'
55
import { calculateCycles } from 'services/remote/wallets'
66

7-
import { Message } from 'utils/const'
7+
import { Message, MAX_DECIMAL_DIGITS } from 'utils/const'
88
import { verifyAddress, verifyAmountRange } from 'utils/validators'
99
import { outputsToTotalCapacity } from 'utils/formatters'
1010
import { TransactionOutput } from '.'
1111

1212
let cyclesTimer: ReturnType<typeof setTimeout>
1313

14-
const MAX_DECIMAL_DIGITS = 8
15-
1614
const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutput[]; dispatch?: StateDispatch }) => {
1715
const errorAction = {
1816
type: AppActions.AddNotification,
@@ -39,22 +37,27 @@ const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutp
3937
}
4038
if (Number.isNaN(+item.amount) || +item.amount < 0) {
4139
errorAction.payload.content = Message.InvalidAmount
42-
return true
43-
}
44-
if (!verifyAmountRange(item.amount)) {
45-
errorAction.payload.content = Message.AmountTooSmall
40+
errorAction.payload.meta = { amount: item.amount }
4641
return true
4742
}
4843
const [, decimal = ''] = item.amount.split('.')
4944
if (decimal.length > MAX_DECIMAL_DIGITS) {
50-
errorAction.payload.content = Message.InvalidAmount
45+
errorAction.payload.content = Message.DecimalExceed
46+
errorAction.payload.meta = { amount: item.amount }
47+
return true
48+
}
49+
if (!verifyAmountRange(item.amount)) {
50+
errorAction.payload.content = Message.AmountTooSmall
51+
errorAction.payload.meta = { amount: item.amount }
5152
return true
5253
}
5354
return false
5455
}
5556
)
56-
if (invalid && dispatch) {
57-
dispatch(errorAction)
57+
if (invalid) {
58+
if (dispatch) {
59+
dispatch(errorAction)
60+
}
5861
return false
5962
}
6063
return true
@@ -156,17 +159,10 @@ const useOnItemChange = (updateTransactionOutput: Function) =>
156159
) => {
157160
if (undefined !== value) {
158161
if (field === 'amount') {
159-
const amount = value.replace(/[^\d.]/g, '')
160-
if (Number.isNaN(+amount)) {
162+
if (Number.isNaN(+value) || /[^\d.]/.test(value)) {
161163
return
162164
}
163-
164-
const [, decimal] = amount.split('.')
165-
if (typeof decimal === 'string' && decimal.length > MAX_DECIMAL_DIGITS) {
166-
return
167-
}
168-
169-
updateTransactionOutput(field)(idx)(amount)
165+
updateTransactionOutput(field)(idx)(value)
170166
} else {
171167
updateTransactionOutput(field)(idx)(value)
172168
}

packages/neuron-ui/src/locales/en.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,6 @@
240240
"protocol-required": "Protocol is required",
241241
"length-of-name-should-be-less-than-or-equal-to": "Length of name should be less than or equal to {{length}}",
242242
"network-name-used": "Network name is used",
243-
"invalid-amount": "Invalid amount",
244-
"amount-not-enough": "Amount is not enough",
245243
"is-unremovable": "{{target}} is unremovable",
246244
"create-wallet-success": "You have created wallet '{{name}}' successfully",
247245
"network-is-not-found": "Network is not found",
@@ -256,7 +254,6 @@
256254
"invalid-mnemonic": "Invalid mnemonic words",
257255
"camera-not-available-or-disabled": "Camera is unavailable or disabled",
258256
"can-not-find-the-default-address": "Cannot find the default address",
259-
"amount-too-small": "Amount should not be less than 61 CKB",
260257
"create-wallet-successfully": "Create a wallet successfully",
261258
"import-wallet-successfully": "Import a wallet successfully",
262259
"update-wallet-successfully": "Update the wallet successfully",
@@ -272,7 +269,10 @@
272269
"rpc-url-should-have-no-whitespaces": "The RPC URL should have no whitespaces",
273270
"is-required": "{{field}} is required",
274271
"is-used": "{{field}} is used",
275-
"invalid-address": "{{address}} is an invalid address"
272+
"invalid-address": "{{address}} is an invalid address",
273+
"amount-decimal-exceed": "The amount {{amount}} CKB should has less than 9 decimal digits",
274+
"invalid-amount": "The amount {{amount}} CKB is invalid",
275+
"amount-too-small": "The amount {{amount}} CKB is too small, please set a value large than or equal to 61 CKB"
276276
},
277277
"sync": {
278278
"syncing": "Syncing",

packages/neuron-ui/src/locales/zh.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,6 @@
240240
"protocol-required": "请指定 URL 协议",
241241
"length-of-name-should-be-less-than-or-equal-to": "名称长度应不大于 {{length}}",
242242
"network-name-used": "节点名称已存在",
243-
"invalid-amount": "无效的价值",
244-
"amount-not-enough": "余额不足",
245243
"is-unremovable": "{{target}}不可删除",
246244
"create-wallet-success": "您已成功创建钱包 '{{name}}'",
247245
"network-is-not-found": "未找到节点信息",
@@ -256,7 +254,6 @@
256254
"invalid-mnemonic": "助记词不合法",
257255
"camera-not-available-or-disabled": "摄像头不可用或被禁用",
258256
"can-not-find-the-default-address": "未获得默认地址",
259-
"amount-too-small": "金额应不小于 61 CKB",
260257
"create-wallet-successfully": "新建钱包成功",
261258
"import-wallet-successfully": "导入钱包成功",
262259
"update-wallet-successfully": "已更新钱包信息",
@@ -272,7 +269,10 @@
272269
"network-address-should-have-no-whitespaces": "RPC 地址不能包含空格",
273270
"is-required": "{{field}}是必须的",
274271
"is-used": "{{field}}已使用",
275-
"invalid-address": "{{address}} 是无效的地址"
272+
"invalid-address": "{{address}} 是无效的地址",
273+
"amount-decimal-exceed": "金额 {{amount}} 的小数不应超过 8 位",
274+
"invalid-amount": "金额 {{amount} 是无效的数字",
275+
"amount-too-small": "金额 {{amount}} 太小, 请设定一个不小于 61 CKB 的值"
276276
},
277277
"sync": {
278278
"syncing": "同步中",

packages/neuron-ui/src/utils/const.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const UNREMOVABLE_NETWORK = 'Testnet'
88
export const UNREMOVABLE_NETWORK_ID = '0'
99
export const CONFIRMATION_THRESHOLD = 10
1010

11+
export const MAX_DECIMAL_DIGITS = 8
12+
1113
export enum ConnectionStatus {
1214
Online = 'online',
1315
Offline = 'offline',
@@ -55,7 +57,7 @@ export enum Message {
5557
AtLeastOneAddressNeeded = 'messages.at-least-one-address-needed',
5658
InvalidAddress = 'messages.invalid-address',
5759
InvalidAmount = 'messages.invalid-amount',
58-
AmountNotEnough = 'messages.amount-not-enough',
60+
DecimalExceed = 'messages.amount-decimal-exceed',
5961
IsUnremovable = 'messages.is-unremovable',
6062
ProtocolRequired = 'messages.protocol-required',
6163
AmountTooSmall = 'messages.amount-too-small',

0 commit comments

Comments
 (0)