Farmbot-Web-App/frontend/sequences/step_tiles/tile_execute_script.tsx

112 lines
4.0 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
import { StepParams } from "../interfaces";
2019-12-27 11:37:54 -07:00
import { ToolTips, Content } from "../../constants";
2017-08-14 12:49:34 -06:00
import { StepInputBox } from "../inputs/step_input_box";
2019-12-27 11:37:54 -07:00
import {
StepWrapper, StepHeader, StepContent, StepWarning
} from "../step_ui/index";
2018-02-14 22:07:36 -07:00
import { Row, Col, FBSelect, DropDownItem } from "../../ui/index";
2018-05-18 16:34:12 -06:00
import { editStep } from "../../api/crud";
import { ExecuteScript, FarmwareConfig } from "farmbot";
import { FarmwareInputs, farmwareList } from "./tile_execute_script_support";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2017-06-29 12:54:02 -06:00
2018-08-30 19:25:58 -06:00
export function TileExecuteScript(props: StepParams) {
2019-12-27 11:37:54 -07:00
const { dispatch, currentStep, index, currentSequence, farmwareData } = props;
2017-06-29 12:54:02 -06:00
if (currentStep.kind === "execute_script") {
2018-02-14 22:07:36 -07:00
2018-05-18 16:34:12 -06:00
const farmwareName = currentStep.args.label;
/** Selected Farmware is installed on connected bot. */
const isInstalled = (name: string): boolean => {
2019-12-27 11:37:54 -07:00
return !!(farmwareData && farmwareData.farmwareNames.includes(name));
2018-02-14 22:07:36 -07:00
};
2018-05-18 16:34:12 -06:00
const selectedFarmwareDDI = (name: string): DropDownItem => {
if (isInstalled(name)) {
2019-05-15 10:12:26 -06:00
return name === "plant-detection"
? { value: name, label: t("Weed Detector") }
: { value: name, label: name };
2018-02-14 22:07:36 -07:00
}
2019-09-11 11:50:48 -06:00
return { label: t("Manual Input"), value: "" };
2018-05-18 16:34:12 -06:00
};
/** dispatch editStep for current ExecuteScript step */
const updateStep = (executor: (stepCopy: ExecuteScript) => void) => {
2018-05-21 17:49:34 -06:00
dispatch(editStep({
2018-05-18 16:34:12 -06:00
sequence: currentSequence,
step: currentStep,
index: index,
executor
}));
};
/** Change step Farmware name. */
const updateStepFarmwareSelection = (item: DropDownItem) => {
updateStep((step: ExecuteScript) => {
2018-06-06 13:47:03 -06:00
if (step.body && (step.args.label !== "" + item.value)) {
// Clear step body when switching to a different Farmware
delete step.body;
}
2018-05-18 16:34:12 -06:00
step.args.label = "" + item.value;
});
2018-02-14 22:07:36 -07:00
};
2018-05-18 16:34:12 -06:00
/** Configs (inputs) from Farmware manifest for <FarmwareInputs />. */
const currentFarmwareConfigDefaults = (fwName: string): FarmwareConfig[] => {
2020-01-03 13:04:45 -07:00
return farmwareData?.farmwareConfigs[fwName]
2019-12-27 11:37:54 -07:00
? farmwareData.farmwareConfigs[fwName]
2018-05-18 16:34:12 -06:00
: [];
2018-02-14 22:07:36 -07:00
};
2017-12-15 16:09:24 -07:00
const className = "execute-script-step";
return <StepWrapper>
<StepHeader
className={className}
helpText={ToolTips.EXECUTE_SCRIPT}
currentSequence={currentSequence}
currentStep={currentStep}
dispatch={dispatch}
2018-08-30 19:25:58 -06:00
index={index}
2019-12-27 11:37:54 -07:00
confirmStepDeletion={props.confirmStepDeletion}>
{props.farmwareData && props.farmwareData.cameraDisabled &&
(farmwareName === "plant-detection") &&
<StepWarning
titleBase={t(Content.NO_CAMERA_SELECTED)}
warning={t(ToolTips.SELECT_A_CAMERA)} />}
</StepHeader>
2017-12-15 16:09:24 -07:00
<StepContent className={className}>
<Row>
<Col xs={12}>
<label>{t("Package Name")}</label>
2018-02-14 22:07:36 -07:00
<FBSelect
2018-12-05 16:53:42 -07:00
key={JSON.stringify(props.currentSequence)}
2019-12-27 11:37:54 -07:00
list={farmwareList(farmwareData)}
2018-05-18 16:34:12 -06:00
selectedItem={selectedFarmwareDDI(farmwareName)}
onChange={updateStepFarmwareSelection}
2018-02-14 22:07:36 -07:00
allowEmpty={true}
2019-09-11 11:50:48 -06:00
customNullLabel={t("Manual Input")} />
2018-05-18 16:34:12 -06:00
{!isInstalled(farmwareName) &&
2019-02-10 22:10:29 -07:00
<div className="farmware-name-manual-input">
2018-02-14 22:07:36 -07:00
<label>{t("Manual input")}</label>
<StepInputBox dispatch={dispatch}
index={index}
step={currentStep}
sequence={currentSequence}
field="label" />
</div>}
2018-05-18 16:34:12 -06:00
<FarmwareInputs
farmwareName={farmwareName}
farmwareInstalled={isInstalled(farmwareName)}
defaultConfigs={currentFarmwareConfigDefaults(farmwareName)}
currentStep={currentStep}
updateStep={updateStep} />
2017-12-15 16:09:24 -07:00
</Col>
</Row>
</StepContent>
</StepWrapper>;
2017-06-29 12:54:02 -06:00
} else {
return <p> ERROR </p>;
}
}