@@ -2,12 +2,16 @@ import React, { useCallback, useEffect } from 'react'
22import { IDropdownOption } from 'office-ui-fabric-react'
33
44import { AppActions , StateDispatch } from 'states/stateProvider/reducer'
5+ import { calculateCycles } from 'services/remote/wallets'
56
67import { Message } from 'utils/const'
78import { verifyAddress , verifyAmountRange } from 'utils/validators'
9+ import { CKBToShannonFormatter } from 'utils/formatters'
810import { TransactionOutput } from '.'
911
10- const validateTransactionParams = ( { items, dispatch } : { items : TransactionOutput [ ] ; dispatch : StateDispatch } ) => {
12+ let cyclesTimer : ReturnType < typeof setTimeout >
13+
14+ const validateTransactionParams = ( { items, dispatch } : { items : TransactionOutput [ ] ; dispatch ?: StateDispatch } ) => {
1115 const errorAction = {
1216 type : AppActions . AddNotification ,
1317 payload : {
@@ -17,7 +21,9 @@ const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutp
1721 } ,
1822 }
1923 if ( ! items . length || ! items [ 0 ] . address ) {
20- dispatch ( errorAction )
24+ if ( dispatch ) {
25+ dispatch ( errorAction )
26+ }
2127 return false
2228 }
2329 const invalid = items . some (
@@ -42,7 +48,7 @@ const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutp
4248 return false
4349 }
4450 )
45- if ( invalid ) {
51+ if ( invalid && dispatch ) {
4652 dispatch ( errorAction )
4753 return false
4854 }
@@ -84,6 +90,42 @@ const useRemoveTransactionOutput = (dispatch: StateDispatch) =>
8490 [ dispatch ]
8591 )
8692
93+ const useOnTransactionChange = ( walletID : string , items : TransactionOutput [ ] , dispatch : StateDispatch ) => {
94+ useEffect ( ( ) => {
95+ clearTimeout ( cyclesTimer )
96+ cyclesTimer = setTimeout ( ( ) => {
97+ if ( validateTransactionParams ( { items } ) ) {
98+ calculateCycles ( {
99+ walletID,
100+ items : items . map ( item => ( {
101+ address : item . address ,
102+ capacity : CKBToShannonFormatter ( item . amount , item . unit ) ,
103+ } ) ) ,
104+ } )
105+ . then ( response => {
106+ if ( response . status ) {
107+ if ( Number . isNaN ( + response . result ) ) {
108+ throw new Error ( 'Invalid Cycles' )
109+ }
110+ dispatch ( {
111+ type : AppActions . UpdateSendCycles ,
112+ payload : response . result ,
113+ } )
114+ } else {
115+ throw new Error ( 'Cycles Not Calculated' )
116+ }
117+ } )
118+ . catch ( ( ) => {
119+ dispatch ( {
120+ type : AppActions . UpdateSendCycles ,
121+ payload : '0' ,
122+ } )
123+ } )
124+ }
125+ } , 300 )
126+ } , [ walletID , items , dispatch ] )
127+ }
128+
87129const useOnSubmit = ( items : TransactionOutput [ ] , dispatch : StateDispatch ) =>
88130 useCallback (
89131 ( walletID : string = '' ) => ( ) => {
@@ -188,6 +230,7 @@ export const useInitialize = (
188230 } , [ address , dispatch , history , updateTransactionOutput ] )
189231
190232 return {
233+ useOnTransactionChange,
191234 updateTransactionOutput,
192235 onItemChange,
193236 onCapacityUnitChange,
0 commit comments