Farmbot-Web-App/frontend/farm_designer/plants/grid/plant_grid.tsx

115 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-11-13 14:39:32 -07:00
import React from "react";
import {
EMPTY_PLANT_GRID,
PlantGridKey,
PlantGridProps,
PlantGridState
} from "./constants";
import { initPlantGrid } from "./generate_grid";
import { init } from "../../../api/crud";
import { uuid } from "farmbot";
import { saveGrid, stashGrid } from "./thunks";
2020-02-15 11:29:31 -07:00
import { error, success } from "../../../toast/toast";
2019-11-15 06:45:22 -07:00
import { t } from "../../../i18next_wrapper";
2019-11-18 13:20:46 -07:00
import { GridInput } from "./grid_input";
2019-11-13 14:39:32 -07:00
export class PlantGrid extends React.Component<PlantGridProps, PlantGridState> {
2019-11-15 13:58:44 -07:00
state: PlantGridState = { ...EMPTY_PLANT_GRID, gridId: uuid() };
2019-11-13 14:39:32 -07:00
2020-02-15 11:29:31 -07:00
get plantCount() {
const { numPlantsH, numPlantsV } = this.state.grid;
return numPlantsH * numPlantsV;
}
2019-11-15 07:30:13 -07:00
onchange = (key: PlantGridKey, val: number) => {
const grid = { ...this.state.grid, [key]: val };
this.setState({ grid });
};
2019-11-15 14:55:22 -07:00
confirmUnsaved = () => {
const prompt = t("You have unsaved changes. Would you like to save them?");
const action = confirm(prompt) ?
saveGrid(this.state.gridId) : stashGrid(this.state.gridId);
this.props.dispatch(action);
}
2019-11-15 07:30:13 -07:00
componentWillUnmount() {
2019-11-15 14:55:22 -07:00
(this.state.status === "dirty") && this.confirmUnsaved();
2019-11-15 07:30:13 -07:00
}
2019-11-13 14:39:32 -07:00
performPreview = () => {
2020-02-15 11:29:31 -07:00
if (this.plantCount > 100) {
2019-11-15 06:45:22 -07:00
error(t("Please make a grid with less than 100 plants"));
return;
}
const plants = initPlantGrid({
grid: this.state.grid,
openfarm_slug: this.props.openfarm_slug,
cropName: this.props.cropName,
gridId: this.state.gridId
});
plants.map(p => this.props.dispatch(init("Point", p)));
2019-11-13 14:39:32 -07:00
this.setState({ status: "dirty" });
}
revertPreview = () => {
const p: Promise<{}> = this.props.dispatch(stashGrid(this.state.gridId));
2019-11-19 07:25:03 -07:00
return p.then(() => this.setState({ status: "clean" }));
2019-11-13 14:39:32 -07:00
}
saveGrid = () => {
const p: Promise<{}> = this.props.dispatch(saveGrid(this.state.gridId));
2020-02-15 11:29:31 -07:00
return p.then(() => {
success(t("{{ count }} plants added.", { count: this.plantCount }));
this.setState({ ...EMPTY_PLANT_GRID, gridId: uuid() });
});
2019-11-13 14:39:32 -07:00
}
inputs = () => {
2019-11-15 07:30:13 -07:00
return <GridInput
2019-11-15 12:36:59 -07:00
xy_swap={this.props.xy_swap}
2019-11-15 07:30:13 -07:00
disabled={this.state.status === "dirty"}
grid={this.state.grid}
onChange={this.onchange} />;
2019-11-13 14:39:32 -07:00
}
buttons = () => {
switch (this.state.status) {
case "clean":
2020-02-28 09:34:28 -07:00
return <div className={"preview-grid-button"}>
<a className={"preview-button"}
title={t("Preview")}
onClick={this.performPreview}>
2020-02-15 11:29:31 -07:00
{t("Preview")}
2019-11-15 09:51:23 -07:00
</a>
2019-11-13 14:39:32 -07:00
</div>;
case "dirty":
2020-02-28 09:34:28 -07:00
return <div className={"save-or-cancel-grid-button"}>
<a className={"cancel-button"}
title={t("Cancel")}
onClick={this.revertPreview}>
2020-02-15 11:29:31 -07:00
{t("Cancel")}
</a>
2020-02-28 09:34:28 -07:00
<a className={"save-button"}
title={t("Save")}
onClick={this.saveGrid}>
2020-02-15 11:29:31 -07:00
{t("Save")}
2019-11-15 09:51:23 -07:00
</a>
2019-11-13 14:39:32 -07:00
</div>;
}
}
render() {
2020-02-28 09:34:28 -07:00
return <div className={"grid-and-row-planting"}>
2019-11-18 12:38:23 -07:00
<hr style={{ borderTop: "1.5px solid rgba(255, 255, 255 ,0.7)" }} />
2019-11-15 10:53:10 -07:00
<h3>
{t("Grid and Row Planting")}
</h3>
2019-11-13 14:39:32 -07:00
{this.inputs()}
2019-11-15 09:51:23 -07:00
<br />
2019-11-13 14:39:32 -07:00
{this.buttons()}
</div>;
}
}