Farmbot-Web-App/frontend/farm_designer/tools/add_tool_slot.tsx

120 lines
4.1 KiB
TypeScript
Raw Normal View History

2019-12-30 09:08:48 -07:00
import React from "react";
import { connect } from "react-redux";
import {
DesignerPanel, DesignerPanelContent, DesignerPanelHeader
} from "../designer_panel";
import { Everything } from "../../interfaces";
import { t } from "../../i18next_wrapper";
import { SaveBtn } from "../../ui";
2020-02-18 12:21:09 -07:00
import {
SpecialStatus, TaggedTool, TaggedToolSlotPointer, FirmwareHardware
} from "farmbot";
2019-12-30 09:08:48 -07:00
import { init, save, edit, destroy } from "../../api/crud";
import { Panel } from "../panel_header";
import { ToolPulloutDirection } from "farmbot/dist/resources/api_resources";
import {
selectAllTools, maybeFindToolById, maybeGetToolSlot
} from "../../resources/selectors";
import { BotPosition } from "../../devices/interfaces";
import { validBotLocationData } from "../../util";
import { history } from "../../history";
import { SlotEditRows } from "./tool_slot_edit_components";
import { UUID } from "../../resources/interfaces";
2020-02-18 12:21:09 -07:00
import {
isExpressBoard, getFwHardwareValue
} from "../../devices/components/firmware_hardware_support";
import { getFbosConfig } from "../../resources/getters";
2019-12-30 09:08:48 -07:00
export interface AddToolSlotProps {
tools: TaggedTool[];
dispatch: Function;
botPosition: BotPosition;
findTool(id: number): TaggedTool | undefined;
findToolSlot(uuid: UUID | undefined): TaggedToolSlotPointer | undefined;
2020-02-18 12:21:09 -07:00
firmwareHardware: FirmwareHardware | undefined;
2019-12-30 09:08:48 -07:00
}
export interface AddToolSlotState {
uuid: UUID | undefined;
}
export const mapStateToProps = (props: Everything): AddToolSlotProps => ({
tools: selectAllTools(props.resources.index),
dispatch: props.dispatch,
botPosition: validBotLocationData(props.bot.hardware.location_data).position,
findTool: (id: number) => maybeFindToolById(props.resources.index, id),
findToolSlot: (uuid: UUID | undefined) =>
maybeGetToolSlot(props.resources.index, uuid),
2020-02-18 12:21:09 -07:00
firmwareHardware: getFwHardwareValue(getFbosConfig(props.resources.index)),
2019-12-30 09:08:48 -07:00
});
export class RawAddToolSlot
extends React.Component<AddToolSlotProps, AddToolSlotState> {
state: AddToolSlotState = { uuid: undefined };
componentDidMount() {
const action = init("Point", {
pointer_type: "ToolSlot", name: "Tool Slot", radius: 0, meta: {},
x: 0, y: 0, z: 0, tool_id: undefined,
2020-02-18 12:21:09 -07:00
pullout_direction: ToolPulloutDirection.NONE,
gantry_mounted: isExpressBoard(this.props.firmwareHardware) ? true : false,
2019-12-30 09:08:48 -07:00
});
this.setState({ uuid: action.payload.uuid });
this.props.dispatch(action);
}
componentWillUnmount() {
if (this.state.uuid && this.toolSlot
&& this.toolSlot.specialStatus == SpecialStatus.DIRTY) {
2020-02-18 12:21:09 -07:00
confirm(t("Save new slot?"))
2019-12-30 09:08:48 -07:00
? this.props.dispatch(save(this.state.uuid))
: this.props.dispatch(destroy(this.state.uuid, true));
}
}
get toolSlot() {
return this.props.findToolSlot(this.state.uuid);
}
get tool() {
return this.toolSlot ?
this.props.findTool(this.toolSlot.body.tool_id || 0) : undefined;
}
updateSlot = (toolSlot: TaggedToolSlotPointer) =>
(update: Partial<TaggedToolSlotPointer["body"]>) =>
this.props.dispatch(edit(toolSlot, update));
save = () => {
this.state.uuid && this.props.dispatch(save(this.state.uuid));
history.push("/app/designer/tools");
}
render() {
const panelName = "add-tool-slot";
return <DesignerPanel panelName={panelName} panel={Panel.Tools}>
<DesignerPanelHeader
panelName={panelName}
2020-02-18 12:21:09 -07:00
title={isExpressBoard(this.props.firmwareHardware)
? t("Add new slot")
: t("Add new tool slot")}
2019-12-30 09:08:48 -07:00
backTo={"/app/designer/tools"}
panel={Panel.Tools} />
<DesignerPanelContent panelName={panelName}>
{this.toolSlot
? <SlotEditRows
2020-02-18 12:21:09 -07:00
isExpress={isExpressBoard(this.props.firmwareHardware)}
2019-12-30 09:08:48 -07:00
toolSlot={this.toolSlot}
tools={this.props.tools}
tool={this.tool}
botPosition={this.props.botPosition}
updateToolSlot={this.updateSlot(this.toolSlot)} />
: "initializing"}
<SaveBtn onClick={this.save} status={SpecialStatus.DIRTY} />
</DesignerPanelContent>
2020-01-03 13:04:45 -07:00
</DesignerPanel>;
2019-12-30 09:08:48 -07:00
}
}
export const AddToolSlot = connect(mapStateToProps)(RawAddToolSlot);