2020-11-30 07:23:05 +00:00
|
|
|
import React, { useState } from "react";
|
2022-06-03 05:07:02 +00:00
|
|
|
import BaseControl, { ControlData, ControlProps } from "./BaseControl";
|
2020-04-15 11:42:11 +00:00
|
|
|
import SearchBox from "react-google-maps/lib/components/places/SearchBox";
|
|
|
|
|
import StandaloneSearchBox from "react-google-maps/lib/components/places/StandaloneSearchBox";
|
2022-01-07 06:08:17 +00:00
|
|
|
import { getAppsmithConfigs } from "@appsmith/configs";
|
2020-11-30 07:23:05 +00:00
|
|
|
import { useScript, ScriptStatus, AddScriptTo } from "utils/hooks/useScript";
|
2021-03-15 12:17:56 +00:00
|
|
|
import { StyledInputGroup } from "./StyledControls";
|
2021-02-24 15:29:12 +00:00
|
|
|
import log from "loglevel";
|
2022-06-03 05:07:02 +00:00
|
|
|
import { isDynamicValue } from "utils/DynamicBindingUtils";
|
2020-08-07 06:22:36 +00:00
|
|
|
|
|
|
|
|
const { google } = getAppsmithConfigs();
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
class LocationSearchControl extends BaseControl<ControlProps> {
|
2020-11-24 08:43:45 +00:00
|
|
|
searchBox: any = null;
|
|
|
|
|
|
2020-11-30 07:23:05 +00:00
|
|
|
clearLocation = () => {
|
|
|
|
|
this.updateProperty(this.props.propertyName, {
|
|
|
|
|
lat: -34.397,
|
|
|
|
|
long: 150.644,
|
|
|
|
|
title: "",
|
|
|
|
|
});
|
2020-11-24 08:43:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onLocationSelection = () => {
|
2021-02-24 15:29:12 +00:00
|
|
|
try {
|
|
|
|
|
// For some places, the length is zero
|
|
|
|
|
const places = this.searchBox.getPlaces();
|
|
|
|
|
const location = places[0].geometry.location;
|
|
|
|
|
const title = places[0].formatted_address;
|
|
|
|
|
const lat = location.lat();
|
|
|
|
|
const long = location.lng();
|
|
|
|
|
const value = { lat, long, title };
|
2022-07-14 05:00:30 +00:00
|
|
|
this.updateProperty(this.props.propertyName, value, true);
|
2021-02-24 15:29:12 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
if (this.searchBox && this.searchBox.getPlaces)
|
|
|
|
|
log.debug("Error selecting location:", this.searchBox.getPlaces());
|
|
|
|
|
else {
|
|
|
|
|
log.debug("Error selecting location - searchBox not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-24 08:43:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onSearchBoxMounted = (ref: SearchBox) => {
|
|
|
|
|
this.searchBox = ref;
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
2020-11-24 08:43:45 +00:00
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
render() {
|
|
|
|
|
return (
|
2020-11-30 07:23:05 +00:00
|
|
|
<MapScriptWrapper
|
2021-04-28 10:28:39 +00:00
|
|
|
clearLocation={this.clearLocation}
|
2020-11-30 07:23:05 +00:00
|
|
|
onPlacesChanged={this.onLocationSelection}
|
2021-04-28 10:28:39 +00:00
|
|
|
onSearchBoxMounted={this.onSearchBoxMounted}
|
2020-11-30 07:23:05 +00:00
|
|
|
propertyValue={this.props.propertyValue}
|
|
|
|
|
/>
|
2020-04-15 11:42:11 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "LOCATION_SEARCH";
|
|
|
|
|
}
|
2022-06-03 05:07:02 +00:00
|
|
|
|
|
|
|
|
static canDisplayValueInUI(config: ControlData, value: any): boolean {
|
|
|
|
|
return !isDynamicValue(value);
|
|
|
|
|
}
|
2020-04-15 11:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
2020-11-30 07:23:05 +00:00
|
|
|
interface MapScriptWrapperProps {
|
|
|
|
|
onSearchBoxMounted: (ref: SearchBox) => void;
|
|
|
|
|
onPlacesChanged: () => void;
|
|
|
|
|
clearLocation: () => void;
|
|
|
|
|
propertyValue: any;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
function MapScriptWrapper(props: MapScriptWrapperProps) {
|
2020-11-30 07:23:05 +00:00
|
|
|
const status = useScript(
|
|
|
|
|
`https://maps.googleapis.com/maps/api/js?key=${google.apiKey}&v=3.exp&libraries=geometry,drawing,places`,
|
|
|
|
|
AddScriptTo.HEAD,
|
|
|
|
|
);
|
|
|
|
|
const [title, setTitle] = useState("");
|
2021-01-19 07:29:15 +00:00
|
|
|
|
2020-11-30 07:23:05 +00:00
|
|
|
return (
|
|
|
|
|
<div data-standalone-searchbox="">
|
|
|
|
|
{status === ScriptStatus.READY && (
|
|
|
|
|
<StandaloneSearchBox
|
|
|
|
|
onPlacesChanged={() => {
|
|
|
|
|
props.onPlacesChanged();
|
|
|
|
|
setTitle("");
|
|
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
ref={props.onSearchBoxMounted}
|
2020-11-30 07:23:05 +00:00
|
|
|
>
|
2021-03-15 12:17:56 +00:00
|
|
|
<StyledInputGroup
|
|
|
|
|
dataType="text"
|
2021-04-28 10:28:39 +00:00
|
|
|
defaultValue={title || props.propertyValue?.title}
|
2021-03-15 12:17:56 +00:00
|
|
|
onChange={(value: string) => {
|
|
|
|
|
if (value === "") {
|
2020-11-30 07:23:05 +00:00
|
|
|
props.clearLocation();
|
|
|
|
|
}
|
2021-03-15 12:17:56 +00:00
|
|
|
setTitle(value);
|
2020-11-30 07:23:05 +00:00
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
placeholder="Enter location"
|
2022-05-25 10:05:53 +00:00
|
|
|
tabIndex={-1}
|
2020-11-30 07:23:05 +00:00
|
|
|
/>
|
|
|
|
|
</StandaloneSearchBox>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2020-11-30 07:23:05 +00:00
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
export default LocationSearchControl;
|