Skip to content

Commit 7bf6dbc

Browse files
committed
feat(neuron-ui): show the error message from api controller on send view.
1 parent 22ab3d8 commit 7bf6dbc

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ const useOnTransactionChange = (
5252
items: TransactionOutput[],
5353
price: string,
5454
dispatch: StateDispatch,
55-
setIsTransactionValid: Function,
56-
setTotalAmount: Function
55+
setTotalAmount: Function,
56+
setErrorMessage: Function
5757
) => {
5858
useEffect(() => {
5959
clearTimeout(generateTxTimer)
60+
setErrorMessage('')
6061
generateTxTimer = setTimeout(() => {
6162
dispatch({
6263
type: AppActions.UpdateGeneratedTx,
6364
payload: null,
6465
})
6566
if (verifyTransactionOutputs(items)) {
66-
setIsTransactionValid(true)
6767
const totalAmount = outputsToTotalAmount(items)
6868
setTotalAmount(totalAmount)
6969
const realParams = {
@@ -81,16 +81,25 @@ const useOnTransactionChange = (
8181
type: AppActions.UpdateGeneratedTx,
8282
payload: res.result,
8383
})
84+
} else {
85+
throw new Error(res.message.content)
8486
}
8587
})
8688
.catch((err: Error) => {
87-
console.error(err)
89+
dispatch({
90+
type: AppActions.UpdateGeneratedTx,
91+
payload: '',
92+
})
93+
setErrorMessage(err.message)
8894
})
8995
} else {
90-
setIsTransactionValid(false)
96+
dispatch({
97+
type: AppActions.UpdateGeneratedTx,
98+
payload: '',
99+
})
91100
}
92101
}, 300)
93-
}, [walletID, items, price, dispatch, setIsTransactionValid, setTotalAmount])
102+
}, [walletID, items, price, dispatch, setTotalAmount])
94103
}
95104

96105
const useOnSubmit = (items: TransactionOutput[], dispatch: StateDispatch) =>
@@ -176,8 +185,8 @@ export const useInitialize = (
176185
) => {
177186
const fee = useMemo(() => calculateFee(generatedTx), [generatedTx])
178187

179-
const [isTransactionValid, setIsTransactionValid] = useState(false)
180188
const [totalAmount, setTotalAmount] = useState('0')
189+
const [errorMessage, setErrorMessage] = useState('')
181190

182191
const updateTransactionOutput = useUpdateTransactionOutput(dispatch)
183192
const onItemChange = useOnItemChange(updateTransactionOutput)
@@ -235,8 +244,6 @@ export const useInitialize = (
235244
fee,
236245
totalAmount,
237246
setTotalAmount,
238-
isTransactionValid,
239-
setIsTransactionValid,
240247
useOnTransactionChange,
241248
onItemChange,
242249
addTransactionOutput,
@@ -247,6 +254,8 @@ export const useInitialize = (
247254
onGetAmountErrorMessage,
248255
onSubmit,
249256
onClear,
257+
errorMessage,
258+
setErrorMessage,
250259
}
251260
}
252261

packages/neuron-ui/src/components/Send/index.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ const Send = ({
4545
fee,
4646
totalAmount,
4747
setTotalAmount,
48-
isTransactionValid,
49-
setIsTransactionValid,
5048
useOnTransactionChange,
5149
onItemChange,
5250
onSubmit,
@@ -57,12 +55,17 @@ const Send = ({
5755
onGetAddressErrorMessage,
5856
onGetAmountErrorMessage,
5957
onClear,
58+
errorMessage,
59+
setErrorMessage,
6060
} = useInitialize(walletID, send.outputs, send.generatedTx, dispatch, t)
61-
useOnTransactionChange(walletID, send.outputs, send.price, dispatch, setIsTransactionValid, setTotalAmount)
61+
useOnTransactionChange(walletID, send.outputs, send.price, dispatch, setTotalAmount, setErrorMessage)
6262
const leftStackWidth = '70%'
6363
const labelWidth = '140px'
6464

65-
const isAffordable = verifyTotalAmount(totalAmount, fee, balance)
65+
let errorMessageUnderTotal = errorMessage
66+
if (!errorMessageUnderTotal && !verifyTotalAmount(totalAmount, fee, balance)) {
67+
errorMessageUnderTotal = t(`messages.codes.${ErrorCode.AmountNotEnough}`)
68+
}
6669

6770
return (
6871
<Stack verticalFill tokens={{ childrenGap: 15, padding: '20px 0 0 0' }}>
@@ -178,7 +181,7 @@ const Send = ({
178181
styles={{
179182
root: {
180183
width: leftStackWidth,
181-
display: send.outputs.length > 1 || !isAffordable ? 'flex' : 'none',
184+
display: send.outputs.length > 1 || errorMessageUnderTotal ? 'flex' : 'none',
182185
},
183186
}}
184187
tokens={{ childrenGap: 20 }}
@@ -192,7 +195,7 @@ const Send = ({
192195
alt={t('send.total-amount')}
193196
value={`${shannonToCKBFormatter(totalAmount)} CKB`}
194197
readOnly
195-
errorMessage={isAffordable ? '' : t(`messages.codes.${ErrorCode.AmountNotEnough}`)}
198+
errorMessage={errorMessageUnderTotal}
196199
/>
197200
</Stack.Item>
198201
</Stack>
@@ -232,7 +235,7 @@ const Send = ({
232235
<PrimaryButton
233236
type="submit"
234237
onClick={onSubmit(walletID)}
235-
disabled={sending || !isTransactionValid || !isAffordable || !send.generatedTx}
238+
disabled={sending || !!errorMessageUnderTotal || !send.generatedTx}
236239
text={t('send.send')}
237240
/>
238241
)}

0 commit comments

Comments
 (0)