@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.
@@ -66,13 +66,31 @@ var useLocationSearch = (googleApi) => {
66
66
  const getPredictions = async (text) => {
67
67
  try {
68
68
  const response = await fetch(
69
- `https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${text}&components=country:nz&key=${googleApi}`
69
+ "https://places.googleapis.com/v1/places:autocomplete",
70
+ {
71
+ body: JSON.stringify({
72
+ includedRegionCodes: ["nz"],
73
+ input: text,
74
+ languageCode: "en"
75
+ }),
76
+ headers: {
77
+ "Content-Type": "application/json",
78
+ "X-Goog-Api-Key": googleApi,
79
+ "X-Goog-FieldMask": "suggestions.placePrediction.placeId,suggestions.placePrediction.text.text"
80
+ },
81
+ method: "POST"
82
+ }
70
83
  );
71
84
  if (!response.ok) {
72
85
  throw new Error(`HTTP error! Status: ${response.status}`);
73
86
  }
74
87
  const data = await response.json();
75
- return data.predictions;
88
+ const suggestions = data.suggestions ?? [];
89
+ const predictions = suggestions.map((suggestion) => suggestion.placePrediction).filter(Boolean).map((prediction) => ({
90
+ description: prediction?.text?.text || "",
91
+ place_id: prediction?.placeId || ""
92
+ })).filter((prediction) => prediction.place_id && prediction.description);
93
+ return predictions;
76
94
  } catch (error) {
77
95
  console.error("Error fetching predictions:", error);
78
96
  handleApiError(error, "Failed to fetch address predictions.");
@@ -81,42 +99,47 @@ var useLocationSearch = (googleApi) => {
81
99
  const getPlaceDetails = async (placeId) => {
82
100
  try {
83
101
  const response = await fetch(
84
- `https://maps.googleapis.com/maps/api/place/details/json?place_id=${placeId}&key=${googleApi}`
102
+ `https://places.googleapis.com/v1/places/${placeId}`,
103
+ {
104
+ headers: {
105
+ "X-Goog-Api-Key": googleApi,
106
+ "X-Goog-FieldMask": "addressComponents,formattedAddress,location"
107
+ }
108
+ }
85
109
  );
86
110
  if (!response.ok) {
87
111
  throw new Error(`HTTP error! Status: ${response.status}`);
88
112
  }
89
- const data = await response.json();
90
- const { result } = data;
91
- const { lat, lng } = result.geometry.location;
92
- const { address_components, formatted_address } = result;
93
- const address = address_components.reduce((acc, item) => {
113
+ const result = await response.json();
114
+ const { latitude, longitude } = result.location;
115
+ const { addressComponents, formattedAddress } = result;
116
+ const address = addressComponents.reduce((acc, item) => {
94
117
  if (item.types.includes("street_number")) {
95
- return { ...acc, streetNumber: item.long_name };
118
+ return { ...acc, streetNumber: item.longText };
96
119
  }
97
120
  if (item.types.includes("route")) {
98
- return { ...acc, streetName: item.long_name };
121
+ return { ...acc, streetName: item.longText };
99
122
  }
100
123
  if (item.types.includes("locality")) {
101
- return { ...acc, city: item.long_name };
124
+ return { ...acc, city: item.longText };
102
125
  }
103
126
  if (item.types.includes("administrative_area_level_1")) {
104
- return { ...acc, region: item.long_name };
127
+ return { ...acc, region: item.longText };
105
128
  }
106
129
  if (item.types.includes("country")) {
107
- return { ...acc, country: item.long_name };
130
+ return { ...acc, country: item.longText };
108
131
  }
109
132
  return acc;
110
133
  }, {});
111
134
  const newLocation = {
112
- city: address.city.toLowerCase(),
113
- coordinates: [lng, lat],
135
+ city: address.city ? address.city.toLowerCase() : "",
136
+ coordinates: [longitude, latitude],
114
137
  // [longitude, latitude]
115
138
  country: address.country,
116
- fullAddress: formatted_address,
117
- latitude: lat,
118
- longitude: lng,
119
- region: address.region.replace(/ Region$/, ""),
139
+ fullAddress: formattedAddress,
140
+ latitude,
141
+ longitude,
142
+ region: address.region ? address.region.replace(/ Region$/, "") : "",
120
143
  // Remove " Region" suffix
121
144
  type: "Point"
122
145
  // Mongoose GeoJSON type