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

100 lines
3.3 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
import { Everything } from "../../interfaces";
import { connect } from "react-redux";
import { OpenFarmResults } from "./openfarm_search_results";
import { CropCatalogProps } from "../interfaces";
import { OFSearch } from "../util";
2018-11-28 15:29:46 -07:00
import { debounce } from "lodash";
import { Actions, Content } from "../../constants";
import {
2020-02-28 09:35:32 -07:00
EmptyStateWrapper, EmptyStateGraphic,
2018-11-28 15:29:46 -07:00
} from "../../ui/empty_state_wrapper";
import { Spinner } from "../../extras/spinner";
import {
2020-02-28 09:35:32 -07:00
DesignerPanel, DesignerPanelHeader, DesignerPanelContent, DesignerPanelTop,
2019-12-10 13:09:52 -07:00
} from "../designer_panel";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2019-10-25 09:33:33 -06:00
import { Panel } from "../panel_header";
2020-04-13 19:15:11 -06:00
import { SearchField } from "../../ui/search_field";
2017-06-29 12:54:02 -06:00
export function mapStateToProps(props: Everything): CropCatalogProps {
2018-11-28 15:29:46 -07:00
const { cropSearchQuery, cropSearchInProgress, cropSearchResults
} = props.resources.consumers.farm_designer;
2017-06-29 12:54:02 -06:00
return {
openfarmSearch: OFSearch,
2018-11-28 15:29:46 -07:00
cropSearchQuery,
dispatch: props.dispatch,
cropSearchResults,
cropSearchInProgress,
2017-06-29 12:54:02 -06:00
};
}
2019-09-19 13:09:00 -06:00
export class RawCropCatalog extends React.Component<CropCatalogProps, {}> {
2017-07-07 00:49:21 -06:00
2018-11-28 15:29:46 -07:00
debouncedOFSearch = debounce((searchTerm: string) => {
this.props.openfarmSearch(searchTerm)(this.props.dispatch);
2017-09-19 21:32:17 -06:00
}, 500);
2020-04-13 19:15:11 -06:00
handleChange = (value: string) => {
this.props.dispatch({ type: Actions.SEARCH_QUERY_CHANGE, payload: value });
2017-09-19 21:32:17 -06:00
this.debouncedOFSearch(value);
2017-06-29 12:54:02 -06:00
}
2018-11-28 15:29:46 -07:00
get tooShort() {
const termLength = this.props.cropSearchQuery.length;
return !this.props.cropSearchInProgress && termLength > 0 && termLength < 3;
}
get validSearchTerm() {
return this.props.cropSearchQuery.length > 2;
}
get showResultChangeSpinner() {
return this.props.cropSearchInProgress &&
this.props.cropSearchResults.length > 0 &&
this.validSearchTerm;
}
2018-12-03 14:36:43 -07:00
componentDidMount() {
this.props.openfarmSearch(this.props.cropSearchQuery)(this.props.dispatch);
}
2017-06-29 12:54:02 -06:00
render() {
2019-10-25 09:33:33 -06:00
return <DesignerPanel panelName={"crop-catalog"} panel={Panel.Plants}>
<DesignerPanelHeader
panelName={"crop-catalog"}
2019-10-25 09:33:33 -06:00
panel={Panel.Plants}
title={t("Choose a crop")}
backTo={"/app/designer/plants"} />
2019-10-25 09:33:33 -06:00
<DesignerPanelTop panel={Panel.Plants}>
2020-04-13 19:15:11 -06:00
<SearchField
searchTerm={this.props.cropSearchQuery}
placeholder={t("Search OpenFarm...")}
onChange={this.handleChange}
onKeyPress={this.handleChange}
autoFocus={true}
customRightIcon={this.showResultChangeSpinner ?
<Spinner radius={10} strokeWidth={3} /> : undefined} />
</DesignerPanelTop>
<DesignerPanelContent panelName={"crop-catalog"}>
<div className="crop-search-result-wrapper row">
<EmptyStateWrapper
notEmpty={this.validSearchTerm}
graphic={EmptyStateGraphic.crops}
title={this.tooShort
? t("Search term too short")
: t("What do you want to grow?")}
text={Content.ENTER_CROP_SEARCH_TERM}
colorScheme={"plants"}>
<OpenFarmResults
cropSearchResults={this.props.cropSearchResults}
cropSearchInProgress={this.props.cropSearchInProgress} />
</EmptyStateWrapper>
2017-06-29 12:54:02 -06:00
</div>
</DesignerPanelContent>
</DesignerPanel>;
2017-06-29 12:54:02 -06:00
}
}
2019-09-19 13:09:00 -06:00
export const CropCatalog = connect(mapStateToProps)(RawCropCatalog);