@timardex/cluemart-shared 1.3.65 → 1.3.67

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.
@@ -36,13 +36,31 @@ var useLocationSearch = (googleApi) => {
36
36
  const getPredictions = async (text) => {
37
37
  try {
38
38
  const response = await fetch(
39
- `https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${text}&components=country:nz&key=${googleApi}`
39
+ "https://places.googleapis.com/v1/places:autocomplete",
40
+ {
41
+ body: JSON.stringify({
42
+ includedRegionCodes: ["nz"],
43
+ input: text,
44
+ languageCode: "en"
45
+ }),
46
+ headers: {
47
+ "Content-Type": "application/json",
48
+ "X-Goog-Api-Key": googleApi,
49
+ "X-Goog-FieldMask": "suggestions.placePrediction.placeId,suggestions.placePrediction.text.text"
50
+ },
51
+ method: "POST"
52
+ }
40
53
  );
41
54
  if (!response.ok) {
42
55
  throw new Error(`HTTP error! Status: ${response.status}`);
43
56
  }
44
57
  const data = await response.json();
45
- return data.predictions;
58
+ const suggestions = data.suggestions ?? [];
59
+ const predictions = suggestions.map((suggestion) => suggestion.placePrediction).filter(Boolean).map((prediction) => ({
60
+ description: prediction?.text?.text || "",
61
+ place_id: prediction?.placeId || ""
62
+ })).filter((prediction) => prediction.place_id && prediction.description);
63
+ return predictions;
46
64
  } catch (error) {
47
65
  console.error("Error fetching predictions:", error);
48
66
  handleApiError(error, "Failed to fetch address predictions.");
@@ -51,42 +69,47 @@ var useLocationSearch = (googleApi) => {
51
69
  const getPlaceDetails = async (placeId) => {
52
70
  try {
53
71
  const response = await fetch(
54
- `https://maps.googleapis.com/maps/api/place/details/json?place_id=${placeId}&key=${googleApi}`
72
+ `https://places.googleapis.com/v1/places/${placeId}`,
73
+ {
74
+ headers: {
75
+ "X-Goog-Api-Key": googleApi,
76
+ "X-Goog-FieldMask": "addressComponents,formattedAddress,location"
77
+ }
78
+ }
55
79
  );
56
80
  if (!response.ok) {
57
81
  throw new Error(`HTTP error! Status: ${response.status}`);
58
82
  }
59
- const data = await response.json();
60
- const { result } = data;
61
- const { lat, lng } = result.geometry.location;
62
- const { address_components, formatted_address } = result;
63
- const address = address_components.reduce((acc, item) => {
83
+ const result = await response.json();
84
+ const { latitude, longitude } = result.location;
85
+ const { addressComponents, formattedAddress } = result;
86
+ const address = addressComponents.reduce((acc, item) => {
64
87
  if (item.types.includes("street_number")) {
65
- return { ...acc, streetNumber: item.long_name };
88
+ return { ...acc, streetNumber: item.longText };
66
89
  }
67
90
  if (item.types.includes("route")) {
68
- return { ...acc, streetName: item.long_name };
91
+ return { ...acc, streetName: item.longText };
69
92
  }
70
93
  if (item.types.includes("locality")) {
71
- return { ...acc, city: item.long_name };
94
+ return { ...acc, city: item.longText };
72
95
  }
73
96
  if (item.types.includes("administrative_area_level_1")) {
74
- return { ...acc, region: item.long_name };
97
+ return { ...acc, region: item.longText };
75
98
  }
76
99
  if (item.types.includes("country")) {
77
- return { ...acc, country: item.long_name };
100
+ return { ...acc, country: item.longText };
78
101
  }
79
102
  return acc;
80
103
  }, {});
81
104
  const newLocation = {
82
- city: address.city.toLowerCase(),
83
- coordinates: [lng, lat],
105
+ city: address.city ? address.city.toLowerCase() : "",
106
+ coordinates: [longitude, latitude],
84
107
  // [longitude, latitude]
85
108
  country: address.country,
86
- fullAddress: formatted_address,
87
- latitude: lat,
88
- longitude: lng,
89
- region: address.region.replace(/ Region$/, ""),
109
+ fullAddress: formattedAddress,
110
+ latitude,
111
+ longitude,
112
+ region: address.region ? address.region.replace(/ Region$/, "") : "",
90
113
  // Remove " Region" suffix
91
114
  type: "Point"
92
115
  // Mongoose GeoJSON type