UNPKG

sitecore-search-with-google-map

Version:

A library for seamless integration of Sitecore Search with Google Maps in Next.js applications, enabling location-based searches with flexible UI options.

198 lines (197 loc) 12.7 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; export var handlePlaceChanged = function (addressInputRef, setFieldValue, fieldMap, setFieldTouched) { var _a, _b; if (addressInputRef.current) { var data = (_a = addressInputRef.current) === null || _a === void 0 ? void 0 : _a.getPlace(); if (data) { var fullStreetAddress_1 = ""; (_b = data === null || data === void 0 ? void 0 : data.address_components) === null || _b === void 0 ? void 0 : _b.forEach(function (component) { var _a, _b, _c, _d, _e, _f, _g; if ((fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.zipcode) && ((_a = component === null || component === void 0 ? void 0 : component.types) === null || _a === void 0 ? void 0 : _a.includes("postal_code"))) { setFieldValue(fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.zipcode, component === null || component === void 0 ? void 0 : component.long_name); setTimeout(function () { if (fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.zipcode) { setFieldTouched(fieldMap.zipcode, true); } }, 100); } if (fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.addressLine1) { // this will be concate with ",_" and will replace the address below. if ((_b = component === null || component === void 0 ? void 0 : component.types) === null || _b === void 0 ? void 0 : _b.includes("street_number")) { fullStreetAddress_1 += (fullStreetAddress_1 ? ",_" : "") + (component === null || component === void 0 ? void 0 : component.short_name); } if ((_c = component === null || component === void 0 ? void 0 : component.types) === null || _c === void 0 ? void 0 : _c.includes("route")) { fullStreetAddress_1 += (fullStreetAddress_1 ? ",_" : "") + (component === null || component === void 0 ? void 0 : component.short_name); } } if ((fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.city) && ((_d = component === null || component === void 0 ? void 0 : component.types) === null || _d === void 0 ? void 0 : _d.includes("locality"))) { setFieldValue(fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.city, component === null || component === void 0 ? void 0 : component.long_name); setTimeout(function () { if (fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.city) { setFieldTouched(fieldMap.city, true); } }, 10); } if ((fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.country) && ((_e = component === null || component === void 0 ? void 0 : component.types) === null || _e === void 0 ? void 0 : _e.includes("country"))) { setFieldValue(fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.country, component === null || component === void 0 ? void 0 : component.short_name); setTimeout(function () { if (fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.country) { setFieldTouched(fieldMap.country, true); } }, 10); } if ((fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.stateCode) && ((_f = component === null || component === void 0 ? void 0 : component.types) === null || _f === void 0 ? void 0 : _f.includes("administrative_area_level_1"))) { setFieldValue(fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.stateCode, component === null || component === void 0 ? void 0 : component.short_name); setTimeout(function () { if (fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.stateCode) { setFieldTouched(fieldMap.stateCode, true); } }, 10); } if ((fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.state) && ((_g = component === null || component === void 0 ? void 0 : component.types) === null || _g === void 0 ? void 0 : _g.includes("administrative_area_level_1"))) { setFieldValue(fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.state, component === null || component === void 0 ? void 0 : component.long_name); setTimeout(function () { if (fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.state) { setFieldTouched(fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.state, true); } }, 10); } }); // set address line 1 field value which includes street number + route if (fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.addressLine1) { // removing this with ,_ and add space between street and route. fullStreetAddress_1 = fullStreetAddress_1 === null || fullStreetAddress_1 === void 0 ? void 0 : fullStreetAddress_1.replace(/,_/, " "); // set the value in field after format the street and route combine in one field. setFieldValue(fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.addressLine1, fullStreetAddress_1); setTimeout(function () { if (fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.addressLine1) { setFieldTouched(fieldMap === null || fieldMap === void 0 ? void 0 : fieldMap.addressLine1, false); } }, 10); } return getGeometryData(data); } else { return null; } } return null; }; // eslint-disable-next-line @typescript-eslint/no-explicit-any export var getGeometryData = function (mapData, keyword, options) { var _a, _b, _c, _d; if ((options === null || options === void 0 ? void 0 : options.bypassBoundErrorCheck) || mapData) { return { keyword: keyword || (mapData === null || mapData === void 0 ? void 0 : mapData.formatted_address), lat: (_b = (_a = mapData === null || mapData === void 0 ? void 0 : mapData.geometry) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.lat(), lng: (_d = (_c = mapData === null || mapData === void 0 ? void 0 : mapData.geometry) === null || _c === void 0 ? void 0 : _c.location) === null || _d === void 0 ? void 0 : _d.lng(), isError: false, results: mapData, }; } else { return { isError: true, keyword: keyword }; } }; // eslint-disable-next-line @typescript-eslint/no-explicit-any export var onAutoSuggestionSubmitClick = function (keyword, options) { return __awaiter(void 0, void 0, void 0, function () { var geocoder, geocoderData, error_1; var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: _c.trys.push([0, 3, , 4]); return [4 /*yield*/, new google.maps.Geocoder()]; case 1: geocoder = _c.sent(); return [4 /*yield*/, geocoder.geocode({ address: keyword, region: (_a = options === null || options === void 0 ? void 0 : options.componentRestrictions) === null || _a === void 0 ? void 0 : _a.country, bounds: (_b = options === null || options === void 0 ? void 0 : options.componentRestrictions) === null || _b === void 0 ? void 0 : _b.bounds, })]; case 2: geocoderData = _c.sent(); return [2 /*return*/, getGeometryData(geocoderData === null || geocoderData === void 0 ? void 0 : geocoderData.results[0], keyword, options)]; case 3: error_1 = _c.sent(); console.error(error_1); return [2 /*return*/, { isError: true, keyword: keyword }]; case 4: return [2 /*return*/]; } }); }); }; //Address Validation export var isValidAddress = function (keyword) { return __awaiter(void 0, void 0, void 0, function () { var geocoder, res, err_1; var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: geocoder = new google.maps.Geocoder(); _d.label = 1; case 1: _d.trys.push([1, 3, , 4]); return [4 /*yield*/, geocoder.geocode({ address: keyword }, function (results, status) { if (status === "OK" && results && results.length > 0) { return results; } else { return null; } })]; case 2: res = _d.sent(); if (res && ((_c = (_b = (_a = res === null || res === void 0 ? void 0 : res.results) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.geometry) === null || _c === void 0 ? void 0 : _c.location_type) === "ROOFTOP") { return [2 /*return*/, true]; } else { return [2 /*return*/, false]; } return [3 /*break*/, 4]; case 3: err_1 = _d.sent(); console.error(err_1); return [3 /*break*/, 4]; case 4: return [2 /*return*/, false]; } }); }); };