import * as React from "react"; import { cloneDeep, capitalize } from "lodash"; import { Row, Col, FBSelect, DropDownItem } from "../../../ui"; import { AddEqCriteria, toggleEqCriteria, editCriteria, AddNumberCriteria, POINTER_TYPE_DDI_LOOKUP, AddStringCriteria, CRITERIA_TYPE_DDI_LOOKUP, toggleStringCriteria } from "."; import { EqCriteriaSelectionProps, NumberCriteriaProps, CriteriaSelectionProps, LocationSelectionProps, GroupCriteriaProps, AddCriteriaState, DEFAULT_CRITERIA } from "./interfaces"; import { t } from "../../../i18next_wrapper"; import { PointGroup } from "farmbot/dist/resources/api_resources"; import { PLANT_STAGE_DDI_LOOKUP } from "../../plants/edit_plant_status"; export class EqCriteriaSelection extends React.Component> { render() { const { criteriaField, criteriaKey, group, dispatch } = this.props; return
group={group} dispatch={dispatch} type={this.props.type} criteriaField={criteriaField} criteriaKey={criteriaKey} /> {criteriaField && Object.entries(criteriaField) .map(([key, values]: [string, T[]], keyIndex) => values && values.length > 0 &&
{values.map((value, valueIndex) => )}
)}
; } } export const NumberCriteriaSelection = (props: NumberCriteriaProps) => { const criteriaField = props.criteria[props.criteriaKey]; return
{criteriaField && Object.entries(criteriaField) .map(([key, value], keyIndex) =>

{key}

{props.criteriaKey == "number_gt" ? ">" : "<"}
)}
; }; const DAY_OPERATOR_DDI_LOOKUP = (): { [x: string]: DropDownItem } => ({ ["<"]: { label: t("less than"), value: "<" }, [">"]: { label: t("greater than"), value: ">" }, }); export const DaySelection = (props: CriteriaSelectionProps) => { const { group, criteria, dispatch } = props; const dayCriteria = criteria.day || cloneDeep(DEFAULT_CRITERIA.day); return
"]]} selectedItem={DAY_OPERATOR_DDI_LOOKUP()[dayCriteria.op]} onChange={ddi => dispatch(editCriteria(group, { day: { days_ago: dayCriteria.days_ago, op: ddi.value as PointGroup["criteria"]["day"]["op"] } }))} /> { const { op } = dayCriteria; const days_ago = parseInt(e.currentTarget.value); dispatch(editCriteria(group, { day: { days_ago, op } })); }} />

{t("days old")}

; }; export const LocationSelection = (props: LocationSelectionProps) => { const { group, criteria, dispatch } = props; const gtCriteria = criteria.number_gt || {}; const ltCriteria = criteria.number_lt || {}; return
{["x", "y"].map(axis => { const tempGtCriteria = cloneDeep(gtCriteria); tempGtCriteria[axis] = parseInt(e.currentTarget.value); dispatch(editCriteria(group, { number_gt: tempGtCriteria })); }} />

{"<"}

{"<"}

{ const tempLtCriteria = cloneDeep(ltCriteria); tempLtCriteria[axis] = parseInt(e.currentTarget.value); dispatch(editCriteria(group, { number_lt: tempLtCriteria })); }} />
)}
; }; export class AddCriteria extends React.Component { labelLookup = (key: string, value: string) => { switch (key) { case "openfarm_slug": return capitalize(value); case "pointer_type": return POINTER_TYPE_DDI_LOOKUP()[value].label; case "plant_stage": return PLANT_STAGE_DDI_LOOKUP()[value].label; } } render() { const { props } = this; const stringCriteria = this.props.group.body.criteria?.string_eq || {}; const displayedCriteria = Object.entries(stringCriteria) .filter(([key, _values]) => ["openfarm_slug", "pointer_type", "plant_stage"].includes(key)); return
{displayedCriteria.map(([key, values]) => values && values.map((value, index) =>
))}
; } }