authscape 1.0.138 → 1.0.140
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +265 -6
- package/package.json +4 -2
- package/src/components/GoogleMapsAutoComplete.js +321 -1
package/index.js
CHANGED
|
@@ -721,10 +721,14 @@ exports["default"] = GooglePlacesModal;
|
|
|
721
721
|
var _react = _interopRequireWildcard(require("react"));
|
|
722
722
|
var _Button = _interopRequireDefault(require("@mui/material/Button"));
|
|
723
723
|
var _system = require("@mui/system");
|
|
724
|
-
var _PlacesAutocomplete = _interopRequireDefault(require("../modals/PlacesAutocomplete"));
|
|
725
724
|
var _TextField = _interopRequireDefault(require("@mui/material/TextField"));
|
|
726
725
|
var _Grid = _interopRequireDefault(require("@mui/material/Grid"));
|
|
727
726
|
var _script = _interopRequireDefault(require("next/script"));
|
|
727
|
+
var _material = require("@mui/material");
|
|
728
|
+
var _ListItemIcon = _interopRequireDefault(require("@mui/material/ListItemIcon"));
|
|
729
|
+
var _LocationOnRounded = _interopRequireDefault(require("@mui/icons-material/LocationOnRounded"));
|
|
730
|
+
var _ListItemText = _interopRequireDefault(require("@mui/material/ListItemText"));
|
|
731
|
+
var _reactCoolOnclickoutside = _interopRequireDefault(require("react-cool-onclickoutside"));
|
|
728
732
|
var _usePlacesAutocomplete = _interopRequireWildcard(require("use-places-autocomplete"));
|
|
729
733
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
730
734
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
@@ -735,10 +739,265 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
|
|
|
735
739
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
736
740
|
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
737
741
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
738
|
-
function
|
|
739
|
-
var
|
|
740
|
-
|
|
741
|
-
|
|
742
|
+
var AutoCompleteDialog = function AutoCompleteDialog(_ref) {
|
|
743
|
+
var placeholder = _ref.placeholder,
|
|
744
|
+
onSelected = _ref.onSelected,
|
|
745
|
+
defaultValue = _ref.defaultValue,
|
|
746
|
+
onChange = _ref.onChange;
|
|
747
|
+
var txtAddressField = (0, _react.useRef)(null);
|
|
748
|
+
var dismissSuggestions = function dismissSuggestions() {
|
|
749
|
+
clearSuggestions();
|
|
750
|
+
};
|
|
751
|
+
var ref = (0, _reactCoolOnclickoutside["default"])(dismissSuggestions);
|
|
752
|
+
var _usePlacesAutocomplet = (0, _usePlacesAutocomplete["default"])({
|
|
753
|
+
requestOptions: {
|
|
754
|
+
/* Define search scope here */
|
|
755
|
+
},
|
|
756
|
+
debounce: 1000
|
|
757
|
+
}),
|
|
758
|
+
ready = _usePlacesAutocomplet.ready,
|
|
759
|
+
value = _usePlacesAutocomplet.value,
|
|
760
|
+
_usePlacesAutocomplet2 = _usePlacesAutocomplet.suggestions,
|
|
761
|
+
status = _usePlacesAutocomplet2.status,
|
|
762
|
+
data = _usePlacesAutocomplet2.data,
|
|
763
|
+
setValue = _usePlacesAutocomplet.setValue,
|
|
764
|
+
clearSuggestions = _usePlacesAutocomplet.clearSuggestions;
|
|
765
|
+
var handleInput = function handleInput(e) {
|
|
766
|
+
e.stopPropagation();
|
|
767
|
+
|
|
768
|
+
// Update the keyword of the input element
|
|
769
|
+
setValue(e.target.value);
|
|
770
|
+
onChange(e.target.value);
|
|
771
|
+
};
|
|
772
|
+
(0, _react.useEffect)(function () {
|
|
773
|
+
setValue(defaultValue); // test this!!!!
|
|
774
|
+
}, [defaultValue]);
|
|
775
|
+
var determineComponentType = function determineComponentType(component) {
|
|
776
|
+
var returnVal = {};
|
|
777
|
+
for (var index = 0; index < component.types.length; index++) {
|
|
778
|
+
var componentType = component.types[index];
|
|
779
|
+
switch (componentType) {
|
|
780
|
+
case "street_number":
|
|
781
|
+
returnVal = {
|
|
782
|
+
componentType: "Street_number",
|
|
783
|
+
long_name: component.long_name,
|
|
784
|
+
short_name: component.short_name
|
|
785
|
+
};
|
|
786
|
+
break;
|
|
787
|
+
case "route":
|
|
788
|
+
returnVal = {
|
|
789
|
+
componentType: "Route",
|
|
790
|
+
long_name: component.long_name,
|
|
791
|
+
short_name: component.short_name
|
|
792
|
+
};
|
|
793
|
+
break;
|
|
794
|
+
case "locality":
|
|
795
|
+
returnVal = {
|
|
796
|
+
componentType: "City",
|
|
797
|
+
long_name: component.long_name,
|
|
798
|
+
short_name: component.short_name
|
|
799
|
+
};
|
|
800
|
+
break;
|
|
801
|
+
case "administrative_area_level_2":
|
|
802
|
+
returnVal = {
|
|
803
|
+
componentType: "County",
|
|
804
|
+
long_name: component.long_name,
|
|
805
|
+
short_name: component.short_name
|
|
806
|
+
};
|
|
807
|
+
break;
|
|
808
|
+
case "administrative_area_level_1":
|
|
809
|
+
returnVal = {
|
|
810
|
+
componentType: "State",
|
|
811
|
+
long_name: component.long_name,
|
|
812
|
+
short_name: component.short_name
|
|
813
|
+
};
|
|
814
|
+
break;
|
|
815
|
+
case "country":
|
|
816
|
+
returnVal = {
|
|
817
|
+
componentType: "country",
|
|
818
|
+
long_name: component.long_name,
|
|
819
|
+
short_name: component.short_name
|
|
820
|
+
};
|
|
821
|
+
break;
|
|
822
|
+
case "postal_code":
|
|
823
|
+
returnVal = {
|
|
824
|
+
componentType: "postal_code",
|
|
825
|
+
long_name: component.long_name,
|
|
826
|
+
short_name: component.short_name
|
|
827
|
+
};
|
|
828
|
+
break;
|
|
829
|
+
case "postal_code_suffix":
|
|
830
|
+
returnVal = {
|
|
831
|
+
componentType: "postal_code_suffix",
|
|
832
|
+
long_name: component.long_name,
|
|
833
|
+
short_name: component.short_name
|
|
834
|
+
};
|
|
835
|
+
break;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return returnVal;
|
|
839
|
+
};
|
|
840
|
+
var getSimplifyResult = function getSimplifyResult(result) {
|
|
841
|
+
var street_number = "";
|
|
842
|
+
var route = "";
|
|
843
|
+
var city = "";
|
|
844
|
+
var county = "";
|
|
845
|
+
var state = "";
|
|
846
|
+
var country = "";
|
|
847
|
+
var postal_code = "";
|
|
848
|
+
var postal_code_suffix = "";
|
|
849
|
+
var lat = null;
|
|
850
|
+
var _long = null;
|
|
851
|
+
var mapUrl = "";
|
|
852
|
+
var placeId = null;
|
|
853
|
+
for (var index = 0; index < result.address_components.length; index++) {
|
|
854
|
+
var component = result.address_components[index];
|
|
855
|
+
var response = determineComponentType(component);
|
|
856
|
+
if (response.componentType == "Street_number") {
|
|
857
|
+
street_number = response.long_name;
|
|
858
|
+
} else if (response.componentType == "Route") {
|
|
859
|
+
route = response.long_name;
|
|
860
|
+
} else if (response.componentType == "City") {
|
|
861
|
+
city = response.long_name;
|
|
862
|
+
} else if (response.componentType == "County") {
|
|
863
|
+
county = response.long_name;
|
|
864
|
+
} else if (response.componentType == "State") {
|
|
865
|
+
state = response.short_name;
|
|
866
|
+
} else if (response.componentType == "country") {
|
|
867
|
+
country = response.long_name;
|
|
868
|
+
} else if (response.componentType == "postal_code") {
|
|
869
|
+
postal_code = response.long_name;
|
|
870
|
+
} else if (response.componentType == "postal_code_suffix") {
|
|
871
|
+
postal_code_suffix = response.long_name;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
if (result.geometry != null && result.geometry.location != null) {
|
|
875
|
+
lat = result.geometry.location.lat;
|
|
876
|
+
_long = result.geometry.location.lng;
|
|
877
|
+
}
|
|
878
|
+
mapUrl = result.url;
|
|
879
|
+
placeId = result.place_id;
|
|
880
|
+
return {
|
|
881
|
+
address: street_number + " " + route,
|
|
882
|
+
city: city,
|
|
883
|
+
state: state,
|
|
884
|
+
county: county,
|
|
885
|
+
postalCode: postal_code,
|
|
886
|
+
country: country,
|
|
887
|
+
lat: lat,
|
|
888
|
+
"long": _long,
|
|
889
|
+
mapUrl: mapUrl,
|
|
890
|
+
placeId: placeId
|
|
891
|
+
};
|
|
892
|
+
};
|
|
893
|
+
var handleSelect = function handleSelect(_ref2) {
|
|
894
|
+
var description = _ref2.description,
|
|
895
|
+
place_id = _ref2.place_id;
|
|
896
|
+
return function () {
|
|
897
|
+
var parameter = {
|
|
898
|
+
// Use the "place_id" of suggestion from the dropdown (object), here just taking first suggestion for brevity
|
|
899
|
+
placeId: place_id
|
|
900
|
+
// Specify the return data that you want (optional)
|
|
901
|
+
//fields: ["name", "rating"],
|
|
902
|
+
};
|
|
903
|
+
|
|
904
|
+
clearSuggestions();
|
|
905
|
+
(0, _usePlacesAutocomplete.getDetails)(parameter).then(function (result) {
|
|
906
|
+
var response = getSimplifyResult(result);
|
|
907
|
+
if (response != null) {
|
|
908
|
+
onSelected(response);
|
|
909
|
+
setValue(response.address, false);
|
|
910
|
+
}
|
|
911
|
+
})["catch"](function (error) {
|
|
912
|
+
alert(error);
|
|
913
|
+
});
|
|
914
|
+
|
|
915
|
+
// When user selects a place, we can replace the keyword without request data from API
|
|
916
|
+
// by setting the second parameter to "false"
|
|
917
|
+
//setValue(description, false);
|
|
918
|
+
// clearSuggestions();
|
|
919
|
+
|
|
920
|
+
// var parseUSAddress = parser.parseLocation(description);
|
|
921
|
+
|
|
922
|
+
// // Get latitude and longitude via utility functions
|
|
923
|
+
// getGeocode({ address: description }).then((results) => {
|
|
924
|
+
// const { lat, lng } = getLatLng(results[0]);
|
|
925
|
+
// //console.log("📍 Coordinates: ", { lat, lng });
|
|
926
|
+
|
|
927
|
+
// const zipCode = getZipCode(results[0], false);
|
|
928
|
+
|
|
929
|
+
// let address = "";
|
|
930
|
+
// if (parseUSAddress.number != null)
|
|
931
|
+
// {
|
|
932
|
+
// address += parseUSAddress.number + " ";
|
|
933
|
+
// }
|
|
934
|
+
|
|
935
|
+
// if (parseUSAddress.street != null)
|
|
936
|
+
// {
|
|
937
|
+
// address += parseUSAddress.street + " ";
|
|
938
|
+
// }
|
|
939
|
+
|
|
940
|
+
// if (parseUSAddress.type != null)
|
|
941
|
+
// {
|
|
942
|
+
// address += parseUSAddress.type;
|
|
943
|
+
// }
|
|
944
|
+
|
|
945
|
+
// onSelected({
|
|
946
|
+
// address: address,
|
|
947
|
+
// city: parseUSAddress.city,
|
|
948
|
+
// state: parseUSAddress.state,
|
|
949
|
+
// zip: zipCode,
|
|
950
|
+
// lat: lat,
|
|
951
|
+
// lng: lng
|
|
952
|
+
// });
|
|
953
|
+
|
|
954
|
+
// setValue(address, false);
|
|
955
|
+
|
|
956
|
+
// });
|
|
957
|
+
};
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
var renderSuggestions = function renderSuggestions() {
|
|
961
|
+
return data.map(function (suggestion) {
|
|
962
|
+
var place_id = suggestion.place_id,
|
|
963
|
+
_suggestion$structure = suggestion.structured_formatting,
|
|
964
|
+
main_text = _suggestion$structure.main_text,
|
|
965
|
+
secondary_text = _suggestion$structure.secondary_text;
|
|
966
|
+
return /*#__PURE__*/_react["default"].createElement(_material.MenuItem, {
|
|
967
|
+
key: place_id,
|
|
968
|
+
onClick: handleSelect(suggestion, place_id),
|
|
969
|
+
sx: {
|
|
970
|
+
paddingRight: 2
|
|
971
|
+
}
|
|
972
|
+
}, /*#__PURE__*/_react["default"].createElement(_ListItemIcon["default"], null, /*#__PURE__*/_react["default"].createElement(_LocationOnRounded["default"], {
|
|
973
|
+
fontSize: "small"
|
|
974
|
+
})), /*#__PURE__*/_react["default"].createElement(_ListItemText["default"], null, /*#__PURE__*/_react["default"].createElement("strong", null, main_text), " ", /*#__PURE__*/_react["default"].createElement("small", null, secondary_text)));
|
|
975
|
+
});
|
|
976
|
+
};
|
|
977
|
+
return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
|
|
978
|
+
ref: txtAddressField,
|
|
979
|
+
autoFocus: true,
|
|
980
|
+
autoComplete: "new-password",
|
|
981
|
+
label: placeholder,
|
|
982
|
+
variant: "outlined",
|
|
983
|
+
fullWidth: true,
|
|
984
|
+
value: value,
|
|
985
|
+
onChange: handleInput
|
|
986
|
+
}), status == "OK" && /*#__PURE__*/_react["default"].createElement(_material.MenuList, {
|
|
987
|
+
ref: ref,
|
|
988
|
+
sx: {
|
|
989
|
+
position: "absolute",
|
|
990
|
+
zIndex: 9999,
|
|
991
|
+
top: 125,
|
|
992
|
+
backgroundColor: "white",
|
|
993
|
+
border: "1px solid black"
|
|
994
|
+
}
|
|
995
|
+
}, renderSuggestions()));
|
|
996
|
+
};
|
|
997
|
+
function GooglePlacesModal(_ref3) {
|
|
998
|
+
var onSave = _ref3.onSave,
|
|
999
|
+
_ref3$createButtonTex = _ref3.createButtonText,
|
|
1000
|
+
createButtonText = _ref3$createButtonTex === void 0 ? "Save" : _ref3$createButtonTex;
|
|
742
1001
|
var _useState = (0, _react.useState)(""),
|
|
743
1002
|
_useState2 = _slicedToArray(_useState, 2),
|
|
744
1003
|
address = _useState2[0],
|
|
@@ -763,7 +1022,7 @@ function GooglePlacesModal(_ref) {
|
|
|
763
1022
|
_useState12 = _slicedToArray(_useState11, 2),
|
|
764
1023
|
lng = _useState12[0],
|
|
765
1024
|
setLng = _useState12[1];
|
|
766
|
-
return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_system.Box, null, /*#__PURE__*/_react["default"].createElement(
|
|
1025
|
+
return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_system.Box, null, /*#__PURE__*/_react["default"].createElement(AutoCompleteDialog, {
|
|
767
1026
|
placeholder: "Address",
|
|
768
1027
|
defaultValue: address,
|
|
769
1028
|
onSelected: function onSelected(data) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "authscape",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.140",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -38,6 +38,8 @@
|
|
|
38
38
|
"next": "^13.3.2",
|
|
39
39
|
"nookies": "^2.5.2",
|
|
40
40
|
"query-string": "^7.1.1",
|
|
41
|
-
"react-
|
|
41
|
+
"react-cool-onclickoutside": "^1.7.0",
|
|
42
|
+
"react-data-table-component": "^7.5.2",
|
|
43
|
+
"use-places-autocomplete": "^4.0.0"
|
|
42
44
|
}
|
|
43
45
|
}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import React, { useState, useRef, useEffect } from "react";
|
|
2
2
|
import Button from "@mui/material/Button";
|
|
3
3
|
import { Box } from "@mui/system";
|
|
4
|
-
import AutoCompleteDialog from "../modals/PlacesAutocomplete";
|
|
5
4
|
import TextField from "@mui/material/TextField";
|
|
6
5
|
import Grid from "@mui/material/Grid";
|
|
7
6
|
import Script from 'next/script';
|
|
7
|
+
import { MenuItem, MenuList } from "@mui/material";
|
|
8
|
+
import ListItemIcon from '@mui/material/ListItemIcon';
|
|
9
|
+
import LocationOnRoundedIcon from '@mui/icons-material/LocationOnRounded';
|
|
10
|
+
import ListItemText from '@mui/material/ListItemText';
|
|
11
|
+
import useOnclickOutside from "react-cool-onclickoutside";
|
|
8
12
|
|
|
9
13
|
import usePlacesAutocomplete, {
|
|
10
14
|
getGeocode,
|
|
@@ -13,6 +17,322 @@ import usePlacesAutocomplete, {
|
|
|
13
17
|
getLatLng,
|
|
14
18
|
} from "use-places-autocomplete";
|
|
15
19
|
|
|
20
|
+
const AutoCompleteDialog = ({placeholder, onSelected, defaultValue, onChange}) => {
|
|
21
|
+
|
|
22
|
+
const txtAddressField = useRef(null);
|
|
23
|
+
|
|
24
|
+
const dismissSuggestions = () => {
|
|
25
|
+
clearSuggestions();
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const ref = useOnclickOutside(dismissSuggestions);
|
|
29
|
+
|
|
30
|
+
const {
|
|
31
|
+
ready,
|
|
32
|
+
value,
|
|
33
|
+
suggestions: { status, data },
|
|
34
|
+
setValue,
|
|
35
|
+
clearSuggestions,
|
|
36
|
+
} = usePlacesAutocomplete({
|
|
37
|
+
requestOptions: {
|
|
38
|
+
/* Define search scope here */
|
|
39
|
+
},
|
|
40
|
+
debounce: 1000,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const handleInput = (e) => {
|
|
44
|
+
e.stopPropagation()
|
|
45
|
+
|
|
46
|
+
// Update the keyword of the input element
|
|
47
|
+
setValue(e.target.value);
|
|
48
|
+
|
|
49
|
+
onChange(e.target.value);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
|
|
54
|
+
setValue(defaultValue) // test this!!!!
|
|
55
|
+
|
|
56
|
+
}, [defaultValue])
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
const determineComponentType = (component) => {
|
|
60
|
+
|
|
61
|
+
let returnVal = {};
|
|
62
|
+
|
|
63
|
+
for (let index = 0; index < component.types.length; index++) {
|
|
64
|
+
const componentType = component.types[index];
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
switch(componentType)
|
|
68
|
+
{
|
|
69
|
+
case "street_number":
|
|
70
|
+
|
|
71
|
+
returnVal = {
|
|
72
|
+
componentType: "Street_number",
|
|
73
|
+
long_name: component.long_name,
|
|
74
|
+
short_name: component.short_name
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
break;
|
|
78
|
+
case "route":
|
|
79
|
+
|
|
80
|
+
returnVal = {
|
|
81
|
+
componentType: "Route",
|
|
82
|
+
long_name: component.long_name,
|
|
83
|
+
short_name: component.short_name
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
break;
|
|
87
|
+
|
|
88
|
+
case "locality":
|
|
89
|
+
|
|
90
|
+
returnVal = {
|
|
91
|
+
componentType: "City",
|
|
92
|
+
long_name: component.long_name,
|
|
93
|
+
short_name: component.short_name
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
break;
|
|
97
|
+
|
|
98
|
+
case "administrative_area_level_2":
|
|
99
|
+
|
|
100
|
+
returnVal = {
|
|
101
|
+
componentType: "County",
|
|
102
|
+
long_name: component.long_name,
|
|
103
|
+
short_name: component.short_name
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
break;
|
|
107
|
+
|
|
108
|
+
case "administrative_area_level_1":
|
|
109
|
+
|
|
110
|
+
returnVal = {
|
|
111
|
+
componentType: "State",
|
|
112
|
+
long_name: component.long_name,
|
|
113
|
+
short_name: component.short_name
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
break;
|
|
117
|
+
|
|
118
|
+
case "country":
|
|
119
|
+
|
|
120
|
+
returnVal = {
|
|
121
|
+
componentType: "country",
|
|
122
|
+
long_name: component.long_name,
|
|
123
|
+
short_name: component.short_name
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
break;
|
|
127
|
+
|
|
128
|
+
case "postal_code":
|
|
129
|
+
|
|
130
|
+
returnVal = {
|
|
131
|
+
componentType: "postal_code",
|
|
132
|
+
long_name: component.long_name,
|
|
133
|
+
short_name: component.short_name
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
break;
|
|
137
|
+
|
|
138
|
+
case "postal_code_suffix":
|
|
139
|
+
|
|
140
|
+
returnVal = {
|
|
141
|
+
componentType: "postal_code_suffix",
|
|
142
|
+
long_name: component.long_name,
|
|
143
|
+
short_name: component.short_name
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return returnVal;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const getSimplifyResult = (result) => {
|
|
154
|
+
|
|
155
|
+
let street_number = "";
|
|
156
|
+
let route = "";
|
|
157
|
+
let city = "";
|
|
158
|
+
let county = "";
|
|
159
|
+
let state = "";
|
|
160
|
+
let country = "";
|
|
161
|
+
let postal_code = "";
|
|
162
|
+
let postal_code_suffix = "";
|
|
163
|
+
let lat = null;
|
|
164
|
+
let long = null;
|
|
165
|
+
let mapUrl = "";
|
|
166
|
+
let placeId = null;
|
|
167
|
+
|
|
168
|
+
for (let index = 0; index < result.address_components.length; index++) {
|
|
169
|
+
const component = result.address_components[index];
|
|
170
|
+
|
|
171
|
+
let response = determineComponentType(component);
|
|
172
|
+
|
|
173
|
+
if (response.componentType == "Street_number")
|
|
174
|
+
{
|
|
175
|
+
street_number = response.long_name;
|
|
176
|
+
}
|
|
177
|
+
else if (response.componentType == "Route")
|
|
178
|
+
{
|
|
179
|
+
route = response.long_name;
|
|
180
|
+
}
|
|
181
|
+
else if (response.componentType == "City")
|
|
182
|
+
{
|
|
183
|
+
city = response.long_name;
|
|
184
|
+
}
|
|
185
|
+
else if (response.componentType == "County")
|
|
186
|
+
{
|
|
187
|
+
county = response.long_name;
|
|
188
|
+
}
|
|
189
|
+
else if (response.componentType == "State")
|
|
190
|
+
{
|
|
191
|
+
state = response.short_name;
|
|
192
|
+
}
|
|
193
|
+
else if (response.componentType == "country")
|
|
194
|
+
{
|
|
195
|
+
country = response.long_name;
|
|
196
|
+
}
|
|
197
|
+
else if (response.componentType == "postal_code")
|
|
198
|
+
{
|
|
199
|
+
postal_code = response.long_name;
|
|
200
|
+
}
|
|
201
|
+
else if (response.componentType == "postal_code_suffix")
|
|
202
|
+
{
|
|
203
|
+
postal_code_suffix = response.long_name;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (result.geometry != null && result.geometry.location != null)
|
|
208
|
+
{
|
|
209
|
+
lat = result.geometry.location.lat;
|
|
210
|
+
long = result.geometry.location.lng;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
mapUrl = result.url;
|
|
214
|
+
placeId = result.place_id;
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
address: (street_number + " " + route),
|
|
218
|
+
city: city,
|
|
219
|
+
state: state,
|
|
220
|
+
county: county,
|
|
221
|
+
postalCode: postal_code,
|
|
222
|
+
country: country,
|
|
223
|
+
lat: lat,
|
|
224
|
+
long: long,
|
|
225
|
+
mapUrl: mapUrl,
|
|
226
|
+
placeId: placeId
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const handleSelect =
|
|
232
|
+
({ description, place_id }) =>
|
|
233
|
+
() => {
|
|
234
|
+
|
|
235
|
+
const parameter = {
|
|
236
|
+
// Use the "place_id" of suggestion from the dropdown (object), here just taking first suggestion for brevity
|
|
237
|
+
placeId: place_id,
|
|
238
|
+
// Specify the return data that you want (optional)
|
|
239
|
+
//fields: ["name", "rating"],
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
clearSuggestions();
|
|
243
|
+
|
|
244
|
+
getDetails(parameter)
|
|
245
|
+
.then((result) => {
|
|
246
|
+
|
|
247
|
+
let response = getSimplifyResult(result);
|
|
248
|
+
if (response != null)
|
|
249
|
+
{
|
|
250
|
+
onSelected(response);
|
|
251
|
+
|
|
252
|
+
setValue(response.address, false);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
})
|
|
256
|
+
.catch((error) => {
|
|
257
|
+
alert(error);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
// When user selects a place, we can replace the keyword without request data from API
|
|
262
|
+
// by setting the second parameter to "false"
|
|
263
|
+
//setValue(description, false);
|
|
264
|
+
// clearSuggestions();
|
|
265
|
+
|
|
266
|
+
// var parseUSAddress = parser.parseLocation(description);
|
|
267
|
+
|
|
268
|
+
// // Get latitude and longitude via utility functions
|
|
269
|
+
// getGeocode({ address: description }).then((results) => {
|
|
270
|
+
// const { lat, lng } = getLatLng(results[0]);
|
|
271
|
+
// //console.log("📍 Coordinates: ", { lat, lng });
|
|
272
|
+
|
|
273
|
+
// const zipCode = getZipCode(results[0], false);
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
// let address = "";
|
|
277
|
+
// if (parseUSAddress.number != null)
|
|
278
|
+
// {
|
|
279
|
+
// address += parseUSAddress.number + " ";
|
|
280
|
+
// }
|
|
281
|
+
|
|
282
|
+
// if (parseUSAddress.street != null)
|
|
283
|
+
// {
|
|
284
|
+
// address += parseUSAddress.street + " ";
|
|
285
|
+
// }
|
|
286
|
+
|
|
287
|
+
// if (parseUSAddress.type != null)
|
|
288
|
+
// {
|
|
289
|
+
// address += parseUSAddress.type;
|
|
290
|
+
// }
|
|
291
|
+
|
|
292
|
+
// onSelected({
|
|
293
|
+
// address: address,
|
|
294
|
+
// city: parseUSAddress.city,
|
|
295
|
+
// state: parseUSAddress.state,
|
|
296
|
+
// zip: zipCode,
|
|
297
|
+
// lat: lat,
|
|
298
|
+
// lng: lng
|
|
299
|
+
// });
|
|
300
|
+
|
|
301
|
+
// setValue(address, false);
|
|
302
|
+
|
|
303
|
+
// });
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const renderSuggestions = () =>
|
|
307
|
+
data.map((suggestion) => {
|
|
308
|
+
const {
|
|
309
|
+
place_id,
|
|
310
|
+
structured_formatting: { main_text, secondary_text },
|
|
311
|
+
} = suggestion;
|
|
312
|
+
|
|
313
|
+
return (
|
|
314
|
+
<MenuItem key={place_id} onClick={handleSelect(suggestion, place_id)} sx={{paddingRight:2}}>
|
|
315
|
+
<ListItemIcon>
|
|
316
|
+
<LocationOnRoundedIcon fontSize="small" />
|
|
317
|
+
</ListItemIcon>
|
|
318
|
+
<ListItemText><strong>{main_text}</strong> <small>{secondary_text}</small></ListItemText>
|
|
319
|
+
</MenuItem>
|
|
320
|
+
);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
return (
|
|
324
|
+
<>
|
|
325
|
+
<TextField ref={txtAddressField} autoFocus={true} autoComplete="new-password" label={placeholder} variant="outlined" fullWidth={true} value={value} onChange={handleInput} />
|
|
326
|
+
|
|
327
|
+
{status == "OK" &&
|
|
328
|
+
<MenuList ref={ref} sx={{position:"absolute", zIndex:9999, top:125, backgroundColor:"white", border:"1px solid black"}}>
|
|
329
|
+
{renderSuggestions()}
|
|
330
|
+
</MenuList>
|
|
331
|
+
}
|
|
332
|
+
</>
|
|
333
|
+
);
|
|
334
|
+
};
|
|
335
|
+
|
|
16
336
|
export default function GooglePlacesModal({
|
|
17
337
|
onSave,
|
|
18
338
|
createButtonText = "Save"
|