Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Upgrade map widget library to support react 17 #19315

Merged
merged 18 commits into from Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions app/client/package.json
Expand Up @@ -17,10 +17,13 @@
"@draft-js-plugins/mention": "^4.5.1",
"@fusioncharts/powercharts": "^3.16.0",
"@github/g-emoji-element": "^1.1.5",
"@googlemaps/markerclusterer": "^2.0.14",
"@googlemaps/react-wrapper": "^1.1.35",
"@manaflair/redux-batch": "^1.0.0",
"@sentry/react": "^6.2.4",
"@sentry/tracing": "^6.2.4",
"@tinymce/tinymce-react": "^3.13.0",
"@types/google.maps": "^3.51.0",
"@types/react-page-visibility": "^6.4.1",
"@uppy/core": "^1.16.0",
"@uppy/dashboard": "^1.16.0",
Expand Down Expand Up @@ -97,18 +100,17 @@
"rc-select": "^14.1.9",
"rc-tree-select": "^5.4.0",
"re-reselect": "^3.4.0",
"react": "^16.12.0",
"react": "^17.0.2",
"react-beautiful-dnd": "^12.2.0",
"react-custom-scrollbars": "^4.2.1",
"react-device-detect": "^2.2.2",
"react-dnd": "^9.3.4",
"react-dnd-html5-backend": "^9.3.4",
"react-dnd-touch-backend": "^9.4.0",
"react-documents": "^1.0.4",
"react-dom": "^16.7.0",
"react-dom": "^17.0.2",
"react-full-screen": "^1.1.0",
"react-fusioncharts": "^3.1.2",
"react-google-maps": "^9.4.5",
"react-google-recaptcha": "^2.1.0",
"react-helmet": "^5.2.1",
"react-hook-form": "^7.28.0",
Expand Down Expand Up @@ -212,10 +214,10 @@
"@types/node-forge": "^0.10.0",
"@types/papaparse": "^5.3.5",
"@types/prismjs": "^1.16.1",
"@types/react": "^16.8.2",
"@types/react": "^17.0.2",
"@types/react-beautiful-dnd": "^11.0.4",
"@types/react-custom-scrollbars": "^4.0.7",
"@types/react-dom": "^16.8.0",
"@types/react-dom": "^17.0.2",
"@types/react-google-recaptcha": "^2.1.1",
"@types/react-helmet": "^5.0.14",
"@types/react-instantsearch-dom": "^6.3.0",
Expand Down
@@ -1,18 +1,25 @@
import log from "loglevel";
import React, { useState } from "react";
import BaseControl, { ControlData, ControlProps } from "./BaseControl";
import SearchBox from "react-google-maps/lib/components/places/SearchBox";
import StandaloneSearchBox from "react-google-maps/lib/components/places/StandaloneSearchBox";
import { getAppsmithConfigs } from "@appsmith/configs";
import { useScript, ScriptStatus, AddScriptTo } from "utils/hooks/useScript";
import { Wrapper, Status } from "@googlemaps/react-wrapper";

import { StyledInputGroup } from "./StyledControls";
import log from "loglevel";
import { getAppsmithConfigs } from "@appsmith/configs";
import { isDynamicValue } from "utils/DynamicBindingUtils";
import BaseControl, { ControlData, ControlProps } from "./BaseControl";

const { google } = getAppsmithConfigs();

const renderMapStatus = (status: Status) => {
switch (status) {
case Status.LOADING:
return <span>Loading...</span>;
case Status.FAILURE:
return <span>Error in the component</span>;
case Status.SUCCESS:
return <span>Component loaded....</span>;
}
};
class LocationSearchControl extends BaseControl<ControlProps> {
searchBox: any = null;

clearLocation = () => {
this.updateProperty(this.props.propertyName, {
lat: -34.397,
Expand All @@ -21,37 +28,47 @@ class LocationSearchControl extends BaseControl<ControlProps> {
});
};

onLocationSelection = () => {
onLocationSelection = (ref: any) => {
try {
// For some places, the length is zero
const places = this.searchBox.getPlaces();
const places = ref.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 };
this.updateProperty(this.props.propertyName, value, true);
} catch (e) {
if (this.searchBox && this.searchBox.getPlaces)
log.debug("Error selecting location:", this.searchBox.getPlaces());
if (ref && ref.getPlaces)
log.debug("Error selecting location:", ref.getPlaces());
else {
log.debug("Error selecting location - searchBox not found");
}
}
};

onSearchBoxMounted = (ref: SearchBox) => {
this.searchBox = ref;
onSearchBoxMounted = (ref: any) => {
if (window) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check if map object exists?

const searchBox = new window.google.maps.places.SearchBox(ref);
searchBox.addListener("places_changed", () => {
this.onLocationSelection(searchBox);
});
}
};

render() {
return (
<MapScriptWrapper
clearLocation={this.clearLocation}
onPlacesChanged={this.onLocationSelection}
onSearchBoxMounted={this.onSearchBoxMounted}
propertyValue={this.props.propertyValue}
/>
<Wrapper
apiKey={google.apiKey}
libraries={["geometry", "drawing", "places"]}
render={renderMapStatus}
>
<MapScriptWrapper
clearLocation={this.clearLocation}
onSearchBoxMounted={this.onSearchBoxMounted}
propertyValue={this.props.propertyValue}
/>
</Wrapper>
);
}

Expand All @@ -65,43 +82,29 @@ class LocationSearchControl extends BaseControl<ControlProps> {
}

interface MapScriptWrapperProps {
onSearchBoxMounted: (ref: SearchBox) => void;
onPlacesChanged: () => void;
onSearchBoxMounted: (ref: any) => void;
clearLocation: () => void;
propertyValue: any;
}

function MapScriptWrapper(props: MapScriptWrapperProps) {
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("");

return (
<div data-standalone-searchbox="">
{status === ScriptStatus.READY && (
<StandaloneSearchBox
onPlacesChanged={() => {
props.onPlacesChanged();
setTitle("");
}}
ref={props.onSearchBoxMounted}
>
<StyledInputGroup
dataType="text"
defaultValue={title || props.propertyValue?.title}
onChange={(value: string) => {
if (value === "") {
props.clearLocation();
}
setTitle(value);
}}
placeholder="Enter location"
tabIndex={-1}
/>
</StandaloneSearchBox>
)}
<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}
/>
</div>
);
}
Expand Down
88 changes: 88 additions & 0 deletions app/client/src/widgets/MapWidget/component/Clusterer.tsx
@@ -0,0 +1,88 @@
import React, { useEffect, useState } from "react";
keyurparalkar marked this conversation as resolved.
Show resolved Hide resolved
import { MarkerClusterer } from "@googlemaps/markerclusterer";

import Marker from "./Marker";
import { MapComponentProps } from ".";

type ClustererProps = {
map?: google.maps.Map;
} & Pick<
MapComponentProps,
| "selectMarker"
| "updateCenter"
| "updateMarker"
| "markers"
| "selectedMarker"
| "clickedMarkerCentered"
>;

const Clusterer: React.FC<ClustererProps> = (props) => {
const {
clickedMarkerCentered,
map,
markers,
selectedMarker,
selectMarker,
updateCenter,
updateMarker,
} = props;
const [markerClusterer, setMarkerClusterer] = useState<MarkerClusterer>();

// add marker clusterer on map
useEffect(() => {
if (!map) return;

setMarkerClusterer(
new MarkerClusterer({
map,
}),
);

return () => {
if (markerClusterer) {
markerClusterer.clearMarkers();
}
};
}, [map]);

if (!Array.isArray(markers)) return null;

return (
<>
{markers.map((marker, index) => (
<Marker
clickable
draggable={
selectedMarker &&
selectedMarker?.lat === marker.lat &&
selectedMarker?.long === marker.long
}
key={index}
map={map}
markerClusterer={markerClusterer}
onClick={() => {
if (clickedMarkerCentered) {
updateCenter(marker.lat, marker.long);
}

selectMarker(marker.lat, marker.long, marker.title);
}}
onDragEnd={(e) => {
updateMarker(
Number(e.latLng?.lat()),
Number(e.latLng?.lng()),
index,
);
}}
position={{
lat: marker.lat,
lng: marker.long,
}}
title={marker.title}
/>
))}
</>
);
};

export default Clusterer;