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 { selectAllPlantPointers, maybeFindSavedGardenById } from "../../resources/selectors"; import { Everything } from "../../interfaces"; import { DesignerPanel, DesignerPanelHeader, DesignerPanelContent } from "../designer_panel"; import { history, getPathArray } from "../../history"; 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 ; }; /** Apply a SavedGarden after checking that the current garden is empty. */ const ApplyGardenButton = (props: { plantPointerCount: number, gardenId: number, dispatch: Function }) => ; const DestroyGardenButton = (props: { dispatch: Function, gardenUuid: string }) => ; export const findSavedGardenByUrl = (ri: ResourceIndex) => { const id = getPathArray()[4]; const num = parseInt(id || "NOPE", 10); if (isNumber(num) && !isNaN(num)) { return maybeFindSavedGardenById(ri, num); } }; export const mapStateToProps = (props: Everything): EditGardenProps => { const { openedSavedGarden } = props.resources.consumers.farm_designer; const savedGarden = findSavedGardenByUrl(props.resources.index); return { savedGarden, gardenIsOpen: !!(savedGarden?.uuid === openedSavedGarden), dispatch: props.dispatch, plantPointerCount: selectAllPlantPointers(props.resources.index).length, }; }; export class RawEditGarden extends React.Component { render() { const { savedGarden } = this.props; !savedGarden && history.push("/app/designer/gardens"); return {savedGarden ?
{ this.props.dispatch(edit(savedGarden, { name: e.currentTarget.value })); this.props.dispatch(save(savedGarden.uuid)); }} />
:

{t("Garden not found.")}

}
; } } export const EditGarden = connect(mapStateToProps)(RawEditGarden);