@@ -7,10 +7,100 @@ import { BranchDiv, Dropdown } from "lowcoder-design";
7
7
import { BottomResTypeEnum } from "types/bottomRes" ;
8
8
import { getPromiseAfterDispatch } from "util/promiseUtils" ;
9
9
import { trans } from "i18n" ;
10
+ import { keyValueListControl , keyValueListToSearchStr , withDefault } from "lowcoder-sdk" ;
11
+ import { KeyValue } from "@lowcoder-ee/types/common" ;
12
+ import { useCallback , useContext , useEffect , useMemo } from "react" ;
10
13
14
+ const ExecuteQueryPropertyView = ( {
15
+ comp,
16
+ placement,
17
+ } : {
18
+ comp : any ,
19
+ placement ?: "query" | "table"
20
+ } ) => {
21
+ const getQueryOptions = useCallback ( ( editorState ?: EditorState ) => {
22
+ const options : { label : string ; value : string ; variable ?: Record < string , string > } [ ] =
23
+ editorState
24
+ ?. queryCompInfoList ( )
25
+ . map ( ( info ) => {
26
+ return {
27
+ label : info . name ,
28
+ value : info . name ,
29
+ variable : info . data . variable ,
30
+ }
31
+ } )
32
+ . filter (
33
+ // Filter out the current query under query
34
+ ( option ) => {
35
+ if (
36
+ placement === "query" &&
37
+ editorState . selectedBottomResType === BottomResTypeEnum . Query
38
+ ) {
39
+ return option . value !== editorState . selectedBottomResName ;
40
+ }
41
+ return true ;
42
+ }
43
+ ) || [ ] ;
44
+
45
+ // input queries
46
+ editorState
47
+ ?. getModuleLayoutComp ( )
48
+ ?. getInputs ( )
49
+ . forEach ( ( i ) => {
50
+ const { name, type } = i . getView ( ) ;
51
+ if ( type === InputTypeEnum . Query ) {
52
+ options . push ( { label : name , value : name } ) ;
53
+ }
54
+ } ) ;
55
+ return options ;
56
+ } , [ placement ] ) ;
57
+
58
+ const getVariableOptions = useCallback ( ( editorState ?: EditorState ) => {
59
+ return comp . children . queryVariables . propertyView ( {
60
+ label : trans ( "eventHandler.queryVariables" ) ,
61
+ layout : "vertical" ,
62
+ isStatic : true ,
63
+ keyFixed : true ,
64
+ } ) ;
65
+ } , [ comp . children . queryVariables . getView ( ) ] )
66
+
67
+ return (
68
+ < >
69
+ < BranchDiv $type = { "inline" } >
70
+ < EditorContext . Consumer >
71
+ { ( editorState ) => (
72
+ < >
73
+ < Dropdown
74
+ showSearch = { true }
75
+ value = { comp . children . queryName . getView ( ) }
76
+ options = { getQueryOptions ( editorState ) }
77
+ label = { trans ( "eventHandler.selectQuery" ) }
78
+ onChange = { ( value ) => {
79
+ const options = getQueryOptions ( editorState ) ;
80
+ const selectedQuery = options . find ( option => option . value === value ) ;
81
+ const variables = selectedQuery ? Object . keys ( selectedQuery . variable || { } ) : [ ] ;
82
+ comp . dispatchChangeValueAction ( {
83
+ queryName : value ,
84
+ queryVariables : variables . map ( ( variable ) => ( { key : variable , value : '' } ) ) ,
85
+ } ) ;
86
+ } }
87
+ />
88
+ </ >
89
+ ) }
90
+ </ EditorContext . Consumer >
91
+ </ BranchDiv >
92
+ < BranchDiv >
93
+ < EditorContext . Consumer >
94
+ { ( editorState ) => getVariableOptions ( editorState ) }
95
+ </ EditorContext . Consumer >
96
+ </ BranchDiv >
97
+ </ >
98
+ ) ;
99
+ }
11
100
const ExecuteQueryTmpAction = ( function ( ) {
12
101
const childrenMap = {
13
102
queryName : SimpleNameComp ,
103
+ queryVariables : withDefault ( keyValueListControl ( false , [ ] , "string" ) , [ ] )
14
104
} ;
15
105
return new MultiCompBuilder ( childrenMap , ( ) => {
16
106
return ( ) => Promise . resolve ( undefined as unknown ) ;
@@ -22,6 +112,15 @@ const ExecuteQueryTmpAction = (function () {
22
112
export class ExecuteQueryAction extends ExecuteQueryTmpAction {
23
113
override getView ( ) {
24
114
const queryName = this . children . queryName . getView ( ) ;
115
+ // const queryParams = keyValueListToSearchStr(Array.isArray(this?.children?.query) ? (this.children.query as unknown as any[]).map((i: any) => i.getView() as KeyValue) : []);
116
+ const result = Object . values ( this . children . queryVariables . children as Record < string , {
117
+ children : {
118
+ key : { unevaledValue : string } ,
119
+ value : { unevaledValue : string }
120
+ } } > )
121
+ . filter ( item => item . children . key . unevaledValue !== "" && item . children . value . unevaledValue !== "" )
122
+ . map ( item => ( { [ item . children . key . unevaledValue ] : item . children . value . unevaledValue } ) )
123
+ . reduce ( ( acc , curr ) => Object . assign ( acc , curr ) , { } ) ;
25
124
if ( ! queryName ) {
26
125
return ( ) => Promise . resolve ( ) ;
27
126
}
@@ -30,9 +129,7 @@ export class ExecuteQueryAction extends ExecuteQueryTmpAction {
30
129
this . dispatch ,
31
130
routeByNameAction (
32
131
queryName ,
33
- executeQueryAction ( {
34
- // can add context in the future
35
- } )
132
+ executeQueryAction ( { args : result } )
36
133
) ,
37
134
{ notHandledError : trans ( "eventHandler.notHandledError" ) }
38
135
) ;
@@ -46,55 +143,11 @@ export class ExecuteQueryAction extends ExecuteQueryTmpAction {
46
143
}
47
144
48
145
propertyView ( { placement } : { placement ?: "query" | "table" } ) {
49
- const getQueryOptions = ( editorState ?: EditorState ) => {
50
- const options : { label : string ; value : string } [ ] =
51
- editorState
52
- ?. queryCompInfoList ( )
53
- . map ( ( info ) => ( {
54
- label : info . name ,
55
- value : info . name ,
56
- } ) )
57
- . filter (
58
- // Filter out the current query under query
59
- ( option ) => {
60
- if (
61
- placement === "query" &&
62
- editorState . selectedBottomResType === BottomResTypeEnum . Query
63
- ) {
64
- return option . value !== editorState . selectedBottomResName ;
65
- }
66
- return true ;
67
- }
68
- ) || [ ] ;
69
-
70
- // input queries
71
- editorState
72
- ?. getModuleLayoutComp ( )
73
- ?. getInputs ( )
74
- . forEach ( ( i ) => {
75
- const { name, type } = i . getView ( ) ;
76
- if ( type === InputTypeEnum . Query ) {
77
- options . push ( { label : name , value : name } ) ;
78
- }
79
- } ) ;
80
- return options ;
81
- } ;
82
146
return (
83
- < BranchDiv $type = { "inline" } >
84
- < EditorContext . Consumer >
85
- { ( editorState ) => (
86
- < >
87
- < Dropdown
88
- showSearch = { true }
89
- value = { this . children . queryName . getView ( ) }
90
- options = { getQueryOptions ( editorState ) }
91
- label = { trans ( "eventHandler.selectQuery" ) }
92
- onChange = { ( value ) => this . dispatchChangeValueAction ( { queryName : value } ) }
93
- />
94
- </ >
95
- ) }
96
- </ EditorContext . Consumer >
97
- </ BranchDiv >
98
- ) ;
147
+ < ExecuteQueryPropertyView
148
+ comp = { this }
149
+ placement = { placement }
150
+ />
151
+ )
99
152
}
100
153
}
0 commit comments