@sledge-app/react-instant-search 0.0.74 → 0.0.75

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.
@@ -6,7 +6,7 @@ const ProductFilterWidget = (props) => {
6
6
  return /* @__PURE__ */ jsxRuntimeExports.jsx(SearchResultWidget, { layoutType: "product-filter", ...props });
7
7
  };
8
8
  const API_URL = "https://sledge-api-preprod.offstack.io";
9
- const INSTANT_SEARCH_ENGINE_URL = "https://instant-search-engine.offstack.io";
9
+ const INSTANT_SEARCH_ENGINE_URL = "http://localhost:7700";
10
10
  const LOCAL_STORAGE_KEY = {
11
11
  ANONYM_ID: "sledge-anonym-id",
12
12
  AUTH_APP: "sledge-auth-app",
@@ -1564,8 +1564,8 @@ const WidgetHeader = (props) => {
1564
1564
  WidgetHeader.Sort = WidgetHeaderSort;
1565
1565
  WidgetHeader.AddTrigger = WidgetHeaderAddTrigger;
1566
1566
  WidgetHeader.Summary = WidgetHeaderSummary;
1567
- const fetchApi = async (url, method, body) => {
1568
- let sledgeInstantSearchAuthApp = localStorage.getItem(LOCAL_STORAGE_KEY.INSTANT_SEARCH_AUTH_APP);
1567
+ const fetchApi = async (url, method, body, token) => {
1568
+ let sledgeInstantSearchAuthApp = token || localStorage.getItem(LOCAL_STORAGE_KEY.INSTANT_SEARCH_AUTH_APP);
1569
1569
  var myHeaders = new Headers();
1570
1570
  myHeaders.append("Authorization", `Bearer ${sledgeInstantSearchAuthApp}`);
1571
1571
  myHeaders.append("Content-Type", "application/json");
@@ -1586,8 +1586,8 @@ const fetchApi = async (url, method, body) => {
1586
1586
  const search = async (index, body) => {
1587
1587
  return await fetchApi(`/indexes/${index}/search`, "POST", body);
1588
1588
  };
1589
- const multiSearch = async (body) => {
1590
- return await fetchApi("/multi-search", "POST", body);
1589
+ const multiSearch = async (body, token) => {
1590
+ return await fetchApi("/multi-search", "POST", body, token);
1591
1591
  };
1592
1592
  const searchTrigger = async (data) => {
1593
1593
  const { keyword, resultCount } = data;
@@ -1673,7 +1673,8 @@ const ProductGrid = ({
1673
1673
  onAfterRenderProduct,
1674
1674
  showPopupComponent,
1675
1675
  setShowPopupComponent,
1676
- dataReviews
1676
+ dataReviews,
1677
+ dataWishlists
1677
1678
  }) => {
1678
1679
  const {
1679
1680
  show_vendor = true,
@@ -1787,7 +1788,8 @@ const ProductGrid = ({
1787
1788
  },
1788
1789
  ...triggerPropAdditional,
1789
1790
  onAfterAddWishlist,
1790
- onAfterRemoveWishlist
1791
+ onAfterRemoveWishlist,
1792
+ wishlistChecked: dataWishlists == null ? void 0 : dataWishlists[id]
1791
1793
  }
1792
1794
  ),
1793
1795
  /* @__PURE__ */ jsxRuntimeExports.jsx("img", { src: (image == null ? void 0 : image.src) || "", alt: "sledge-product-card-image", loading: "lazy", onError: (event) => event.currentTarget.src = `${API_URL}/img/blank-image.png` })
@@ -2664,10 +2666,9 @@ const SearchIconWidgetPopup = () => {
2664
2666
  ] }) });
2665
2667
  };
2666
2668
  const SearchResultWidget = (props) => {
2667
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2668
- console.log("SearchResultWidget");
2669
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2669
2670
  const { isRenderApp, isJsVersion } = React__default.useContext(SledgeContext);
2670
- const { layoutType = "default", query, params, children, onAfterAddToCart, onAfterAddWishlist, onAfterRemoveWishlist, onAfterRenderProduct } = props;
2671
+ const { layoutType = "default", query, params, children, onAfterAddToCart, onAfterAddWishlist, onAfterRemoveWishlist, onAfterRenderProduct, data: propsData } = props;
2671
2672
  const { keyword: queryKeyword = "" } = query || {};
2672
2673
  const { collectionId } = params || {};
2673
2674
  let productCardsComponent = null;
@@ -2678,6 +2679,7 @@ const SearchResultWidget = (props) => {
2678
2679
  productCardsComponent = productCard;
2679
2680
  }
2680
2681
  });
2682
+ const [settings] = React__default.useState(typeof localStorage !== "undefined" ? (_a = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY.INSTANT_SEARCH_SETTING) || "{}")) == null ? void 0 : _a.data : {});
2681
2683
  const [isFirstLoading, setIsFirstLoading] = React__default.useState(true);
2682
2684
  const [isLoading, setIsLoading] = React__default.useState(true);
2683
2685
  const [keyword, setKeyword] = React__default.useState("");
@@ -2696,6 +2698,7 @@ const SearchResultWidget = (props) => {
2696
2698
  const [colorSwatches, setColorSwatches] = React__default.useState([]);
2697
2699
  const [defaultSort, setDefaultSort] = React__default.useState("");
2698
2700
  const [showOutOfStock, setShowOutOfStock] = React__default.useState(false);
2701
+ const [initStatesFirstTime, setInitStatesFirstTime] = React__default.useState(true);
2699
2702
  const searchResultContainerRef = React__default.useRef(null);
2700
2703
  const previousState = usePrevious({ keyword });
2701
2704
  const handleChangeTabIndex = (id, name, e) => {
@@ -2743,7 +2746,12 @@ const SearchResultWidget = (props) => {
2743
2746
  };
2744
2747
  })
2745
2748
  };
2746
- response = await multiSearch(body);
2749
+ if (propsData && initStatesFirstTime) {
2750
+ response = propsData.result;
2751
+ setInitStatesFirstTime(false);
2752
+ response = await multiSearch(body);
2753
+ } else
2754
+ response = await multiSearch(body);
2747
2755
  if (!response)
2748
2756
  return;
2749
2757
  const { results } = response;
@@ -2840,8 +2848,8 @@ const SearchResultWidget = (props) => {
2840
2848
  }
2841
2849
  }, [keyword]);
2842
2850
  const tabSkeleton = layoutType === "default" ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-tab-list sledge-instant-search__result-tab-list-skeleton", children: [1, 2, 3, 4, 5].map((index) => /* @__PURE__ */ jsxRuntimeExports.jsx(SkeletonLoading.Item, { width: "122px", height: "28px", rounded: "md", color: "grey-100", animationDuration: "15s" }, index)) }) : null;
2843
- const filterHorizontalSkeleton = ((_a = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _a.enable_on_search) && ((_b = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _b.layout) === "horizontal" ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-filter-horizontal-skeleton", children: [1, 2, 3, 4, 5, 6, 7].map((index) => /* @__PURE__ */ jsxRuntimeExports.jsx(SkeletonLoading.Item, { width: "106px", height: "42px", rounded: "md", color: "grey-100", animationDuration: "13s" }, index)) }) : null;
2844
- const filterVerticalSkeleton = ((_c = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _c.enable_on_search) && ((_d = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _d.layout) === "vertical" ? /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
2851
+ const filterHorizontalSkeleton = ((_b = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _b.enable_on_search) && ((_c = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _c.layout) === "horizontal" ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-filter-horizontal-skeleton", children: [1, 2, 3, 4, 5, 6, 7].map((index) => /* @__PURE__ */ jsxRuntimeExports.jsx(SkeletonLoading.Item, { width: "106px", height: "42px", rounded: "md", color: "grey-100", animationDuration: "13s" }, index)) }) : null;
2852
+ const filterVerticalSkeleton = ((_d = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _d.enable_on_search) && ((_e = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _e.layout) === "vertical" ? /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
2845
2853
  /* @__PURE__ */ jsxRuntimeExports.jsxs(
2846
2854
  "div",
2847
2855
  {
@@ -3057,7 +3065,7 @@ const SearchResultWidget = (props) => {
3057
3065
  )
3058
3066
  ] }) : null;
3059
3067
  const productGridSkeleton = /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
3060
- ((_e = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _e.show_total_products) && ((_f = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _f.layout) === "vertical" ? /* @__PURE__ */ jsxRuntimeExports.jsx(
3068
+ ((_f = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _f.show_total_products) && ((_g = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _g.layout) === "vertical" ? /* @__PURE__ */ jsxRuntimeExports.jsx(
3061
3069
  SkeletonLoading.Item,
3062
3070
  {
3063
3071
  width: "117px",
@@ -3073,7 +3081,7 @@ const SearchResultWidget = (props) => {
3073
3081
  /* @__PURE__ */ jsxRuntimeExports.jsx(SkeletonLoading.ProductGrid, { count: 12, type: "large" })
3074
3082
  ] });
3075
3083
  const searchAndSelectOptionSkeleton = /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sledge-instant-search__result-data-keyword", children: [
3076
- ((_g = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _g.show_total_products) && ((_h = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _h.layout) === "horizontal" ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-data-summary sledge-instant-search__result-data-summary-filter-horizontal", children: /* @__PURE__ */ jsxRuntimeExports.jsx(SkeletonLoading.Item, { width: "117px", height: "28px", color: "grey-100", rounded: "md", animationDuration: "13s" }) }) : null,
3084
+ ((_h = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _h.show_total_products) && ((_i = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _i.layout) === "horizontal" ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-data-summary sledge-instant-search__result-data-summary-filter-horizontal", children: /* @__PURE__ */ jsxRuntimeExports.jsx(SkeletonLoading.Item, { width: "117px", height: "28px", color: "grey-100", rounded: "md", animationDuration: "13s" }) }) : null,
3077
3085
  /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-data-keyword-form", children: /* @__PURE__ */ jsxRuntimeExports.jsx(
3078
3086
  SkeletonLoading.Item,
3079
3087
  {
@@ -3100,11 +3108,11 @@ const SearchResultWidget = (props) => {
3100
3108
  /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "sledge-instant-search__result-tab-total", children: total || 0 })
3101
3109
  ] }, index);
3102
3110
  }) }) : null }),
3103
- isFirstLoading && (layoutType == null ? void 0 : layoutType.length) ? /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
3111
+ isFirstLoading && (layoutType == null ? void 0 : layoutType.length) && !propsData ? /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
3104
3112
  tabSkeleton,
3105
3113
  filterHorizontalSkeleton,
3106
3114
  /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sledge-instant-search__result-wrapper sledge-instant-search__result-wrapper-skeleton", children: [
3107
- ((_i = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _i.enable_on_search) && ((_j = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _j.layout) === "vertical" ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-filter", children: filterVerticalSkeleton }) : null,
3115
+ ((_j = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _j.enable_on_search) && ((_k = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _k.layout) === "vertical" ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-filter", children: filterVerticalSkeleton }) : null,
3108
3116
  /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sledge-instant-search__result-data", children: [
3109
3117
  searchAndSelectOptionSkeleton,
3110
3118
  productGridSkeleton
@@ -3139,7 +3147,9 @@ const SearchResultWidget = (props) => {
3139
3147
  filterVerticalSkeleton,
3140
3148
  productGridSkeleton,
3141
3149
  searchAndSelectOptionSkeleton,
3142
- searchResultContainerRef
3150
+ searchResultContainerRef,
3151
+ data: propsData,
3152
+ settings
3143
3153
  })
3144
3154
  ] });
3145
3155
  };
@@ -3171,7 +3181,9 @@ const ResultProduct = (props) => {
3171
3181
  onAfterAddWishlist,
3172
3182
  onAfterRemoveWishlist,
3173
3183
  onAfterRenderProduct,
3174
- searchResultContainerRef
3184
+ searchResultContainerRef,
3185
+ data: propsData,
3186
+ settings
3175
3187
  } = props || {};
3176
3188
  const { keyword: queryKeyword = "" } = query || {};
3177
3189
  const { collectionId } = params || {};
@@ -3198,6 +3210,7 @@ const ResultProduct = (props) => {
3198
3210
  const [allowedFilter, setAllowedFilter] = React__default.useState([]);
3199
3211
  const [isFilterChanged, setIsFilterChanged] = React__default.useState(false);
3200
3212
  const [summaryText, setSummaryText] = React__default.useState(null);
3213
+ const [handleSearchResultFirstTime, setHandleSearchResultFirstTime] = React__default.useState(true);
3201
3214
  const previousState = usePrevious({ keyword, clickedLimitId, clickedFacets });
3202
3215
  const { color: filter_title_color, font_size: filter_title_font_size, font_weight: filter_title_font_weight, text_transform: filter_title_text_transform } = ((_c = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _c.title) || {};
3203
3216
  const {
@@ -3371,6 +3384,7 @@ const ResultProduct = (props) => {
3371
3384
  );
3372
3385
  };
3373
3386
  const handleSearchResult = async (isRefreshPage = false, onSearch = false) => {
3387
+ var _a2, _b2;
3374
3388
  if (!clickedTabIndexId)
3375
3389
  return;
3376
3390
  if (isRefreshPage)
@@ -3379,13 +3393,13 @@ const ResultProduct = (props) => {
3379
3393
  let detectClickedFacets = detectQueryStringFilter(clickedFacets);
3380
3394
  let response;
3381
3395
  let filters = Object.entries(detectClickedFacets).map((facet) => {
3382
- var _a2, _b2;
3396
+ var _a3, _b3;
3383
3397
  if ((facet == null ? void 0 : facet.length) && facet[1].length) {
3384
3398
  let value = facet[0];
3385
3399
  let isCustom = ["variants.inventory_quantity"].includes(value);
3386
3400
  let isRange = ["variants.price"].includes(value);
3387
3401
  let isArray = ["tags"].includes(value);
3388
- let items = Array.isArray(facet[1]) && ((_a2 = facet[1]) == null ? void 0 : _a2.length) > 1 ? isRange ? `${value} >= ${facet[1][0]} AND ${value} <= ${facet[1][1]}` : isArray ? `${value} IN [${facet[1]}]` : (_b2 = facet[1]) == null ? void 0 : _b2.map((item) => {
3402
+ let items = Array.isArray(facet[1]) && ((_a3 = facet[1]) == null ? void 0 : _a3.length) > 1 ? isRange ? `${value} >= ${facet[1][0]} AND ${value} <= ${facet[1][1]}` : isArray ? `${value} IN [${facet[1]}]` : (_b3 = facet[1]) == null ? void 0 : _b3.map((item) => {
3389
3403
  return isCustom ? `${value} ${item}` : `${value} = '${item}'`;
3390
3404
  }).join(" OR ") : isCustom ? `${value} ${facet[1]}` : isArray ? `${value} IN [${facet[1]}]` : `${value} = '${facet[1]}'`;
3391
3405
  return items;
@@ -3404,7 +3418,11 @@ const ResultProduct = (props) => {
3404
3418
  facets,
3405
3419
  filter: filters.join(" AND ")
3406
3420
  };
3407
- response = await search(clickedTabIndexId, body);
3421
+ if (propsData && handleSearchResultFirstTime) {
3422
+ response = (_b2 = (_a2 = propsData == null ? void 0 : propsData.result) == null ? void 0 : _a2.results) == null ? void 0 : _b2[0];
3423
+ setHandleSearchResultFirstTime(false);
3424
+ } else
3425
+ response = await search(clickedTabIndexId, body);
3408
3426
  handleSearchResultData(response, onSearch);
3409
3427
  };
3410
3428
  const handleAllowedFilter = () => {
@@ -3650,7 +3668,7 @@ const ResultProduct = (props) => {
3650
3668
  });
3651
3669
  const paginationComponent = /* @__PURE__ */ jsxRuntimeExports.jsx(Pagination, { currentPage, totalPage, totalResult: totalSearchResult, onChange: (page) => handlePageChange(page) });
3652
3670
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
3653
- ((_f = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _f.enable_on_search) && ((_g = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _g.layout) === "horizontal" ? /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: isFirstLoading ? filterHorizontalSkeleton : /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: isLoadingSetting ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-filter-horizontal-loading", children: filterHorizontalSkeleton }) : filterHorizontalComponents.every((currentValue) => !currentValue) ? null : /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sledge-instant-search__result-filter-horizontal-wrapper", children: [
3671
+ ((_f = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _f.enable_on_search) && ((_g = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _g.layout) === "horizontal" ? /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: isFirstLoading && !propsData ? filterHorizontalSkeleton : /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: isLoadingSetting && !propsData ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-filter-horizontal-loading", children: filterHorizontalSkeleton }) : filterHorizontalComponents.every((currentValue) => !currentValue) ? null : /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sledge-instant-search__result-filter-horizontal-wrapper", children: [
3654
3672
  /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-filter-horizontal", children: filterHorizontalComponents }),
3655
3673
  Boolean((dataClickedFacets == null ? void 0 : dataClickedFacets.length) && ((_h = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _h.show_refine_by_block)) ? /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: Boolean(dataClickedFacets.every((currentValue) => !currentValue)) ? null : /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sledge-instant-search__result-filter-horizontal", children: [
3656
3674
  /* @__PURE__ */ jsxRuntimeExports.jsx(
@@ -3728,7 +3746,7 @@ const ResultProduct = (props) => {
3728
3746
  value
3729
3747
  };
3730
3748
  }),
3731
- value: clickedSortId,
3749
+ value: clickedSortId || (settings == null ? void 0 : settings.default_sort),
3732
3750
  onChange: ({ label, value }) => handleChangeSort(label, value)
3733
3751
  }
3734
3752
  )
@@ -3781,7 +3799,9 @@ const ResultProduct = (props) => {
3781
3799
  onAfterAddToCart,
3782
3800
  onAfterAddWishlist,
3783
3801
  onAfterRemoveWishlist,
3784
- onAfterRenderProduct
3802
+ onAfterRenderProduct,
3803
+ dataWishlists: propsData == null ? void 0 : propsData.wishlists,
3804
+ dataReviews: propsData == null ? void 0 : propsData.reviews
3785
3805
  }
3786
3806
  ),
3787
3807
  paginationComponent