Handle select returns scopedeclaration now

pull/1061/head
Rick Carlino 2018-11-29 12:05:41 -06:00
parent 47d924f8a9
commit 90104252b8
5 changed files with 71 additions and 69 deletions

View File

@ -1,7 +0,0 @@
I hope that we can someday not use this folder and use all the @types/* repositories.
Some (legacy) libraries don't work with the newer typings (eg: react-redux).
In those rare circumstances, we can put them in here.
Hopefully, this folder will go away some day.

View File

@ -19,7 +19,7 @@ export const ParentVariableForm =
const { sequence, resources, onChange } = props;
const { x, y, z } = props.parent.location;
const isDisabled = !props.parent.editable;
const list = generateList(resources, [PARENT]);
return <div className="parent-variable-form">
<Row>
<Col xs={12}>
@ -27,9 +27,11 @@ export const ParentVariableForm =
<FBSelect
key={JSON.stringify(sequence)}
allowEmpty={true}
list={generateList(resources, [PARENT])}
list={list}
selectedItem={props.parent.dropdown}
onChange={(ddi) => {
const list2 = generateList(resources, [PARENT]);
console.dir(list2);
console.error("FINISH ME");
handleSelect(props.resources, ddi);
onChange({ x: -23, y: -23, z: -23 });

View File

@ -32,19 +32,6 @@ describe("handleSelect()", () => {
});
});
it("handles 'parameter_declaration's", () => {
const result = handleSelect(fakeResourceIndex(), {
headingId: "parameter",
value: "parent",
label: "1"
});
expect(result.kind).toBe("parameter_declaration");
if (result.kind == "parameter_declaration") {
expect(result.args.label).toBe("parent");
expect(result.args.data_type).toBe("point");
}
});
it("returns location data: identifier", () => {
const location = handleSelect(fakeResourceIndex(),
{

View File

@ -2,50 +2,73 @@
* figures out the corresponding Tool | Coordinate | Point */
import { DropDownItem } from "../../../ui/index";
import { ResourceIndex } from "../../../resources/interfaces";
import { KnownGroupTag, LocationData } from "./interfaces";
import { findPointerByTypeAndId, findToolById } from "../../../resources/selectors";
import { bail } from "../../../util";
import { ParameterDeclaration, Coordinate } from "farmbot";
import { LocationData } from "./interfaces";
import {
ParameterDeclaration,
Coordinate,
ScopeDeclaration,
ScopeDeclarationBodyItem,
VariableDeclaration
} from "farmbot";
export type CeleryVariable = LocationData | ParameterDeclaration;
export const EMPTY_COORD: Coordinate = {
kind: "coordinate",
args: { x: 0, y: 0, z: 0 }
};
export const EMPTY_COORD: Coordinate =
({ kind: "coordinate", args: { x: 0, y: 0, z: 0 } });
/** Takes a DropDownItem and turns it into data suitable
* for MoveAbsolute["args"]["location"] */
export let handleSelect = (index: ResourceIndex, input: DropDownItem): CeleryVariable => {
const tag = input.headingId as (KnownGroupTag | "parameter");
const label = "" + input.value;
const id = parseInt(label);
switch (tag) {
case "ToolSlot":
case "GenericPointer":
case "Plant":
const p = findPointerByTypeAndId(index, tag, id);
if (p && p.body.id) {
return {
kind: "point",
args: { pointer_type: tag, pointer_id: p.body.id }
};
} else {
return bail("Bad point_id: " + JSON.stringify(p));
const toolVar = (value: string | number): VariableDeclaration => ({
kind: "variable_declaration",
args: {
label: "parent",
data_value: {
kind: "tool",
args: {
tool_id: parseInt("" + value)
}
case "Tool":
const tool_id = findToolById(index, id)
.body
.id || bail("No id");
return { kind: "tool", args: { tool_id } };
case "identifier":
return { kind: "identifier", args: { label } };
case "parameter":
// AUTHOR'S NOTE: At the time of writing, the only parameter supported
// is `parent` which is always has a `data_type` of `point`. This will
// need to be updated later.
const data_type = "point";
return { kind: "parameter_declaration", args: { label, data_type } };
default:
return EMPTY_COORD;
}
}
});
const pointVar =
(pointer_type: "Plant" | "GenericPointer", value: string | number): VariableDeclaration => ({
kind: "variable_declaration",
args: {
label: "parent",
data_value: {
kind: "point",
args: { pointer_type, pointer_id: parseInt("" + value) }
}
}
});
const manualEntry: VariableDeclaration = {
kind: "variable_declaration",
args: {
label: "parent",
data_value: { kind: "coordinate", args: { x: 0, y: 0, z: 0 } }
}
};
const parentParameter: ParameterDeclaration = {
kind: "parameter_declaration",
args: { label: "parent", data_type: "point" }
};
const createNewParent =
(_index: ResourceIndex, input: DropDownItem): ScopeDeclarationBodyItem | undefined => {
switch (input.headingId) {
case "Plant":
case "GenericPointer": return pointVar(input.headingId, input.value);
case "Tool": return toolVar(input.value);
case "parameter": return parentParameter; // Caller decides X/Y/Z
case "Other": return manualEntry;
}
return undefined;
};
export let handleSelect =
(index: ResourceIndex, input: DropDownItem): ScopeDeclaration => {
const sd: ScopeDeclaration =
({ kind: "scope_declaration", args: {}, body: [] });
const parent = createNewParent(index, input);
parent && sd.body /** lol */ && sd.body.push(parent);
return sd;
};

View File

@ -1,13 +1,12 @@
import * as React from "react";
import { generateList, PARENT_DDI } from "./generate_list";
import { handleSelect } from "./handle_select";
import { formatSelectedDropdown } from "./format_selected_dropdown";
import { TileMoveAbsProps } from "./interfaces";
import { FBSelect, DropDownItem } from "../../../ui/index";
import { Feature } from "../../../devices/interfaces";
export function TileMoveAbsSelect(props: TileMoveAbsProps) {
const { selectedItem, resources, onChange, shouldDisplay } = props;
const { selectedItem, resources, shouldDisplay } = props;
const i = selectedItem;
const additionalItems: DropDownItem[] =
shouldDisplay(Feature.variables) ? PARENT_DDI : [];
@ -15,9 +14,7 @@ export function TileMoveAbsSelect(props: TileMoveAbsProps) {
allowEmpty={true}
list={generateList(resources, additionalItems)}
selectedItem={formatSelectedDropdown(resources, i)}
onChange={(x: DropDownItem) => {
const y = handleSelect(resources, x);
// This guard is only to please the type checker. -RC 10 Aug 18
(y.kind !== "parameter_declaration") && onChange(y);
onChange={(_x: DropDownItem) => {
console.error("RE-write this!");
}} />;
}