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.

257 lines (256 loc) 21.2 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 }; } }; import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { FilterGeo, PageController, WidgetDataType, useSearchResults, useSearchResultsSelectedFilters, widget, } from "@sitecore-search/react"; import Spinner from "../Spinner"; import Filter from "../Filter"; import QueryResultsSummary from "../QueryResultsSummary"; import SearchFacets from "../SearchFacets"; import SortOrder from "../SortOrder"; import ArticleHorizontalItemCard from "../ArticleHorizontalCard"; import ResultsPerPage from "../ResultsPerPage"; import SearchPagination from "../SearchPagination"; import { LoadScript } from "@react-google-maps/api"; import AutoCompleteWrapper from "../../helpers/AutoCompleteWrapper"; import { useEffect, useState } from "react"; import GoogleMap from "../../helpers/GoogleMap"; export var SearchResultsComponent = function (_a) { var _b = _a.defaultSortType, defaultSortType = _b === void 0 ? "featured_desc" : _b, _c = _a.defaultPage, defaultPage = _c === void 0 ? 1 : _c, _d = _a.defaultKeyphrase, defaultKeyphrase = _d === void 0 ? "" : _d, _e = _a.defaultItemsPerPage, defaultItemsPerPage = _e === void 0 ? 10 : _e, searchSource = _a.searchSource, layout = _a.layout, title = _a.title, description = _a.description, noResultFound = _a.noResultFound, inputPlaceholder = _a.inputPlaceholder, mapIcon = _a.mapIcon, radiusInKM = _a.radiusInKM, customArticleRenderer = _a.customArticleRenderer; var _f = useState(false), isErrorInStoreData = _f[0], setIsErrorInStoreData = _f[1]; var _g = useState({}), activeMarker = _g[0], setActiveMarker = _g[1]; var selectedFacetsFromApi = useSearchResultsSelectedFilters(); var searchSourceIds = (searchSource === null || searchSource === void 0 ? void 0 : searchSource.split("|")) || []; var _h = useState({ lat: 37.8044, lng: -122.2711, }), coordinates = _h[0], setCoordinates = _h[1]; var _j = useState([]), stores = _j[0], setStores = _j[1]; var _k = useState(""), address = _k[0], setAddress = _k[1]; var _l = useState({ Keywords: "", }), selectedSuggestions = _l[0], setSelectedSuggestions = _l[1]; var renderForm = function (onAutoSuggestionSubmit) { return (_jsx(_Fragment, { children: _jsxs("form", { className: "relative px-[16px]", children: [_jsx("input", { "aria-label": "google-auto-suggestion-address", name: "google-auto-suggestion-address", id: "google-auto-suggestion-address", value: address, placeholder: inputPlaceholder, className: "input w-full p-4 rounded-2xl shadow-md bg-gray-100 text-gray-800 placeholder-gray-400 border-2 border-transparent focus:border-blue-400 hover:border-blue-400 focus:outline-none transition-all", onChange: function (e) { var _a; setAddress((_a = e === null || e === void 0 ? void 0 : e.target) === null || _a === void 0 ? void 0 : _a.value); setIsErrorInStoreData(false); } }), _jsx("button", { "aria-label": "search", onClick: function (e) { return __awaiter(void 0, void 0, void 0, function () { var inputElement, inputValue, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: e.preventDefault(); inputElement = document.getElementById("google-auto-suggestion-address"); if (!inputElement) return [3 /*break*/, 2]; inputValue = inputElement === null || inputElement === void 0 ? void 0 : inputElement.value; setAddress(inputValue); setIsErrorInStoreData(false); if (!inputValue) return [3 /*break*/, 2]; _a = handleSelect; return [4 /*yield*/, onAutoSuggestionSubmit(inputValue)]; case 1: _a.apply(void 0, [_b.sent()]); _b.label = 2; case 2: return [2 /*return*/]; } }); }); }, className: "" })] }) })); }; var _m = useSearchResults({ query: function (query) { var geoFilter = new FilterGeo("location", "".concat(radiusInKM || 10, "km")); query .getRequest() .setSearchFacetAll(true) .setSources(searchSourceIds) .setSearchFilter(geoFilter); }, state: { sortType: defaultSortType, page: defaultPage, itemsPerPage: defaultItemsPerPage, keyphrase: defaultKeyphrase, }, }), widgetRef = _m.widgetRef, onItemClick = _m.actions.onItemClick, _o = _m.state, sortType = _o.sortType, page = _o.page, itemsPerPage = _o.itemsPerPage, _p = _m.queryResult, isLoading = _p.isLoading, isFetching = _p.isFetching, _q = _p.data, _r = _q === void 0 ? {} : _q, _s = _r.total_item, totalItems = _s === void 0 ? 0 : _s, _t = _r.sort, _u = _t === void 0 ? {} : _t, _v = _u.choices, sortChoices = _v === void 0 ? [] : _v, _w = _r.facet, facets = _w === void 0 ? [] : _w, _x = _r.content, storesList = _x === void 0 ? [] : _x; useEffect(function () { if ((coordinates === null || coordinates === void 0 ? void 0 : coordinates.lat) && (coordinates === null || coordinates === void 0 ? void 0 : coordinates.lng)) { PageController.getContext().setGeo({ location: { lat: Number(coordinates === null || coordinates === void 0 ? void 0 : coordinates.lat), lon: Number(coordinates === null || coordinates === void 0 ? void 0 : coordinates.lng), }, }); } }, [coordinates]); useEffect(function () { var _a; if (!(isLoading || isFetching)) { setStores(storesList); if ((storesList === null || storesList === void 0 ? void 0 : storesList.length) <= 0 && ((_a = selectedSuggestions === null || selectedSuggestions === void 0 ? void 0 : selectedSuggestions.Keywords) === null || _a === void 0 ? void 0 : _a.length) > 0) { setIsErrorInStoreData(true); } } }, [isLoading, isFetching]); var totalPages = Math.ceil(totalItems / itemsPerPage); var handleSelect = function (value) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { setAddress(value === null || value === void 0 ? void 0 : value.keyword); if (value && (value === null || value === void 0 ? void 0 : value.isError)) { setIsErrorInStoreData(true); } else if (value === null || value === void 0 ? void 0 : value.keyword) { setIsErrorInStoreData(false); setSelectedSuggestions({ Keywords: value === null || value === void 0 ? void 0 : value.keyword, }); if (!((coordinates === null || coordinates === void 0 ? void 0 : coordinates.lat) === value.lat && (coordinates === null || coordinates === void 0 ? void 0 : coordinates.lng) === value.lng)) { setStores([]); } if ((coordinates === null || coordinates === void 0 ? void 0 : coordinates.lat) === value.lat && (coordinates === null || coordinates === void 0 ? void 0 : coordinates.lng) === value.lng && (stores === null || stores === void 0 ? void 0 : stores.length) === 0 && (storesList === null || storesList === void 0 ? void 0 : storesList.length) > 0) { setStores(storesList); } //TODO: No need to do anything when same latitude and longitude else if ((coordinates === null || coordinates === void 0 ? void 0 : coordinates.lat) === value.lat && (coordinates === null || coordinates === void 0 ? void 0 : coordinates.lng) === value.lng) { } else { setCoordinates({ lat: value.lat, lng: value.lng }); } } return [2 /*return*/]; }); }); }; if (isLoading) { return (_jsx("div", { className: "flex justify-center items-center h-screen w-full bg-white ", children: _jsx(Spinner, { loading: true }) })); } var renderInput = function () { return (_jsx(AutoCompleteWrapper, { options: { types: ["(regions)"], componentRestrictions: { country: "us", }, bounds: { //This calculates the radius and this will be in static form only east: coordinates.lng + (30000 / 111000) * Math.cos(coordinates.lat * (Math.PI / 180)), west: coordinates.lng - (30000 / 111000) * Math.cos(coordinates.lat * (Math.PI / 180)), north: coordinates.lat + 30000 / 111000, south: coordinates.lat - 30000 / 111000, }, }, addressLine1: address, hideSuggestions: (address === null || address === void 0 ? void 0 : address.length) < 3, onHandleChange: handleSelect, children: function (_a) { var onAutoSuggestionSubmit = _a.onAutoSuggestionSubmit; return renderForm(onAutoSuggestionSubmit); } })); }; var renderMap = function (mapStyles) { return (_jsx(GoogleMap, { coordinates: coordinates, activeMarker: activeMarker, mapStyles: mapStyles, setActiveMarker: setActiveMarker, mapIcon: mapIcon, storeCoordinates: stores === null || stores === void 0 ? void 0 : stores.map(function (store) { return { id: store === null || store === void 0 ? void 0 : store.id, latitude: +(store === null || store === void 0 ? void 0 : store.latitude), longitude: +(store === null || store === void 0 ? void 0 : store.longitude), }; }) })); }; var renderContent = function () { return (_jsxs("div", { ref: widgetRef, className: "flex relative max-w-full px-4 text-black dark:text-gray-100 text-opacity-75", children: [isFetching && (_jsx("div", { className: "w-full h-full fixed top-0 left-0 bottom-0 right-0 z-30 bg-white dark:bg-gray-900 opacity-50", children: _jsx("div", { className: "absolute top-[50%] left-[50%] -translate-x-[50%] -translate-y-[50%] flex flex-col justify-center items-center z-40", children: _jsx(Spinner, { loading: true }) }) })), totalItems > 0 && (_jsxs(_Fragment, { children: [selectedFacetsFromApi.length > 0 || ((facets === null || facets === void 0 ? void 0 : facets.length) > 0 && (_jsxs("section", { className: "flex flex-col flex-none relative mt-4 mr-8 w-[25%]", children: [_jsx(Filter, {}), _jsx(SearchFacets, { facets: facets })] }))), _jsxs("section", { className: "flex flex-col flex-[4_1_0%]", children: [_jsxs("section", { className: "flex justify-between text-xs", children: [totalItems > 0 && (_jsx(QueryResultsSummary, { currentPage: page, itemsPerPage: itemsPerPage, totalItems: totalItems, totalItemsReturned: storesList.length })), _jsx(SortOrder, { options: sortChoices, selected: sortType })] }), _jsx("div", { className: "w-full", children: storesList.map(function (a, index) { return (_jsx(ArticleHorizontalItemCard, { setActiveMarker: setActiveMarker, article: a, index: index, onItemClick: onItemClick, displayText: true, customArticleRenderer: customArticleRenderer }, a.id)); }) }), _jsxs("div", { className: " text-black !text-[16px] flex flex-col md:flex-row md:justify-between text-xs", children: [_jsx(ResultsPerPage, { defaultItemsPerPage: defaultItemsPerPage }), _jsx(SearchPagination, { currentPage: page, totalPages: totalPages })] })] })] })), totalItems <= 0 && !isFetching && (_jsx("div", { className: "w-full flex pt-[50px] justify-center", children: _jsx("h3", { className: "text-black text-[20px] font-[500]", children: noResultFound }) }))] })); }; var SearchWithInputOnTopWithParallelMapAndContent = function () { return (_jsxs(_Fragment, { children: [title && (_jsx("p", { className: "title text-[50px] font-[600] mb-[20px] w-full text-center", children: title })), description && (_jsx("p", { className: "description text-[20px] font-[600] mb-[40px] w-full text-center", children: description })), renderInput(), _jsxs("div", { className: "flex ", children: [_jsx("div", { className: "min-w-[50%]", children: renderMap({ height: "500px", width: "100%", borderRadius: "30px", }) }), _jsx("div", { className: "min-w-[50%]", children: renderContent() })] })] })); }; var SearchWithInputOnTopWithParallelMapAndContentReverse = function () { return (_jsxs(_Fragment, { children: [title && (_jsx("p", { className: "title text-[50px] font-[600] mb-[20px] w-full text-center", children: title })), description && (_jsx("p", { className: "description text-[20px] font-[600] mb-[40px] w-full text-center", children: description })), renderInput(), _jsxs("div", { className: "flex flex-row-reverse", children: [_jsx("div", { className: "min-w-[50%]", children: renderMap({ height: "500px", width: "100%", borderRadius: "30px", }) }), _jsx("div", { className: "min-w-[50%]", children: renderContent() })] })] })); }; var SearchWithInputMapContentStack = function () { return (_jsxs(_Fragment, { children: [title && (_jsx("p", { className: "title text-[50px] font-[600] mb-[20px] w-full text-center", children: title })), description && (_jsx("p", { className: "description text-[20px] font-[600] mb-[40px] w-full text-center", children: description })), renderInput(), renderMap({ height: "500px", width: "100%", borderRadius: "30px", }), renderContent()] })); }; var SearchWithInputOnTopWithParallelMapAndContentSmallMap = function () { return (_jsxs(_Fragment, { children: [title && (_jsx("p", { className: "title text-[50px] font-[600] mb-[20px] w-full text-center", children: title })), description && (_jsx("p", { className: "description text-[20px] font-[600] mb-[40px] w-full text-center", children: description })), renderInput(), _jsxs("div", { className: "flex ", children: [renderMap({ height: "500px", width: "500px", borderRadius: "30px", }), _jsxs("div", { className: "w-full", children: [" ", renderContent()] })] })] })); }; var SearchWithInputOnTopWithParallelMapAndContentSmallMapReverse = function () { return (_jsxs(_Fragment, { children: [title && (_jsx("p", { className: "title text-[50px] font-[600] mb-[20px] w-full text-center", children: title })), description && (_jsx("p", { className: "description text-[20px] font-[600] mb-[40px] w-full text-center", children: description })), renderInput(), _jsxs("div", { className: "flex flex-row-reverse", children: [renderMap({ height: "500px", width: "500px", borderRadius: "30px", }), _jsxs("div", { className: "w-full", children: [" ", renderContent()] })] })] })); }; var SearchWithParallelMapAndInputWithContent = function () { return (_jsxs(_Fragment, { children: [title && (_jsx("p", { className: "title text-[50px] font-[600] mb-[20px] w-full text-center", children: title })), description && (_jsx("p", { className: "description text-[20px] font-[600] mb-[40px] w-full text-center", children: description })), _jsxs("div", { className: "flex ", children: [renderMap({ height: "500px", width: "500px", borderRadius: "30px", }), _jsxs("div", { className: "w-full", children: [renderInput(), renderContent()] })] })] })); }; var SearchWithParallelMapAndInputWithContentReverse = function () { return (_jsxs(_Fragment, { children: [title && (_jsx("p", { className: "title text-[50px] font-[600] mb-[20px] w-full text-center", children: title })), description && (_jsx("p", { className: "description text-[20px] font-[600] mb-[40px] w-full text-center", children: description })), _jsxs("div", { className: "flex flex-row-reverse", children: [renderMap({ height: "500px", width: "500px", borderRadius: "30px", }), _jsxs("div", { className: "w-full", children: [renderInput(), renderContent()] })] })] })); }; var componentMap = { SearchWithInputOnTopWithParallelMapAndContent: SearchWithInputOnTopWithParallelMapAndContent, SearchWithInputMapContentStack: SearchWithInputMapContentStack, SearchWithInputOnTopWithParallelMapAndContentSmallMap: SearchWithInputOnTopWithParallelMapAndContentSmallMap, SearchWithParallelMapAndInputWithContent: SearchWithParallelMapAndInputWithContent, SearchWithInputOnTopWithParallelMapAndContentReverse: SearchWithInputOnTopWithParallelMapAndContentReverse, SearchWithInputOnTopWithParallelMapAndContentSmallMapReverse: SearchWithInputOnTopWithParallelMapAndContentSmallMapReverse, SearchWithParallelMapAndInputWithContentReverse: SearchWithParallelMapAndInputWithContentReverse, }; return (_jsx("div", { className: "container mx-auto pt-[40px] pb-[80px]", children: _jsx(LoadScript, { googleMapsApiKey: process.env.GOOGLE_API_KEY || "", libraries: ["places"], children: layout && (componentMap === null || componentMap === void 0 ? void 0 : componentMap[layout]) ? componentMap === null || componentMap === void 0 ? void 0 : componentMap[layout]() : SearchWithInputOnTopWithParallelMapAndContent() }) })); }; var SearchResultsWidget = widget(SearchResultsComponent, WidgetDataType.SEARCH_RESULTS, "content"); export default SearchResultsWidget;