-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
index.tsx
206 lines (185 loc) · 5.35 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React, { useState, useCallback } from "react";
import {
Query, Builder, BuilderProps,
Utils as QbUtils,
JsonSwitchGroup,
Config,
ImmutableTree
} from "@react-awesome-query-builder/ui";
import { LazyStyleModule } from "../skins/utils";
// @ts-ignore
import styles from "@react-awesome-query-builder/mui/css/styles.scss";
import getConfig from "./config";
(styles as LazyStyleModule).use();
const config: Config = getConfig();
const preStyle = { backgroundColor: "darkgrey", margin: "10px", padding: "10px" };
const preErrorStyle = { backgroundColor: "lightpink", margin: "10px", padding: "10px" };
const emptyJsonTree: JsonSwitchGroup = {
id: QbUtils.uuid(),
type: "switch_group",
};
const emptyTree: ImmutableTree = QbUtils.loadTree(emptyJsonTree);
const ternaryJsonLogic = {
"if": [
{
"and": [
{ ">": [ { "var": "discount" }, 50 ] },
{ "==": [ { "var": "is_promotion" }, true ] }
]
},
"Hot",
{
"if": [
{ ">": [ { "var": "qty" }, 0 ] },
"In stock",
"other"
]
}
]
};
const initialTreeFromJsonLogic: ImmutableTree = QbUtils.loadFromJsonLogic(ternaryJsonLogic, config);
const validationErrors = QbUtils.validateTree(initialTreeFromJsonLogic, config);
if (validationErrors?.length) {
console.warn("Validation errors:", validationErrors);
}
const ternarySpel = "((discount > 50 && is_promotion == true) ? 'Hot' : (qty > 0 ? 'In stock' : 'other'))";
const [initialTreeFromSpel, spelLoadingErrors] = QbUtils.loadFromSpel(ternarySpel, config);
if (spelLoadingErrors?.length) {
console.warn("Errors while importing from SpEL: ", spelLoadingErrors);
}
const Demo: React.FC = () => {
const [state, setState] = useState({
tree: initialTreeFromJsonLogic,
// tree: initialTreeFromSpel,
// tree: emptyTree,
config: config,
spelStr: "",
spelErrors: [] as string[],
});
const onChange = useCallback((tree: ImmutableTree, config: Config) => {
setState(prevState => ({ ...prevState, tree, config }));
}, []);
const renderBuilder = useCallback((props: BuilderProps) => (
<div className="query-builder-container" style={{ padding: "10px" }}>
<div className="query-builder qb-lite">
<Builder {...props} />
</div>
</div>
), []);
const onChangeSpelStr = (e: React.ChangeEvent<HTMLInputElement>) => {
const spelStr = e.target.value;
setState({
...state,
spelStr
});
};
const importFromSpel = () => {
const [tree, spelErrors] = QbUtils.loadFromSpel(state.spelStr, state.config);
const {fixedTree, fixedErrors} = QbUtils.sanitizeTree(tree!, state.config);
if (fixedErrors.length) {
console.warn("Fixed errors after import from SpEL:", fixedErrors);
}
setState({
...state,
tree: fixedTree ?? state.tree,
spelErrors
});
};
const renderQueryBuilder = () => (
<Query
{...config}
value={state.tree}
onInit={onChange}
onChange={onChange}
renderBuilder={renderBuilder}
/>
);
const renderSpelInput = () => (
<div className="query-import-spel">
Input SpEL:
<input type="text" size={150} value={state.spelStr} onChange={onChangeSpelStr} />
<button onClick={importFromSpel}>import</button>
<br />
{ state.spelErrors.length > 0
&& <pre style={preErrorStyle}>
{JSON.stringify(state.spelErrors, undefined, 2)}
</pre>
}
</div>
);
const renderSpelBlock = () => {
const [spel, spelErrors] = QbUtils._spelFormat(state.tree, state.config);
return (
<>
<div>
spelFormat:
{ spelErrors.length > 0
&& <pre style={preErrorStyle}>
{JSON.stringify(spelErrors, undefined, 2)}
</pre>
}
<pre style={preStyle}>
{JSON.stringify(spel, undefined, 2)}
</pre>
Values:
<pre>
{JSON.stringify(QbUtils.getSwitchValues(state.tree), undefined, 2)}
</pre>
</div>
<hr/>
</>
);
};
const renderJsTreeBlock = () => {
const treeJs = QbUtils.getTree(state.tree);
return (
<>
Tree:
<pre>
{JSON.stringify(treeJs, undefined, 2)}
</pre>
<br/>
<hr/>
<br/>
</>
);
};
const renderJsonLogicBlock = () => {
const {logic, data: logicData, errors: logicErrors} = QbUtils.jsonLogicFormat(state.tree, state.config);
return (
<>
<div>
<a href="http://jsonlogic.com/play.html" target="_blank" rel="noopener noreferrer">jsonLogicFormat</a>:
{ (logicErrors?.length || 0) > 0
&& <pre style={preErrorStyle}>
{JSON.stringify(logicErrors, undefined, 2)}
</pre>
}
{ !!logic
&& <pre style={preStyle}>
{"// Rule"}:<br />
{JSON.stringify(logic, undefined, 2)}
<br />
<hr />
{"// Data"}:<br />
{JSON.stringify(logicData, undefined, 2)}
</pre>
}
</div>
<hr/>
</>
);
};
return (
<div>
{renderSpelInput()}
{renderQueryBuilder()}
<div className="query-builder-result">
{renderSpelBlock()}
{renderJsonLogicBlock()}
{renderJsTreeBlock()}
</div>
</div>
);
};
export default Demo;