2018-09-13 16:00:14 -06:00
|
|
|
import axios from "axios";
|
|
|
|
import { API } from "../../api";
|
2019-06-24 15:39:49 -06:00
|
|
|
import { success, info } from "../../toast/toast";
|
2018-09-13 16:00:14 -06:00
|
|
|
import { history } from "../../history";
|
|
|
|
import { Actions } from "../../constants";
|
2018-11-14 17:36:52 -07:00
|
|
|
import { destroy, initSave, initSaveGetId } from "../../api/crud";
|
2018-09-13 16:00:14 -06:00
|
|
|
import { unpackUUID } from "../../util";
|
|
|
|
import { isString } from "lodash";
|
2018-11-14 17:36:52 -07:00
|
|
|
import { TaggedSavedGarden, TaggedPlantTemplate } from "farmbot";
|
2019-04-02 13:59:37 -06:00
|
|
|
import { t } from "../../i18next_wrapper";
|
2019-06-06 17:54:48 -06:00
|
|
|
import { stopTracking } from "../../connectivity/data_consistency";
|
2018-09-13 16:00:14 -06:00
|
|
|
|
2018-11-14 17:36:52 -07:00
|
|
|
/** Save all Plant to PlantTemplates in a new SavedGarden. */
|
2020-02-28 09:34:28 -07:00
|
|
|
export const snapshotGarden = (gardenName?: string | undefined) =>
|
|
|
|
axios.post<void>(API.current.snapshotPath, gardenName
|
|
|
|
? { name: gardenName } : {})
|
2019-11-06 10:01:05 -07:00
|
|
|
.then(() => {
|
|
|
|
success(t("Garden Saved."));
|
|
|
|
history.push("/app/designer/gardens");
|
|
|
|
});
|
2018-09-13 16:00:14 -06:00
|
|
|
|
2018-11-14 17:36:52 -07:00
|
|
|
export const unselectSavedGarden = {
|
2018-09-13 16:00:14 -06:00
|
|
|
type: Actions.CHOOSE_SAVED_GARDEN,
|
|
|
|
payload: undefined
|
|
|
|
};
|
|
|
|
|
2018-11-14 17:36:52 -07:00
|
|
|
/** Save a SavedGarden's PlantTemplates as Plants. */
|
2018-09-13 16:00:14 -06:00
|
|
|
export const applyGarden = (gardenId: number) => (dispatch: Function) => axios
|
|
|
|
.patch<void>(API.current.applyGardenPath(gardenId))
|
2019-06-06 17:54:48 -06:00
|
|
|
.then(data => {
|
|
|
|
stopTracking(data.headers["x-farmbot-rpc-id"]);
|
2018-09-13 16:00:14 -06:00
|
|
|
history.push("/app/designer/plants");
|
|
|
|
dispatch(unselectSavedGarden);
|
2018-11-29 12:54:53 -07:00
|
|
|
const busyToastTitle = t("Please wait");
|
|
|
|
info(t("while your garden is applied."), busyToastTitle, "blue");
|
2018-09-13 16:00:14 -06:00
|
|
|
});
|
|
|
|
|
2018-11-01 11:56:41 -06:00
|
|
|
export const destroySavedGarden = (uuid: string) => (dispatch: Function) => {
|
2019-11-06 10:01:05 -07:00
|
|
|
dispatch(unselectSavedGarden);
|
|
|
|
history.push("/app/designer/gardens");
|
|
|
|
dispatch(destroy(uuid));
|
2018-11-01 11:56:41 -06:00
|
|
|
};
|
2018-09-13 16:00:14 -06:00
|
|
|
|
|
|
|
export const closeSavedGarden = () => {
|
2019-11-06 10:01:05 -07:00
|
|
|
history.push("/app/designer/gardens");
|
2018-09-13 16:00:14 -06:00
|
|
|
return (dispatch: Function) =>
|
|
|
|
dispatch(unselectSavedGarden);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const openSavedGarden = (savedGarden: string) => {
|
2019-11-06 10:01:05 -07:00
|
|
|
history.push("/app/designer/gardens/" + unpackUUID(savedGarden).remoteId);
|
2018-09-13 16:00:14 -06:00
|
|
|
return (dispatch: Function) =>
|
|
|
|
dispatch({ type: Actions.CHOOSE_SAVED_GARDEN, payload: savedGarden });
|
|
|
|
};
|
|
|
|
|
2018-11-14 17:36:52 -07:00
|
|
|
/** Open a SavedGarden if it is closed, otherwise close it. */
|
2018-09-13 16:00:14 -06:00
|
|
|
export const openOrCloseGarden = (props: {
|
|
|
|
savedGarden: string | undefined,
|
|
|
|
gardenIsOpen: boolean,
|
|
|
|
dispatch: Function
|
|
|
|
}) => () =>
|
|
|
|
!props.gardenIsOpen && isString(props.savedGarden)
|
|
|
|
? props.dispatch(openSavedGarden(props.savedGarden))
|
|
|
|
: props.dispatch(closeSavedGarden());
|
2018-11-01 11:56:41 -06:00
|
|
|
|
2018-11-14 17:36:52 -07:00
|
|
|
/** Create a new SavedGarden with the chosen name. */
|
2020-02-28 09:34:28 -07:00
|
|
|
export const newSavedGarden = (gardenName: string) =>
|
2018-11-01 11:56:41 -06:00
|
|
|
(dispatch: Function) => {
|
2020-02-28 09:34:28 -07:00
|
|
|
dispatch(initSave("SavedGarden", { name: gardenName || "Untitled Garden" }))
|
2019-11-06 10:01:05 -07:00
|
|
|
.then(() => {
|
|
|
|
success(t("Garden Saved."));
|
|
|
|
history.push("/app/designer/gardens");
|
|
|
|
});
|
2018-11-01 11:56:41 -06:00
|
|
|
};
|
2018-11-14 17:36:52 -07:00
|
|
|
|
|
|
|
/** Create a copy of a PlantTemplate body and assign it a new SavedGarden. */
|
|
|
|
const newPTBody =
|
|
|
|
(source: TaggedPlantTemplate, newSGId: number): TaggedPlantTemplate["body"] =>
|
|
|
|
({
|
|
|
|
name: source.body.name,
|
|
|
|
openfarm_slug: source.body.openfarm_slug,
|
|
|
|
saved_garden_id: newSGId,
|
|
|
|
radius: source.body.radius,
|
|
|
|
x: source.body.x,
|
|
|
|
y: source.body.y,
|
|
|
|
z: source.body.z,
|
|
|
|
});
|
|
|
|
|
|
|
|
/** Copy a SavedGarden and all of its PlantTemplates. */
|
|
|
|
export const copySavedGarden = ({ newSGName, savedGarden, plantTemplates }: {
|
|
|
|
newSGName: string,
|
|
|
|
savedGarden: TaggedSavedGarden,
|
|
|
|
plantTemplates: TaggedPlantTemplate[]
|
|
|
|
}) =>
|
|
|
|
(dispatch: Function) => {
|
|
|
|
const sourceSavedGardenId = savedGarden.body.id;
|
2020-02-28 09:34:28 -07:00
|
|
|
const gardenName = newSGName || `${savedGarden.body.name} (${t("copy")})`;
|
|
|
|
dispatch(initSaveGetId(savedGarden.kind, { name: gardenName }))
|
2019-11-27 13:11:45 -07:00
|
|
|
.then((newSGId: number) => {
|
|
|
|
plantTemplates
|
|
|
|
.filter(x => x.body.saved_garden_id === sourceSavedGardenId)
|
|
|
|
.map(x => dispatch(initSave(x.kind, newPTBody(x, newSGId))));
|
|
|
|
success(t("Garden Saved."));
|
|
|
|
history.push("/app/designer/gardens");
|
|
|
|
});
|
2018-11-14 17:36:52 -07:00
|
|
|
};
|