Farmbot-Web-App/frontend/farm_designer/plants/plant_info.tsx

68 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
import { connect } from "react-redux";
import { mapStateToProps, formatPlantInfo } from "./map_state_to_props";
import { PlantPanel } from "./plant_panel";
2019-11-22 12:57:22 -07:00
import { unselectPlant } from "../map/actions";
2018-09-13 16:00:14 -06:00
import { TaggedPlant } from "../map/interfaces";
2019-12-10 13:09:52 -07:00
import { DesignerPanel, DesignerPanelHeader } from "../designer_panel";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2019-06-14 16:59:11 -06:00
import { EditPlantInfoProps, PlantOptions } from "../interfaces";
2020-01-03 13:04:45 -07:00
import { isString } from "lodash";
2019-06-14 16:59:11 -06:00
import { history, getPathArray } from "../../history";
import { destroy, edit, save } from "../../api/crud";
2019-06-17 19:43:44 -06:00
import { BooleanSetting } from "../../session_keys";
2019-10-25 09:33:33 -06:00
import { Panel } from "../panel_header";
2017-06-29 12:54:02 -06:00
2019-09-19 13:09:00 -06:00
export class RawPlantInfo extends React.Component<EditPlantInfoProps, {}> {
2019-06-14 16:59:11 -06:00
get templates() { return isString(this.props.openedSavedGarden); }
get stringyID() { return getPathArray()[this.templates ? 5 : 4] || ""; }
get plant() { return this.props.findPlant(this.stringyID); }
2019-06-14 17:00:42 -06:00
get confirmDelete() {
const confirmSetting = this.props.getConfigValue(
2019-06-17 19:43:44 -06:00
BooleanSetting.confirm_plant_deletion);
2020-01-03 13:04:45 -07:00
return confirmSetting ?? true;
2019-06-14 17:00:42 -06:00
}
2019-06-14 16:59:11 -06:00
destroy = (plantUUID: string) => {
2019-06-14 17:00:42 -06:00
this.props.dispatch(destroy(plantUUID, !this.confirmDelete));
2019-06-14 16:59:11 -06:00
}
updatePlant = (plantUUID: string, update: PlantOptions) => {
if (this.plant) {
this.props.dispatch(edit(this.plant, update));
this.props.dispatch(save(plantUUID));
}
}
fallback = () => {
history.push("/app/designer/plants");
return <span>{t("Redirecting...")}</span>;
}
2017-06-29 12:54:02 -06:00
2018-09-13 16:00:14 -06:00
default = (plant_info: TaggedPlant) => {
2017-08-28 05:49:13 -06:00
const info = formatPlantInfo(plant_info);
2019-10-25 09:33:33 -06:00
return <DesignerPanel panelName={"plant-info"} panel={Panel.Plants}>
<DesignerPanelHeader
panelName={"plant-info"}
2019-10-25 09:33:33 -06:00
panel={Panel.Plants}
2019-06-07 18:26:12 -06:00
title={`${t("Edit")} ${info.name}`}
2018-12-05 16:53:56 -07:00
backTo={"/app/designer/plants"}
2019-12-26 13:20:10 -07:00
onBack={unselectPlant(this.props.dispatch)} />
2018-09-13 16:00:14 -06:00
<PlantPanel
info={info}
2019-06-07 18:26:12 -06:00
onDestroy={this.destroy}
updatePlant={this.updatePlant}
2018-09-13 16:00:14 -06:00
dispatch={this.props.dispatch}
2019-06-07 18:26:12 -06:00
timeSettings={this.props.timeSettings}
2018-09-13 16:00:14 -06:00
inSavedGarden={!!this.props.openedSavedGarden} />
</DesignerPanel>;
2017-06-29 12:54:02 -06:00
}
render() {
2017-09-15 23:51:12 -06:00
const plant_info = this.plant;
2017-06-29 12:54:02 -06:00
return plant_info ? this.default(plant_info) : this.fallback();
}
}
2019-09-19 13:09:00 -06:00
export const PlantInfo = connect(mapStateToProps)(RawPlantInfo);