1
- import React , { useContext , useState } from 'react' ;
1
+ import React , { useContext , useState , useCallback } from 'react' ;
2
2
import {
3
3
BLOCK_CONTEXT ,
4
4
} from '@rocket.chat/ui-kit' ;
@@ -14,26 +14,28 @@ export const kitContext = React.createContext(defaultContext);
14
14
15
15
export const useBlockContext = ( { blockId, actionId, appId, initialValue } , context ) => {
16
16
const { action, appId : appIdFromContext , viewId, state, errors, values = { } } = useContext ( kitContext ) ;
17
- const { value = initialValue } = values [ actionId ] || { } ;
18
- // const [value, setValue] = useState(initialValue );
17
+ const { value : _value = initialValue } = values [ actionId ] || { } ;
18
+ const [ value , setValue ] = useState ( _value ) ;
19
19
const [ loading , setLoading ] = useState ( false ) ;
20
20
21
21
const error = errors && actionId && errors [ actionId ] ;
22
22
23
+ const actionFunction = useCallback ( async ( { target : { value } } ) => {
24
+ setLoading ( true ) ;
25
+ await action ( { blockId, appId : appId || appIdFromContext , actionId, value, viewId } ) ;
26
+ setLoading ( false ) ;
27
+ } , [ ] ) ;
28
+
29
+ const stateFunction = useCallback ( async ( { target : { value } } ) => {
30
+ setValue ( value ) ;
31
+ await state ( { blockId, appId, actionId, value } ) ;
32
+ } , [ ] ) ;
33
+
23
34
if ( [ BLOCK_CONTEXT . SECTION , BLOCK_CONTEXT . ACTION ] . includes ( context ) ) {
24
- return [ { loading, setLoading, error } , async ( { target : { value } } ) => {
25
- setLoading ( true ) ;
26
- await action ( { blockId, appId : appId || appIdFromContext , actionId, value, viewId } ) ;
27
- setLoading ( false ) ;
28
- } ] ;
35
+ return [ { loading, setLoading, error } , actionFunction ] ;
29
36
}
30
37
31
- return [ { loading, setLoading, value, error } , async ( { target : { value } } ) => {
32
- // setValue(value);
33
- setLoading ( true ) ;
34
- await state ( { blockId, appId, actionId, value } ) ;
35
- setLoading ( false ) ;
36
- } ] ;
38
+ return [ { loading, setLoading, value, error } , stateFunction ] ;
37
39
} ;
38
40
39
41
export const getStyle = ( style ) => {
0 commit comments