import { Dispatch, useCallback } from "react";
import { Button, Intent, MenuItem } from "@blueprintjs/core";
import { Select } from "@blueprintjs/select";
import { Placement } from "@blueprintjs/popover2";
import InputComponent from "../InputComponent";
import { QueryOperator, defaultQueryTerm, queries } from "../queries";
import { returnSiblingArray } from "../utils";
import { CustomClasses } from "../../../../../constants";
import { QueryProperty, QueryTerm, QueryEntryAction } from "../types";
import "./_index.sass";
const popoverProps = {
minimal: true,
placement: "bottom" as Placement,
popoverClassName: CustomClasses.POPOVER
};
const propertyPopoverProps = {
...popoverProps,
targetProps: {
title: "Select a property to query"
}
};
const relationshipPopoverProps = {
...popoverProps,
targetProps: {
title: "Select a relationship to test for the property"
}
};
function itemRenderer(item, itemProps) {
const { handleClick/*, modifiers: { active }*/ } = itemProps;
return ;
}
type OwnProps = {
dispatch: Dispatch,
isFirstChild: boolean,
isOnlyChild: boolean,
term: QueryTerm,
useOR?: boolean
};
function QueryEntry({ dispatch, isFirstChild, isOnlyChild, term, useOR = false }: OwnProps){
const { property, relationship, value = "" } = term;
const addSiblingTerm = useCallback(() => {
dispatch({ type: "updateSelf", value: returnSiblingArray(term, defaultQueryTerm) });
}, [dispatch, term]);
const removeSelf = useCallback(() => dispatch({ type: "removeSelf" }), [dispatch]);
const handleUpdate = useCallback((update) => {
dispatch({ type: "updateSelf", value: { ...term, ...update } });
}, [dispatch, term]);
const handlePropertyChange = useCallback((newProp: QueryProperty) => {
const updates: Partial = { property: newProp };
const newOperatorList = queries[newProp] as typeof queries[keyof typeof queries];
// Update relationship also, if necessary
if(!(relationship in newOperatorList)){ updates.relationship = Object.keys(newOperatorList)[0]; }
// Update value also, if necessary
if (updates.relationship) {
const newOperator = newOperatorList[updates.relationship] as QueryOperator;
if (!newOperator.checkInput(value)) { updates.value = newOperator.defaultInput; }
}
handleUpdate(updates);
}, [handleUpdate, relationship, value]);
const handleRelationshipChange = useCallback((newRel: string) => {
const updates: Partial = { relationship: newRel };
const newOperator = queries[property][newRel] as QueryOperator;
// Update value also, if necessary
if(!newOperator.checkInput(value)) { updates.value = newOperator.defaultInput; }
handleUpdate(updates);
}, [handleUpdate, property, value]);
const handleValueChange = useCallback((val) => handleUpdate({ value: val }), [handleUpdate]);
return
{!isFirstChild &&
{useOR ? "AND" : "OR"}}
{isOnlyChild
? null
:
}
;
}
export default QueryEntry;