/*eslint @typescript-eslint/no-unused-vars: ["off", {"varsIgnorePattern": "^_"}]*/ import React, {Component} from "react"; import { Query, Builder, Utils, //types: ImmutableTree, Config, BuilderProps, JsonTree, JsonLogicTree } from "react-awesome-query-builder"; import throttle from "lodash/throttle"; import loadedConfig from "./config_simple"; // <- you can try './config' for more complex examples import loadedInitValue from "./init_value"; import loadedInitLogic from "./init_logic"; const stringify = JSON.stringify; const {queryBuilderFormat, jsonLogicFormat, queryString, mongodbFormat, sqlFormat, getTree, checkTree, loadTree, uuid, loadFromJsonLogic} = Utils; const preStyle = { backgroundColor: "darkgrey", margin: "10px", padding: "10px" }; const preErrorStyle = { backgroundColor: "lightpink", margin: "10px", padding: "10px" }; const emptyInitValue: JsonTree = {"id": uuid(), "type": "group"}; // get init value in JsonTree format: const initValue: JsonTree = loadedInitValue && Object.keys(loadedInitValue).length > 0 ? loadedInitValue as JsonTree : emptyInitValue; const initTree: ImmutableTree = checkTree(loadTree(initValue), loadedConfig); // -OR- alternativaly get init value in JsonLogic format: //const initLogic: JsonLogicTree = loadedInitLogic && Object.keys(loadedInitLogic).length > 0 ? loadedInitLogic : undefined; //const initTree: ImmutableTree = checkTree(loadFromJsonLogic(initLogic, loadedConfig), loadedConfig); interface DemoQueryBuilderState { tree: ImmutableTree; config: Config; } export default class DemoQueryBuilder extends Component<{}, DemoQueryBuilderState> { private immutableTree: ImmutableTree; private config: Config; state = { tree: initTree, config: loadedConfig }; render = () => (
{this.renderResult(this.state)}
) resetValue = () => { this.setState({ tree: initTree, }); }; clearValue = () => { this.setState({ tree: loadTree(emptyInitValue), }); }; renderBuilder = (props: BuilderProps) => (
) onChange = (immutableTree: ImmutableTree, config: Config) => { this.immutableTree = immutableTree; this.config = config; this.updateResult(); // `jsonTree` or `logic` can be saved to backend // (and then loaded with `loadTree` or `loadFromJsonLogic` as seen above) const jsonTree = getTree(immutableTree); const {logic, data, errors} = jsonLogicFormat(immutableTree, config); } updateResult = throttle(() => { this.setState({tree: this.immutableTree, config: this.config}); }, 100) renderResult = ({tree: immutableTree, config} : {tree: ImmutableTree, config: Config}) => { const {logic, data, errors} = jsonLogicFormat(immutableTree, config); return (

stringFormat:
              {stringify(queryString(immutableTree, config), undefined, 2)}
            

humanStringFormat:
              {stringify(queryString(immutableTree, config, true), undefined, 2)}
            

sqlFormat:
              {stringify(sqlFormat(immutableTree, config), undefined, 2)}
            

mongodbFormat:
              {stringify(mongodbFormat(immutableTree, config), undefined, 2)}
            

jsonLogicFormat: { errors.length > 0 &&
                {stringify(errors, undefined, 2)}
              
} { !!logic &&
                {"// Rule"}:
{stringify(logic, undefined, 2)}

{"// Data"}:
{stringify(data, undefined, 2)}
}

Tree:
              {stringify(getTree(immutableTree), undefined, 2)}
            
); } }