@usereactify/search 3.10.0 → 3.10.3

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.
@@ -41,7 +41,7 @@ function getTrackEvents(event) {
41
41
  },
42
42
  ];
43
43
  break;
44
- case "viewProduct":
44
+ case "zeroResults":
45
45
  events = [
46
46
  {
47
47
  eventName,
@@ -49,7 +49,7 @@ function getTrackEvents(event) {
49
49
  },
50
50
  ];
51
51
  break;
52
- case "clickProduct":
52
+ case "viewProduct":
53
53
  events = [
54
54
  {
55
55
  eventName,
@@ -57,7 +57,7 @@ function getTrackEvents(event) {
57
57
  },
58
58
  ];
59
59
  break;
60
- case "viewPromotion":
60
+ case "clickProduct":
61
61
  events = [
62
62
  {
63
63
  eventName,
@@ -65,7 +65,7 @@ function getTrackEvents(event) {
65
65
  },
66
66
  ];
67
67
  break;
68
- case "clickPromotion":
68
+ case "viewPromotion":
69
69
  events = [
70
70
  {
71
71
  eventName,
@@ -73,7 +73,7 @@ function getTrackEvents(event) {
73
73
  },
74
74
  ];
75
75
  break;
76
- case "zeroResults":
76
+ case "clickPromotion":
77
77
  events = [
78
78
  {
79
79
  eventName,
@@ -11,6 +11,7 @@ declare type Context = {
11
11
  collection?: Collection;
12
12
  instantSearch: boolean;
13
13
  credentials: Credentials;
14
+ onRedirect?: (type: "redirect" | "search", url: string) => void;
14
15
  submitSearch: (localSearchQuery?: string) => void;
15
16
  setSearchQuery: (searchQuery: string) => void;
16
17
  setSortOption: (sortOptionId: string) => void;
@@ -29,6 +30,7 @@ declare type Props = {
29
30
  collection?: Collection;
30
31
  noReactiveBase?: boolean;
31
32
  instantSearch?: boolean;
33
+ onRedirect?: (type: "redirect" | "search", url: string) => void;
32
34
  credentials?: Credentials;
33
35
  theme?: Theme;
34
36
  /** Array of additional component IDs managed outside of Reactify Search */
package/dist/provider.js CHANGED
@@ -38,7 +38,7 @@ const Provider = (_a) => {
38
38
  exports.Provider = Provider;
39
39
  const ConfiguredProvider = (props) => {
40
40
  var _a, _b;
41
- const { index, config, children, collection, instantSearch, filterStackId, noReactiveBase, shopifyPermanentDomain, additionalComponentIds, } = props;
41
+ const { index, config, children, collection, instantSearch, filterStackId, noReactiveBase, shopifyPermanentDomain, additionalComponentIds, onRedirect, } = props;
42
42
  const credentials = (_a = props.credentials) !== null && _a !== void 0 ? _a : defaultCredentials;
43
43
  const theme = (_b = props.theme) !== null && _b !== void 0 ? _b : {
44
44
  typography: {
@@ -56,16 +56,36 @@ const ConfiguredProvider = (props) => {
56
56
  react_1.default.useEffect(() => {
57
57
  debug("props", props);
58
58
  }, [props]);
59
- // @todo make this hackable with a prop
60
- // https://gitlab.com/reactifyapps/reactify-search-frontend/-/issues/1
61
59
  const searchQueryFromURL = react_1.default.useMemo(() => {
62
60
  var _a;
63
61
  if (typeof window === "undefined")
64
62
  return undefined;
65
63
  const urlParams = new URLSearchParams(window.location.search);
66
64
  return (_a = urlParams.get("q")) !== null && _a !== void 0 ? _a : undefined;
67
- }, []);
65
+ }, [window.location.search]);
66
+ const searchSortFromURL = react_1.default.useMemo(() => {
67
+ var _a;
68
+ if (typeof window === "undefined")
69
+ return undefined;
70
+ const urlParams = new URLSearchParams(window.location.search);
71
+ return (_a = urlParams.get("sort")) !== null && _a !== void 0 ? _a : undefined;
72
+ }, [window.location.search]);
68
73
  const [searchQuery, setSearchQuery] = react_1.default.useState(searchQueryFromURL);
74
+ const { sortOption, sortOptions, setSortOption } = useSortState(config, collection);
75
+ react_1.default.useEffect(() => {
76
+ if (searchQueryFromURL && searchQuery !== searchQueryFromURL)
77
+ setSearchQuery(searchQueryFromURL);
78
+ }, [searchQueryFromURL]);
79
+ react_1.default.useEffect(() => {
80
+ if (searchSortFromURL && (sortOption === null || sortOption === void 0 ? void 0 : sortOption.id) !== searchSortFromURL) {
81
+ setSortOption(searchSortFromURL, true);
82
+ }
83
+ }, [searchSortFromURL]);
84
+ react_1.default.useEffect(() => {
85
+ if (window.location.search === "" && (sortOption === null || sortOption === void 0 ? void 0 : sortOption.id)) {
86
+ setSortOption(sortOptions[0].id, true);
87
+ }
88
+ }, [window.location.href]);
69
89
  const [showInstantSearchResults, setShowInstantSearchResults] = react_1.default.useState(false);
70
90
  const submitSearch = react_1.default.useCallback((localSearchQuery) => {
71
91
  const actualSearchQuery = localSearchQuery !== null && localSearchQuery !== void 0 ? localSearchQuery : searchQuery;
@@ -73,12 +93,17 @@ const ConfiguredProvider = (props) => {
73
93
  return;
74
94
  const redirect = config.redirects.find((redirect) => redirect.query.trim().toLowerCase() ===
75
95
  actualSearchQuery.trim().toLowerCase());
76
- if (redirect)
96
+ setShowInstantSearchResults(false);
97
+ if (onRedirect) {
98
+ onRedirect(redirect ? "redirect" : "search", redirect ? redirect.url : `/search?q=${actualSearchQuery}`);
99
+ }
100
+ else if (redirect) {
77
101
  window.location.href = redirect.url;
78
- else
102
+ }
103
+ else {
79
104
  window.location.href = `/search?q=${actualSearchQuery}`;
105
+ }
80
106
  }, [searchQuery]);
81
- const { sortOption, sortOptions, setSortOption } = useSortState(config, collection);
82
107
  // do not attempt to resolve a filter stack if in instantSearch mode
83
108
  const filterStack = instantSearch
84
109
  ? undefined
@@ -94,6 +119,7 @@ const ConfiguredProvider = (props) => {
94
119
  filterStack,
95
120
  sortOption,
96
121
  sortOptions,
122
+ onRedirect,
97
123
  submitSearch,
98
124
  setSortOption,
99
125
  setSearchQuery,
@@ -114,6 +140,7 @@ const ConfiguredProvider = (props) => {
114
140
  filterStack,
115
141
  sortOption,
116
142
  sortOptions,
143
+ onRedirect,
117
144
  submitSearch,
118
145
  instantSearch,
119
146
  additionalComponentIds,
@@ -142,9 +169,16 @@ const useSortState = (config, collection) => {
142
169
  }, [config, collection]);
143
170
  const [sortOptionState, setSortOptionState] = react_1.default.useState((_a = sortOptions[0]) === null || _a === void 0 ? void 0 : _a.id);
144
171
  const sortOption = react_1.default.useMemo(() => sortOptions.find(({ id }) => id === sortOptionState) || sortOptions[0], [sortOptions, sortOptionState]);
145
- const setSortOption = react_1.default.useCallback((sortOptionId) => {
172
+ const setSortOption = react_1.default.useCallback((sortOptionId, ignoreHistoryState = false) => {
146
173
  setSortOptionState(sortOptionId);
147
- }, []);
174
+ if (!ignoreHistoryState) {
175
+ const url = new URL(window.location.href);
176
+ url.searchParams.has("sort")
177
+ ? url.searchParams.set("sort", sortOptionId)
178
+ : url.searchParams.append("sort", sortOptionId);
179
+ window.history.pushState({}, "", url.toString());
180
+ }
181
+ }, [window.location.href]);
148
182
  return react_1.default.useMemo(() => ({ sortOptions, sortOption, setSortOption }), [sortOption, sortOptions, setSortOption]);
149
183
  };
150
184
  // resolve the "filter" in use for this view
@@ -161,6 +195,12 @@ const useFilterStack = (config, collection, filterStackId) => react_1.default.us
161
195
  if (filterStack)
162
196
  return filterStack;
163
197
  }
198
+ // automatically select filter stack by collection if provided
199
+ if (collection === null || collection === void 0 ? void 0 : collection.handle) {
200
+ const filterStack = config.filters.find((filter) => { var _a; return (_a = filter.collections) === null || _a === void 0 ? void 0 : _a.includes(collection.handle); });
201
+ if (filterStack)
202
+ return filterStack;
203
+ }
164
204
  const type = !!collection ? "collection" : "search";
165
205
  return ((_a = config.filters.find((filter) => type === filter.type)) !== null && _a !== void 0 ? _a : config.filters[0]);
166
206
  }, [config, collection]);
@@ -120,6 +120,7 @@ export interface Filter {
120
120
  pageSize: number;
121
121
  keywords: string[];
122
122
  defaultSort: string;
123
+ collections: string[];
123
124
  type: "search" | "collection";
124
125
  paginationType: "pagination" | "load_more" | "next_prev" | "infinite_scroll";
125
126
  inventoryVisibility: "show_all" | "hide_products" | "hide_variants" | "hide_all";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@usereactify/search",
3
3
  "description": "React UI library for Reactify Search",
4
- "version": "3.10.0",
4
+ "version": "3.10.3",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",