Farmbot-Web-App/frontend/farm_designer/saved_gardens/garden_edit.tsx

124 lines
4.4 KiB
TypeScript
Raw Normal View History

2019-11-06 10:01:05 -07:00
import * as React from "react";
import { GardenViewButtonProps, EditGardenProps } from "./interfaces";
import { openOrCloseGarden, applyGarden, destroySavedGarden } from "./actions";
import { error } from "../../toast/toast";
import { trim } from "../../util";
import { BlurableInput, Row } from "../../ui";
import { edit, save } from "../../api/crud";
import { connect } from "react-redux";
import {
2019-11-27 13:11:45 -07:00
selectAllPlantPointers, maybeFindSavedGardenById
2019-11-06 10:01:05 -07:00
} from "../../resources/selectors";
import { Everything } from "../../interfaces";
import {
DesignerPanel, DesignerPanelHeader, DesignerPanelContent
2019-12-10 13:09:52 -07:00
} from "../designer_panel";
2020-02-28 09:34:28 -07:00
import { history, getPathArray } from "../../history";
2019-11-06 10:01:05 -07:00
import { isNumber } from "lodash";
import { ResourceIndex } from "../../resources/interfaces";
import { t } from "../../i18next_wrapper";
import { Panel } from "../panel_header";
/** Open or close a SavedGarden. */
const GardenViewButton = (props: GardenViewButtonProps) => {
const { dispatch, savedGarden, gardenIsOpen } = props;
const onClick = openOrCloseGarden({ savedGarden, gardenIsOpen, dispatch });
const btnText = gardenIsOpen
? t("exit")
: t("view");
return <button
className={`fb-button ${gardenIsOpen ? "gray" : "yellow"}`}
2020-02-28 09:34:28 -07:00
title={btnText}
2019-11-06 10:01:05 -07:00
onClick={onClick}>
{btnText}
</button>;
};
/** Apply a SavedGarden after checking that the current garden is empty. */
const ApplyGardenButton =
(props: { plantPointerCount: number, gardenId: number, dispatch: Function }) =>
<button
className="fb-button green"
2020-02-28 09:34:28 -07:00
title={t("apply garden")}
2019-11-06 10:01:05 -07:00
onClick={() => props.plantPointerCount > 0
? error(trim(`${t("Please clear current garden first.")}
(${props.plantPointerCount} ${t("plants")})`))
: props.dispatch(applyGarden(props.gardenId))}>
{t("apply")}
</button>;
const DestroyGardenButton =
(props: { dispatch: Function, gardenUuid: string }) =>
<button
className="fb-button red"
2020-02-28 09:34:28 -07:00
title={t("delete garden")}
2019-11-06 10:01:05 -07:00
onClick={() => props.dispatch(destroySavedGarden(props.gardenUuid))}>
{t("delete")}
</button>;
2019-11-27 13:11:45 -07:00
export const findSavedGardenByUrl = (ri: ResourceIndex) => {
2019-11-06 10:01:05 -07:00
const id = getPathArray()[4];
const num = parseInt(id || "NOPE", 10);
if (isNumber(num) && !isNaN(num)) {
2019-11-27 13:11:45 -07:00
return maybeFindSavedGardenById(ri, num);
2019-11-06 10:01:05 -07:00
}
};
export const mapStateToProps = (props: Everything): EditGardenProps => {
const { openedSavedGarden } = props.resources.consumers.farm_designer;
2019-11-27 13:11:45 -07:00
const savedGarden = findSavedGardenByUrl(props.resources.index);
2019-11-06 10:01:05 -07:00
return {
savedGarden,
2020-01-03 13:04:45 -07:00
gardenIsOpen: !!(savedGarden?.uuid === openedSavedGarden),
2019-11-06 10:01:05 -07:00
dispatch: props.dispatch,
plantPointerCount: selectAllPlantPointers(props.resources.index).length,
};
};
export class RawEditGarden extends React.Component<EditGardenProps, {}> {
render() {
const { savedGarden } = this.props;
2020-02-28 09:34:28 -07:00
!savedGarden && history.push("/app/designer/gardens");
2019-11-06 10:01:05 -07:00
return <DesignerPanel panelName={"saved-garden-edit"}
panel={Panel.SavedGardens}>
<DesignerPanelHeader
panelName={"saved-garden"}
panel={Panel.SavedGardens}
2020-02-26 11:10:59 -07:00
title={t("Edit garden")}
2019-11-06 10:01:05 -07:00
backTo={"/app/designer/gardens"} />
<DesignerPanelContent panelName={"saved-garden-edit"}>
{savedGarden
2020-02-28 09:34:28 -07:00
? <div className={"saved-garden-content"}>
2019-11-06 10:01:05 -07:00
<Row>
2019-11-20 12:48:14 -07:00
<label>{t("name")}</label>
2019-11-06 10:01:05 -07:00
<BlurableInput
value={savedGarden.body.name || ""}
onCommit={e => {
this.props.dispatch(edit(savedGarden, {
name: e.currentTarget.value
}));
this.props.dispatch(save(savedGarden.uuid));
}} />
</Row>
<Row>
<ApplyGardenButton
dispatch={this.props.dispatch}
plantPointerCount={this.props.plantPointerCount}
gardenId={savedGarden.body.id || -1} />
<DestroyGardenButton
dispatch={this.props.dispatch}
gardenUuid={savedGarden.uuid} />
<GardenViewButton
dispatch={this.props.dispatch}
savedGarden={savedGarden.uuid}
gardenIsOpen={this.props.gardenIsOpen} />
</Row>
</div>
: <p>{t("Garden not found.")}</p>}
</DesignerPanelContent>
</DesignerPanel>;
}
}
export const EditGarden = connect(mapStateToProps)(RawEditGarden);