@sledge-app/react-instant-search 0.0.76 → 0.0.78

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.
@@ -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,9 +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;
2669
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2668
2670
  const { isRenderApp, isJsVersion } = React__default.useContext(SledgeContext);
2669
- 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;
2670
2672
  const { keyword: queryKeyword = "" } = query || {};
2671
2673
  const { collectionId } = params || {};
2672
2674
  let productCardsComponent = null;
@@ -2677,9 +2679,11 @@ const SearchResultWidget = (props) => {
2677
2679
  productCardsComponent = productCard;
2678
2680
  }
2679
2681
  });
2682
+ const searchParams = typeof document !== "undefined" ? new URLSearchParams((_a = document == null ? void 0 : document.location) == null ? void 0 : _a.search) : null;
2683
+ const [settings] = React__default.useState(typeof localStorage !== "undefined" ? (_b = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY.INSTANT_SEARCH_SETTING) || "{}")) == null ? void 0 : _b.data : {});
2680
2684
  const [isFirstLoading, setIsFirstLoading] = React__default.useState(true);
2681
2685
  const [isLoading, setIsLoading] = React__default.useState(true);
2682
- const [keyword, setKeyword] = React__default.useState("");
2686
+ const [keyword, setKeyword] = React__default.useState(searchParams == null ? void 0 : searchParams.get(queryKeyword));
2683
2687
  const [clickedTabIndexId, setClickedTabIndexId] = React__default.useState(null);
2684
2688
  const [clickedTabIndexName, setClickedTabIndexName] = React__default.useState();
2685
2689
  const [resultComponent, setResultComponent] = React__default.useState(/* @__PURE__ */ jsxRuntimeExports.jsx(ResultProduct, {}));
@@ -2695,6 +2699,7 @@ const SearchResultWidget = (props) => {
2695
2699
  const [colorSwatches, setColorSwatches] = React__default.useState([]);
2696
2700
  const [defaultSort, setDefaultSort] = React__default.useState("");
2697
2701
  const [showOutOfStock, setShowOutOfStock] = React__default.useState(false);
2702
+ const [initStatesFirstTime, setInitStatesFirstTime] = React__default.useState(true);
2698
2703
  const searchResultContainerRef = React__default.useRef(null);
2699
2704
  const previousState = usePrevious({ keyword });
2700
2705
  const handleChangeTabIndex = (id, name, e) => {
@@ -2742,7 +2747,11 @@ const SearchResultWidget = (props) => {
2742
2747
  };
2743
2748
  })
2744
2749
  };
2745
- response = await multiSearch(body);
2750
+ if (propsData && initStatesFirstTime) {
2751
+ response = propsData.result;
2752
+ setInitStatesFirstTime(false);
2753
+ } else
2754
+ response = await multiSearch(body);
2746
2755
  if (!response)
2747
2756
  return;
2748
2757
  const { results } = response;
@@ -2824,8 +2833,8 @@ const SearchResultWidget = (props) => {
2824
2833
  React__default.useEffect(() => {
2825
2834
  if (!(isRenderApp == null ? void 0 : isRenderApp.instantSearch))
2826
2835
  return;
2827
- const searchParams = new URLSearchParams(document.location.search);
2828
- setKeyword((searchParams == null ? void 0 : searchParams.get(queryKeyword)) || "");
2836
+ const searchParams2 = new URLSearchParams(document.location.search);
2837
+ setKeyword((searchParams2 == null ? void 0 : searchParams2.get(queryKeyword)) || "");
2829
2838
  handleSettings(localStorage.getItem(LOCAL_STORAGE_KEY.INSTANT_SEARCH_SETTING) || "");
2830
2839
  }, [isRenderApp]);
2831
2840
  React__default.useEffect(() => {
@@ -2839,8 +2848,8 @@ const SearchResultWidget = (props) => {
2839
2848
  }
2840
2849
  }, [keyword]);
2841
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;
2842
- 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;
2843
- 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 = ((_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) === "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 = ((_e = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _e.enable_on_search) && ((_f = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _f.layout) === "vertical" ? /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
2844
2853
  /* @__PURE__ */ jsxRuntimeExports.jsxs(
2845
2854
  "div",
2846
2855
  {
@@ -3056,7 +3065,7 @@ const SearchResultWidget = (props) => {
3056
3065
  )
3057
3066
  ] }) : null;
3058
3067
  const productGridSkeleton = /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
3059
- ((_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
+ ((_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) === "vertical" ? /* @__PURE__ */ jsxRuntimeExports.jsx(
3060
3069
  SkeletonLoading.Item,
3061
3070
  {
3062
3071
  width: "117px",
@@ -3072,7 +3081,7 @@ const SearchResultWidget = (props) => {
3072
3081
  /* @__PURE__ */ jsxRuntimeExports.jsx(SkeletonLoading.ProductGrid, { count: 12, type: "large" })
3073
3082
  ] });
3074
3083
  const searchAndSelectOptionSkeleton = /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sledge-instant-search__result-data-keyword", children: [
3075
- ((_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
+ ((_i = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _i.show_total_products) && ((_j = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _j.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,
3076
3085
  /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-data-keyword-form", children: /* @__PURE__ */ jsxRuntimeExports.jsx(
3077
3086
  SkeletonLoading.Item,
3078
3087
  {
@@ -3099,11 +3108,11 @@ const SearchResultWidget = (props) => {
3099
3108
  /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "sledge-instant-search__result-tab-total", children: total || 0 })
3100
3109
  ] }, index);
3101
3110
  }) }) : null }),
3102
- 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: [
3103
3112
  tabSkeleton,
3104
3113
  filterHorizontalSkeleton,
3105
3114
  /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sledge-instant-search__result-wrapper sledge-instant-search__result-wrapper-skeleton", children: [
3106
- ((_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
+ ((_k = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _k.enable_on_search) && ((_l = displaySettings == null ? void 0 : displaySettings.filter) == null ? void 0 : _l.layout) === "vertical" ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-filter", children: filterVerticalSkeleton }) : null,
3107
3116
  /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sledge-instant-search__result-data", children: [
3108
3117
  searchAndSelectOptionSkeleton,
3109
3118
  productGridSkeleton
@@ -3138,7 +3147,9 @@ const SearchResultWidget = (props) => {
3138
3147
  filterVerticalSkeleton,
3139
3148
  productGridSkeleton,
3140
3149
  searchAndSelectOptionSkeleton,
3141
- searchResultContainerRef
3150
+ searchResultContainerRef,
3151
+ data: propsData,
3152
+ settings
3142
3153
  })
3143
3154
  ] });
3144
3155
  };
@@ -3170,7 +3181,9 @@ const ResultProduct = (props) => {
3170
3181
  onAfterAddWishlist,
3171
3182
  onAfterRemoveWishlist,
3172
3183
  onAfterRenderProduct,
3173
- searchResultContainerRef
3184
+ searchResultContainerRef,
3185
+ data: propsData,
3186
+ settings
3174
3187
  } = props || {};
3175
3188
  const { keyword: queryKeyword = "" } = query || {};
3176
3189
  const { collectionId } = params || {};
@@ -3197,6 +3210,7 @@ const ResultProduct = (props) => {
3197
3210
  const [allowedFilter, setAllowedFilter] = React__default.useState([]);
3198
3211
  const [isFilterChanged, setIsFilterChanged] = React__default.useState(false);
3199
3212
  const [summaryText, setSummaryText] = React__default.useState(null);
3213
+ const [handleSearchResultFirstTime, setHandleSearchResultFirstTime] = React__default.useState(true);
3200
3214
  const previousState = usePrevious({ keyword, clickedLimitId, clickedFacets });
3201
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) || {};
3202
3216
  const {
@@ -3370,6 +3384,7 @@ const ResultProduct = (props) => {
3370
3384
  );
3371
3385
  };
3372
3386
  const handleSearchResult = async (isRefreshPage = false, onSearch = false) => {
3387
+ var _a2, _b2;
3373
3388
  if (!clickedTabIndexId)
3374
3389
  return;
3375
3390
  if (isRefreshPage)
@@ -3378,13 +3393,13 @@ const ResultProduct = (props) => {
3378
3393
  let detectClickedFacets = detectQueryStringFilter(clickedFacets);
3379
3394
  let response;
3380
3395
  let filters = Object.entries(detectClickedFacets).map((facet) => {
3381
- var _a2, _b2;
3396
+ var _a3, _b3;
3382
3397
  if ((facet == null ? void 0 : facet.length) && facet[1].length) {
3383
3398
  let value = facet[0];
3384
3399
  let isCustom = ["variants.inventory_quantity"].includes(value);
3385
3400
  let isRange = ["variants.price"].includes(value);
3386
3401
  let isArray = ["tags"].includes(value);
3387
- 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) => {
3388
3403
  return isCustom ? `${value} ${item}` : `${value} = '${item}'`;
3389
3404
  }).join(" OR ") : isCustom ? `${value} ${facet[1]}` : isArray ? `${value} IN [${facet[1]}]` : `${value} = '${facet[1]}'`;
3390
3405
  return items;
@@ -3403,7 +3418,11 @@ const ResultProduct = (props) => {
3403
3418
  facets,
3404
3419
  filter: filters.join(" AND ")
3405
3420
  };
3406
- 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);
3407
3426
  handleSearchResultData(response, onSearch);
3408
3427
  };
3409
3428
  const handleAllowedFilter = () => {
@@ -3649,7 +3668,7 @@ const ResultProduct = (props) => {
3649
3668
  });
3650
3669
  const paginationComponent = /* @__PURE__ */ jsxRuntimeExports.jsx(Pagination, { currentPage, totalPage, totalResult: totalSearchResult, onChange: (page) => handlePageChange(page) });
3651
3670
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
3652
- ((_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: [
3653
3672
  /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sledge-instant-search__result-filter-horizontal", children: filterHorizontalComponents }),
3654
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: [
3655
3674
  /* @__PURE__ */ jsxRuntimeExports.jsx(
@@ -3727,7 +3746,7 @@ const ResultProduct = (props) => {
3727
3746
  value
3728
3747
  };
3729
3748
  }),
3730
- value: clickedSortId,
3749
+ value: clickedSortId || (settings == null ? void 0 : settings.default_sort),
3731
3750
  onChange: ({ label, value }) => handleChangeSort(label, value)
3732
3751
  }
3733
3752
  )
@@ -3780,7 +3799,9 @@ const ResultProduct = (props) => {
3780
3799
  onAfterAddToCart,
3781
3800
  onAfterAddWishlist,
3782
3801
  onAfterRemoveWishlist,
3783
- onAfterRenderProduct
3802
+ onAfterRenderProduct,
3803
+ dataWishlists: propsData == null ? void 0 : propsData.wishlists,
3804
+ dataReviews: propsData == null ? void 0 : propsData.reviews
3784
3805
  }
3785
3806
  ),
3786
3807
  paginationComponent