Farmbot-Web-App/frontend/sequences/step_tiles/mark_as/unpack_step.ts

88 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-09-07 10:15:31 -06:00
import { DropDownItem } from "../../../ui";
2018-09-11 12:50:06 -06:00
import {
findToolById, findPointerByTypeAndId,
2018-09-11 12:50:06 -06:00
} from "../../../resources/selectors";
import { point2ddi } from "./resource_list";
2018-09-11 15:53:27 -06:00
import { MOUNTED_TO } from "./constants";
import { DropDownPair, StepWithResourceIndex } from "./interfaces";
2020-02-18 12:21:09 -07:00
import { t } from "../../../i18next_wrapper";
import { PLANT_STAGE_DDI_LOOKUP } from "../../../farm_designer/plants/edit_plant_status";
2018-09-07 10:15:31 -06:00
2020-02-18 12:21:09 -07:00
export const TOOL_MOUNT = (): DropDownItem => ({
label: t("Tool Mount"), value: "tool_mount"
});
const NOT_IN_USE = (): DropDownItem => ({ label: t("Not Mounted"), value: 0 });
export const DISMOUNTED = (): DropDownPair => ({
leftSide: TOOL_MOUNT(),
rightSide: NOT_IN_USE()
});
const DEFAULT_TOOL_NAME = () => t("Untitled Tool");
const REMOVED_ACTION = () => ({ label: t("Removed"), value: "removed" });
2018-09-07 10:15:31 -06:00
const mountedTo = (toolName = DEFAULT_TOOL_NAME()): DropDownItem =>
({ label: `${MOUNTED_TO()} ${toolName}`, value: "mounted" });
2018-09-07 10:15:31 -06:00
2018-09-11 15:53:27 -06:00
/** The user wants to change the `mounted_tool_id` of their Device. */
function mountTool(i: StepWithResourceIndex): DropDownPair {
2018-09-10 13:23:52 -06:00
const { value } = i.step.args;
2018-09-10 13:41:39 -06:00
if (typeof value === "number" && value > 0) {
try { // Good tool id
const tool = findToolById(i.resourceIndex, value as number);
2020-02-18 12:21:09 -07:00
return { leftSide: TOOL_MOUNT(), rightSide: mountedTo(tool.body.name) };
2018-09-10 13:41:39 -06:00
} catch { // Bad tool ID or app still loading.
2020-02-18 12:21:09 -07:00
return { leftSide: TOOL_MOUNT(), rightSide: mountedTo("an unknown tool") };
2018-09-10 13:41:39 -06:00
}
2018-09-10 13:23:52 -06:00
} else {
2018-09-10 13:41:39 -06:00
// No tool id
2020-02-18 12:21:09 -07:00
return DISMOUNTED();
2018-09-10 13:23:52 -06:00
}
2018-09-07 10:15:31 -06:00
}
2018-09-11 15:53:27 -06:00
/** When we can't properly guess the correct way to to render the screen,
* possibly for legacy reasons or because the user wrote their CeleryScript by
* hand. */
function unknownOption(i: StepWithResourceIndex): DropDownPair {
const { resource_type, resource_id, label, value } = i.step.args;
2018-09-07 10:15:31 -06:00
return {
2018-09-11 15:53:27 -06:00
leftSide: { label: resource_type, value: resource_id },
rightSide: { label: `${label} = ${value}`, value: "" + value }
};
2018-09-07 10:15:31 -06:00
}
2018-09-11 15:53:27 -06:00
/** The user wants to mark a the `discarded_at` attribute of a Point. */
function discardPoint(i: StepWithResourceIndex): DropDownPair {
const { resource_id, resource_type } = i.step.args;
const pointerBody =
findPointerByTypeAndId(i.resourceIndex, resource_type, resource_id).body;
2018-09-07 10:15:31 -06:00
return {
leftSide: point2ddi(pointerBody),
rightSide: REMOVED_ACTION(),
2018-09-07 10:15:31 -06:00
};
}
2018-09-11 15:53:27 -06:00
/** The user wants to mark a the `plant_stage` attribute of a Plant resource. */
function plantStage(i: StepWithResourceIndex): DropDownPair {
const { resource_id, resource_type, value } = i.step.args;
const pointerBody =
findPointerByTypeAndId(i.resourceIndex, resource_type, resource_id).body;
return {
leftSide: point2ddi(pointerBody),
rightSide: PLANT_STAGE_DDI_LOOKUP()["" + value]
|| { label: "" + value, value: "" + value },
};
2018-09-07 10:15:31 -06:00
}
2018-09-11 15:53:27 -06:00
/** We can guess how the "Mark As.." UI will be rendered (left and right side
* drop downs) based on the shape of the current step. There are several
* strategies and this function will dispatch the appropriate one. */
export function unpackStep(i: StepWithResourceIndex): DropDownPair {
2018-09-10 13:23:52 -06:00
const { label } = i.step.args;
2018-09-07 10:15:31 -06:00
switch (label) {
2018-09-10 13:23:52 -06:00
case "mounted_tool_id": return mountTool(i);
2018-09-07 10:15:31 -06:00
case "discarded_at": return discardPoint(i);
case "plant_stage": return plantStage(i);
default: return unknownOption(i);
2018-09-07 10:15:31 -06:00
}
}