2023-01-13 18:49:21 +00:00
|
|
|
import log from "loglevel";
|
2020-11-30 07:23:05 +00:00
|
|
|
import React, { useState } from "react";
|
2023-01-13 18:49:21 +00:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { Wrapper, Status } from "@googlemaps/react-wrapper";
|
|
|
|
|
|
2021-03-15 12:17:56 +00:00
|
|
|
import { StyledInputGroup } from "./StyledControls";
|
2022-06-03 05:07:02 +00:00
|
|
|
import { isDynamicValue } from "utils/DynamicBindingUtils";
|
2023-01-13 18:49:21 +00:00
|
|
|
import BaseControl, { ControlData, ControlProps } from "./BaseControl";
|
2020-08-07 06:22:36 +00:00
|
|
|
|
2023-01-13 18:49:21 +00:00
|
|
|
const MapStatusText = styled.span`
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
`;
|
2020-11-24 08:43:45 +00:00
|
|
|
|
2023-01-13 18:49:21 +00:00
|
|
|
const renderMapStatus = (status: Status) => {
|
|
|
|
|
switch (status) {
|
|
|
|
|
case Status.LOADING:
|
|
|
|
|
return <MapStatusText>Loading...</MapStatusText>;
|
|
|
|
|
case Status.FAILURE:
|
|
|
|
|
return <MapStatusText>Error in the component</MapStatusText>;
|
|
|
|
|
case Status.SUCCESS:
|
|
|
|
|
return <MapStatusText>Component loaded....</MapStatusText>;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
class LocationSearchControl extends BaseControl<ControlProps> {
|
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
|
|
|
};
|
|
|
|
|
|
2023-01-13 18:49:21 +00:00
|
|
|
onLocationSelection = (ref: any) => {
|
2021-02-24 15:29:12 +00:00
|
|
|
try {
|
|
|
|
|
// For some places, the length is zero
|
2023-01-13 18:49:21 +00:00
|
|
|
const places = ref.getPlaces();
|
2021-02-24 15:29:12 +00:00
|
|
|
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) {
|
2023-01-13 18:49:21 +00:00
|
|
|
if (ref && ref.getPlaces)
|
|
|
|
|
log.debug("Error selecting location:", ref.getPlaces());
|
2021-02-24 15:29:12 +00:00
|
|
|
else {
|
|
|
|
|
log.debug("Error selecting location - searchBox not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-24 08:43:45 +00:00
|
|
|
};
|
|
|
|
|
|
2023-01-13 18:49:21 +00:00
|
|
|
onSearchBoxMounted = (ref: any) => {
|
|
|
|
|
if (window) {
|
|
|
|
|
const searchBox = new window.google.maps.places.SearchBox(ref);
|
|
|
|
|
searchBox.addListener("places_changed", () => {
|
|
|
|
|
this.onLocationSelection(searchBox);
|
|
|
|
|
});
|
|
|
|
|
}
|
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 (
|
2023-01-13 18:49:21 +00:00
|
|
|
<Wrapper
|
2023-02-07 09:23:15 +00:00
|
|
|
apiKey={this.props.widgetProperties.googleMapsApiKey}
|
2023-01-13 18:49:21 +00:00
|
|
|
libraries={["geometry", "drawing", "places"]}
|
|
|
|
|
render={renderMapStatus}
|
|
|
|
|
>
|
|
|
|
|
<MapScriptWrapper
|
|
|
|
|
clearLocation={this.clearLocation}
|
|
|
|
|
onSearchBoxMounted={this.onSearchBoxMounted}
|
|
|
|
|
propertyValue={this.props.propertyValue}
|
|
|
|
|
/>
|
|
|
|
|
</Wrapper>
|
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 {
|
2023-01-13 18:49:21 +00:00
|
|
|
onSearchBoxMounted: (ref: any) => void;
|
2020-11-30 07:23:05 +00:00
|
|
|
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 [title, setTitle] = useState("");
|
2021-01-19 07:29:15 +00:00
|
|
|
|
2020-11-30 07:23:05 +00:00
|
|
|
return (
|
|
|
|
|
<div data-standalone-searchbox="">
|
2023-01-13 18:49:21 +00:00
|
|
|
<StyledInputGroup
|
|
|
|
|
dataType="text"
|
|
|
|
|
defaultValue={title || props.propertyValue?.title}
|
|
|
|
|
onChange={(value: string) => {
|
|
|
|
|
if (value === "") {
|
|
|
|
|
props.clearLocation();
|
|
|
|
|
}
|
|
|
|
|
setTitle(value);
|
|
|
|
|
}}
|
|
|
|
|
placeholder="Enter location"
|
|
|
|
|
ref={props.onSearchBoxMounted}
|
|
|
|
|
tabIndex={-1}
|
|
|
|
|
/>
|
2020-11-30 07:23:05 +00:00
|
|
|
</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;
|