1- import React , { useCallback , useEffect , useMemo } from 'react'
1+ import React , { useCallback , useEffect } from 'react'
22import { IDropdownOption } from 'office-ui-fabric-react'
33
44import { AppActions , StateDispatch } from 'states/stateProvider/reducer'
5- import actionCreators from 'states/stateProvider/actionCreators'
65
6+ import { Message } from 'utils/const'
7+ import { verifyAddress } from 'utils/validators'
78import { TransactionOutput } from '.'
89
10+ const validateTransactionParams = ( { items, dispatch } : { items : TransactionOutput [ ] ; dispatch : StateDispatch } ) => {
11+ const errorAction = {
12+ type : AppActions . AddNotification ,
13+ payload : {
14+ type : 'warning' ,
15+ timestamp : Date . now ( ) ,
16+ content : Message . AtLeastOneAddressNeeded ,
17+ } ,
18+ }
19+ if ( ! items . length || ! items [ 0 ] . address ) {
20+ dispatch ( errorAction )
21+ return false
22+ }
23+ const invalid = items . some (
24+ ( item ) : boolean => {
25+ if ( ! verifyAddress ( item . address ) ) {
26+ errorAction . payload . content = Message . InvalidAddress
27+ return true
28+ }
29+ if ( Number . isNaN ( + item . amount ) || + item . amount < 0 ) {
30+ errorAction . payload . content = Message . InvalidAmount
31+ return true
32+ }
33+ const [ , decimal = '' ] = item . amount . split ( '.' )
34+ if ( decimal . length > 8 ) {
35+ errorAction . payload . content = Message . InvalidAmount
36+ return true
37+ }
38+ return false
39+ }
40+ )
41+ if ( invalid ) {
42+ dispatch ( errorAction )
43+ return false
44+ }
45+ return true
46+ }
47+
948const useUpdateTransactionOutput = ( dispatch : StateDispatch ) =>
1049 useCallback (
1150 ( field : string ) => ( idx : number ) => ( value : string ) => {
@@ -14,7 +53,7 @@ const useUpdateTransactionOutput = (dispatch: StateDispatch) =>
1453 payload : {
1554 idx,
1655 item : {
17- [ field ] : value ,
56+ [ field ] : value . trim ( ) ,
1857 } ,
1958 } ,
2059 } )
@@ -41,24 +80,38 @@ const useRemoveTransactionOutput = (dispatch: StateDispatch) =>
4180 [ dispatch ]
4281 )
4382
44- const useOnSubmit = ( dispatch : StateDispatch ) =>
83+ const useOnSubmit = ( items : TransactionOutput [ ] , dispatch : StateDispatch ) =>
4584 useCallback (
46- ( id : string = '' , walletID : string = '' , items : TransactionOutput [ ] = [ ] , description : string = '' ) => ( ) => {
47- setTimeout ( ( ) => {
48- dispatch ( actionCreators . submitTransaction ( id , walletID , items , description ) )
49- } , 10 )
85+ ( walletID : string = '' ) => ( ) => {
86+ if ( validateTransactionParams ( { items, dispatch } ) ) {
87+ dispatch ( {
88+ type : AppActions . UpdateTransactionID ,
89+ payload : null ,
90+ } )
91+ dispatch ( {
92+ type : AppActions . RequestPassword ,
93+ payload : {
94+ walletID,
95+ actionType : 'send' ,
96+ } ,
97+ } )
98+ }
5099 } ,
51- [ dispatch ]
100+ [ dispatch , items ]
52101 )
53102
54- const useOnItemChange = ( updateTransactionOutput : Function ) => ( field : string = '' , idx : number = - 1 ) => (
55- _e : React . FormEvent < HTMLInputElement | HTMLTextAreaElement > ,
56- value ?: string
57- ) => {
58- if ( undefined !== value ) {
59- updateTransactionOutput ( field ) ( idx ) ( value )
60- }
61- }
103+ const useOnItemChange = ( updateTransactionOutput : Function ) =>
104+ useCallback (
105+ ( field : string = '' , idx : number = - 1 ) => (
106+ _e : React . FormEvent < HTMLInputElement | HTMLTextAreaElement > ,
107+ value ?: string
108+ ) => {
109+ if ( undefined !== value ) {
110+ updateTransactionOutput ( field ) ( idx ) ( value )
111+ }
112+ } ,
113+ [ updateTransactionOutput ]
114+ )
62115
63116const useCapacityUnitChange = ( updateTransactionOutput : Function ) =>
64117 useCallback (
@@ -76,7 +129,7 @@ const useUpdateTransactionPrice = (dispatch: StateDispatch) =>
76129 if ( undefined !== value ) {
77130 dispatch ( {
78131 type : AppActions . UpdateSendPrice ,
79- payload : value ,
132+ payload : value . trim ( ) ,
80133 } )
81134 }
82135 } ,
@@ -105,11 +158,16 @@ const clear = (dispatch: StateDispatch) => {
105158
106159const useClear = ( dispatch : StateDispatch ) => useCallback ( ( ) => clear ( dispatch ) , [ dispatch ] )
107160
108- export const useInitialize = ( address : string , dispatch : React . Dispatch < any > , history : any ) => {
161+ export const useInitialize = (
162+ address : string ,
163+ items : TransactionOutput [ ] ,
164+ dispatch : React . Dispatch < any > ,
165+ history : any
166+ ) => {
109167 const updateTransactionOutput = useUpdateTransactionOutput ( dispatch )
110168 const onItemChange = useOnItemChange ( updateTransactionOutput )
111169 const onCapacityUnitChange = useCapacityUnitChange ( updateTransactionOutput )
112- const onSubmit = useOnSubmit ( dispatch )
170+ const onSubmit = useOnSubmit ( items , dispatch )
113171 const addTransactionOutput = useAddTransactionOutput ( dispatch )
114172 const removeTransactionOutput = useRemoveTransactionOutput ( dispatch )
115173 const updateTransactionPrice = useUpdateTransactionPrice ( dispatch )
@@ -125,11 +183,7 @@ export const useInitialize = (address: string, dispatch: React.Dispatch<any>, hi
125183 }
126184 } , [ address , dispatch , history , updateTransactionOutput ] )
127185
128- // TODO: generate new id on every submission
129- const id = useMemo ( ( ) => Math . round ( Math . random ( ) * 1000 ) . toString ( ) , [ ] )
130-
131186 return {
132- id,
133187 updateTransactionOutput,
134188 onItemChange,
135189 onCapacityUnitChange,
0 commit comments