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

79 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-08-23 15:18:28 -06:00
import React from "react";
import { connect } from "react-redux";
import {
DesignerPanel, DesignerPanelContent, DesignerPanelHeader
2019-12-10 13:09:52 -07:00
} from "../designer_panel";
2019-08-23 15:18:28 -06:00
import { Everything } from "../../interfaces";
import { t } from "../../i18next_wrapper";
import { getPathArray } from "../../history";
import { TaggedTool, SpecialStatus } from "farmbot";
import { maybeFindToolById } from "../../resources/selectors";
import { SaveBtn } from "../../ui";
2019-12-30 09:08:48 -07:00
import { edit, destroy } from "../../api/crud";
2019-08-23 15:18:28 -06:00
import { history } from "../../history";
2019-10-25 09:33:33 -06:00
import { Panel } from "../panel_header";
2019-08-23 15:18:28 -06:00
export interface EditToolProps {
findTool(id: string): TaggedTool | undefined;
dispatch: Function;
}
export interface EditToolState {
toolName: string;
}
export const mapStateToProps = (props: Everything): EditToolProps => ({
findTool: (id: string) =>
maybeFindToolById(props.resources.index, parseInt(id)),
dispatch: props.dispatch,
});
2019-09-19 13:09:00 -06:00
export class RawEditTool extends React.Component<EditToolProps, EditToolState> {
2019-08-23 15:18:28 -06:00
state: EditToolState = { toolName: this.tool ? this.tool.body.name || "" : "" };
get stringyID() { return getPathArray()[4] || ""; }
get tool() { return this.props.findTool(this.stringyID); }
fallback = () => {
history.push("/app/designer/tools");
return <span>{t("Redirecting...")}</span>;
}
2019-12-30 09:08:48 -07:00
default = (tool: TaggedTool) => {
const { dispatch } = this.props;
const { toolName } = this.state;
const panelName = "edit-tool";
return <DesignerPanel panelName={panelName} panel={Panel.Tools}>
2019-08-23 15:18:28 -06:00
<DesignerPanelHeader
2019-12-30 09:08:48 -07:00
panelName={panelName}
title={t("Edit tool")}
2019-08-23 15:18:28 -06:00
backTo={"/app/designer/tools"}
2019-10-25 09:33:33 -06:00
panel={Panel.Tools} />
2019-12-30 09:08:48 -07:00
<DesignerPanelContent panelName={panelName}>
2020-02-18 12:21:09 -07:00
<label>{t("Name")}</label>
2019-08-23 15:18:28 -06:00
<input
2019-12-30 09:08:48 -07:00
value={toolName}
2019-08-23 15:18:28 -06:00
onChange={e => this.setState({ toolName: e.currentTarget.value })} />
<SaveBtn
onClick={() => {
2019-12-30 09:08:48 -07:00
dispatch(edit(tool, { name: toolName }));
2019-08-23 15:18:28 -06:00
history.push("/app/designer/tools");
}}
status={SpecialStatus.DIRTY} />
2019-12-30 09:08:48 -07:00
<button
className="fb-button red no-float"
onClick={() => dispatch(destroy(tool.uuid))}>
{t("Delete")}
</button>
2019-08-23 15:18:28 -06:00
</DesignerPanelContent>
</DesignerPanel>;
2019-12-30 09:08:48 -07:00
}
2019-08-23 15:18:28 -06:00
render() {
return this.tool ? this.default(this.tool) : this.fallback();
}
}
2019-09-19 13:09:00 -06:00
export const EditTool = connect(mapStateToProps)(RawEditTool);