@usereactify/search 5.50.1 → 5.52.0
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.
- package/CHANGELOG.md +14 -0
- package/dist/package.json +1 -1
- package/dist/src/components/ReactifySearchProvider/ReactifySearchProvider.js +5 -0
- package/dist/src/components/ReactifySearchProvider/ReactifySearchProvider.js.map +1 -1
- package/dist/src/hooks/useReactifySearchContext.d.ts +3 -1
- package/dist/src/hooks/useReactifySearchContext.js.map +1 -1
- package/dist/src/hooks/useSearch.d.ts +5 -0
- package/dist/src/hooks/useSearch.js +32 -0
- package/dist/src/hooks/useSearch.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [5.52.0](https://bitbucket.org/usereactify/reactify-search-ui/compare/release-v5.51.0...release-v5.52.0) (2024-09-13)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* add submitSearchQuery method to useSearch hook ([6619d6e](https://bitbucket.org/usereactify/reactify-search-ui/commit/6619d6e80fba88896f10562868f8f27ac758ab78))
|
|
11
|
+
|
|
12
|
+
## [5.51.0](https://bitbucket.org/usereactify/reactify-search-ui/compare/release-v5.50.1...release-v5.51.0) (2024-08-21)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* add searchRedirect field to check for current redirects ([5c4077c](https://bitbucket.org/usereactify/reactify-search-ui/commit/5c4077c5725b6e895ea3f49ee8aacec74bfba5ae))
|
|
18
|
+
|
|
5
19
|
### [5.50.1](https://bitbucket.org/usereactify/reactify-search-ui/compare/release-v5.50.0...release-v5.50.1) (2024-03-14)
|
|
6
20
|
|
|
7
21
|
|
package/dist/package.json
CHANGED
|
@@ -78,6 +78,9 @@ const ReactifySearchInner = (props) => {
|
|
|
78
78
|
return (_a = urlParams.get("index")) !== null && _a !== void 0 ? _a : undefined;
|
|
79
79
|
}, [typeof window !== "undefined" && window.location.search]);
|
|
80
80
|
const [searchTerm, setSearchTerm] = react_1.default.useState(searchTermFromURL !== null && searchTermFromURL !== void 0 ? searchTermFromURL : "");
|
|
81
|
+
const searchRedirect = react_1.default.useMemo(() => {
|
|
82
|
+
return config === null || config === void 0 ? void 0 : config.redirects.find((redirect) => redirect.query.trim().toLowerCase() === searchTerm.trim().toLowerCase());
|
|
83
|
+
}, [config === null || config === void 0 ? void 0 : config.redirects, searchTerm]);
|
|
81
84
|
const [showInstantSearchResults, setShowInstantSearchResults] = react_1.default.useState(false);
|
|
82
85
|
const [sortOption, setSortOption] = react_1.default.useState(searchSortFromURL !== null && searchSortFromURL !== void 0 ? searchSortFromURL : "");
|
|
83
86
|
const [results, setResults] = react_1.default.useState([]);
|
|
@@ -105,6 +108,7 @@ const ReactifySearchInner = (props) => {
|
|
|
105
108
|
setSearchTerm: setSearchTerm,
|
|
106
109
|
showInstantSearchResults: showInstantSearchResults,
|
|
107
110
|
setShowInstantSearchResults: setShowInstantSearchResults,
|
|
111
|
+
searchRedirect: searchRedirect,
|
|
108
112
|
},
|
|
109
113
|
sortby: {
|
|
110
114
|
sortOption: sortOption,
|
|
@@ -122,6 +126,7 @@ const ReactifySearchInner = (props) => {
|
|
|
122
126
|
props,
|
|
123
127
|
searchTerm,
|
|
124
128
|
showInstantSearchResults,
|
|
129
|
+
searchRedirect,
|
|
125
130
|
sortOption,
|
|
126
131
|
results,
|
|
127
132
|
resultStats,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ReactifySearchProvider.js","sourceRoot":"","sources":["../../../../src/components/ReactifySearchProvider/ReactifySearchProvider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA0B;AAC1B,iCAAiC;AACjC,gDAAkC;AAElC,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gBACtB,WAAW,EAAE,WAAW;gBACxB,cAAc,EAAE,cAAc;aAC/B;SACF,CAAC;IACJ,CAAC,EAAE;QACD,MAAM;QACN,KAAK;QACL,UAAU;QACV,wBAAwB;QACxB,UAAU;QACV,OAAO;QACP,WAAW;KACZ,CAAC,CAAC;IAEH,eAAK,CAAC,IAAI,CAAC,wBAAwB,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;IAE9D,IAAI,CAAC,YAAY,EAAE;QACjB,IAAI,KAAK,CAAC,aAAa,EAAE;YACvB,OAAO,KAAK,CAAC,aAAa,EAAE,CAAC;SAC9B;QACD,OAAO,IAAI,CAAC;KACb;IAED,OAAO,CACL,8BAAC,6BAAqB,CAAC,QAAQ,IAAC,KAAK,EAAE,YAAY;QACjD,8BAAC,6CAAgC,QAC9B,KAAK,CAAC,QAAQ,CACkB,CACJ,CAClC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import React from \"react\";\n// @ts-expect-error missing types\nimport * as GaGtag from \"ga-gtag\";\n\nimport pkg from \"../../../package.json\";\n\nimport { ReactifySearchContext, useConfig } from \"../../hooks\";\nimport { UtilityAuthenticatedReactiveBase } from \"../../components\";\nimport { debug } from \"../../utility\";\nimport { Config, ReactivesearchResultProps } from \"../../types\";\n\nexport type ReactifySearchProviderProps = {\n /** Standard react children */\n children?: React.ReactNode | undefined;\n /** The search area the provider will be used in */\n mode: \"search\" | \"collection\" | \"instant-search\";\n /** Shopify store domain used to resolve the site configuration */\n shopifyPermanentDomain: string;\n /** Market ID used to change merchandising based on market e.g. 7494533198 */\n market?: string;\n /** Callback function for redirects, suitable for headless sites to avoid full page refresh */\n onRedirect?: (type: \"redirect\" | \"search\", url: string) => void;\n /** Render method to display a component when the config is loading */\n renderBooting?: () => JSX.Element | null;\n /** Advanced usage: Override the default Reactify Search id (for telemetry) */\n clientId?: string;\n /** Advanced usage: Override the default Filters selection logic */\n filtersHandle?: string;\n /** Advanced usage: Array of additional component IDs managed outside of Reactify Search */\n additionalComponentIds?: Array<string>;\n /** Advanced usage: Override the default Elasticsearch index */\n index?: string;\n /** Advanced usage: Override the default Reactify Search config (for multi-instance stores) */\n configId?: string;\n /** Advanced usage: Override the default Elasticsearch credentials */\n credentials?: string;\n /** Advanced usage: Override the default ReactiveBase theme */\n theme?: Record<string, unknown>;\n /** Advanced usage: Fields to include in the Elasticsearch response e.g. [\"title\"] */\n includeFields?: Array<string>;\n /** Advanced usage: Fields to exclude from the Elasticsearch response e.g. [\"variant_skus\", \"*price*\"] */\n excludeFields?: Array<string>;\n /** Advanced usage: Flags are used to enable or disable specific features within Reactify Search */\n flags?: Record<string, boolean>;\n /** Advanced usage: Server content for SSR */\n query?: Record<string, string>;\n preload?: {\n config: Config;\n state: unknown;\n }\n} & (\n | {\n mode: \"search\";\n }\n | {\n mode: \"collection\";\n /** Collection object that includes the handle, used to find curations */\n collectionHandle: string;\n }\n | {\n mode: \"instant-search\";\n }\n);\n\nexport const ReactifySearchProvider: React.FC<ReactifySearchProviderProps> = (\n props\n) => {\n React.useEffect(() => {\n debug.log(\"ReactifySearchProvider\", \"props\", props);\n\n GaGtag.install(\"G-DV00Z0X5VP\", {\n cookie_prefix: \"_rs\",\n user_properties: {\n shop: props.shopifyPermanentDomain,\n version: pkg.version,\n },\n });\n }, [props]);\n\n return <ReactifySearchInner {...props} />;\n};\n\nconst ReactifySearchInner: React.FC<ReactifySearchProviderProps> = (props) => {\n const { config } = useConfig(props.shopifyPermanentDomain, props.preload?.config);\n\n const theme = props.theme ?? {\n typography: {\n fontFamily: \"inherit\",\n fontSize: \"inherit\",\n },\n };\n\n const searchTermFromURL = React.useMemo(() => {\n if (typeof window === \"undefined\") {\n return;\n }\n const urlParams = new URLSearchParams(window.location.search);\n return urlParams.get(\"q\") ?? undefined;\n }, [typeof window !== \"undefined\" && window.location.search]);\n\n const searchSortFromURL = React.useMemo(() => {\n if (typeof window === \"undefined\") {\n return props.query?.sort;\n }\n const urlParams = new URLSearchParams(window.location.search);\n return urlParams.get(\"sort\") ?? undefined;\n }, [typeof window !== \"undefined\" && window.location.search]);\n\n const indexFromURL = React.useMemo(() => {\n if (typeof window === \"undefined\") {\n return;\n }\n const urlParams = new URLSearchParams(window.location.search);\n return urlParams.get(\"index\") ?? undefined;\n }, [typeof window !== \"undefined\" && window.location.search]);\n\n const [searchTerm, setSearchTerm] = React.useState<string>(\n searchTermFromURL ?? \"\"\n );\n const [showInstantSearchResults, setShowInstantSearchResults] =\n React.useState<boolean>(false);\n const [sortOption, setSortOption] = React.useState<string>(\n searchSortFromURL ?? \"\"\n );\n\n const [results, setResults] = React.useState<\n ReactivesearchResultProps[\"data\"]\n >([]);\n const [resultStats, setResultStats] =\n React.useState<ReactivesearchResultProps[\"resultStats\"]>();\n\n React.useEffect(() => {\n if (searchTermFromURL && searchTerm !== searchTermFromURL) {\n setSearchTerm(searchTermFromURL);\n }\n }, [searchTermFromURL]);\n\n React.useEffect(() => {\n if (searchSortFromURL && sortOption !== searchSortFromURL) {\n setSortOption(searchSortFromURL);\n }\n }, [searchSortFromURL]);\n\n const contextValue = React.useMemo(() => {\n if (!config) {\n return;\n }\n\n return {\n config: config,\n options: {\n ...props,\n clientId: props.clientId ?? \"theme\",\n index: props.index ?? indexFromURL ?? config.index,\n filtersHandle: props.filtersHandle,\n credentials: props.credentials,\n theme: theme,\n },\n search: {\n searchTerm: searchTerm,\n setSearchTerm: setSearchTerm,\n showInstantSearchResults: showInstantSearchResults,\n setShowInstantSearchResults: setShowInstantSearchResults,\n },\n sortby: {\n sortOption: sortOption,\n setSortOption: setSortOption,\n },\n results: {\n results: results,\n setResults: setResults,\n resultStats: resultStats,\n setResultStats: setResultStats,\n },\n };\n }, [\n config,\n props,\n searchTerm,\n showInstantSearchResults,\n sortOption,\n results,\n resultStats,\n ]);\n\n debug.hook(\"ReactifySearchProvider\", \"context\", contextValue);\n\n if (!contextValue) {\n if (props.renderBooting) {\n return props.renderBooting();\n }\n return null;\n }\n\n return (\n <ReactifySearchContext.Provider value={contextValue}>\n <UtilityAuthenticatedReactiveBase>\n {props.children}\n </UtilityAuthenticatedReactiveBase>\n </ReactifySearchContext.Provider>\n );\n};\n"]}
|
|
1
|
+
{"version":3,"file":"ReactifySearchProvider.js","sourceRoot":"","sources":["../../../../src/components/ReactifySearchProvider/ReactifySearchProvider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA0B;AAC1B,iCAAiC;AACjC,gDAAkC;AAElC,yEAAwC;AAExC,uCAA+D;AAC/D,iDAAoE;AACpE,2CAAsC;AAwD/B,MAAM,sBAAsB,GAA0C,CAC3E,KAAK,EACL,EAAE;IACF,eAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,eAAK,CAAC,GAAG,CAAC,wBAAwB,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;QAEpD,MAAM,CAAC,OAAO,CAAC,cAAc,EAAE;YAC7B,aAAa,EAAE,KAAK;YACpB,eAAe,EAAE;gBACf,IAAI,EAAE,KAAK,CAAC,sBAAsB;gBAClC,OAAO,EAAE,sBAAG,CAAC,OAAO;aACrB;SACF,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO,8BAAC,mBAAmB,oBAAK,KAAK,EAAI,CAAC;AAC5C,CAAC,CAAC;AAhBW,QAAA,sBAAsB,0BAgBjC;AAEF,MAAM,mBAAmB,GAA0C,CAAC,KAAK,EAAE,EAAE;;IAC3E,MAAM,EAAE,MAAM,EAAE,GAAG,IAAA,iBAAS,EAAC,KAAK,CAAC,sBAAsB,EAAE,MAAA,KAAK,CAAC,OAAO,0CAAE,MAAM,CAAC,CAAC;IAElF,MAAM,KAAK,GAAG,MAAA,KAAK,CAAC,KAAK,mCAAI;QAC3B,UAAU,EAAE;YACV,UAAU,EAAE,SAAS;YACrB,QAAQ,EAAE,SAAS;SACpB;KACF,CAAC;IAEF,MAAM,iBAAiB,GAAG,eAAK,CAAC,OAAO,CAAC,GAAG,EAAE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,CAClC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import React from \"react\";\n// @ts-expect-error missing types\nimport * as GaGtag from \"ga-gtag\";\n\nimport pkg from \"../../../package.json\";\n\nimport { ReactifySearchContext, useConfig } from \"../../hooks\";\nimport { UtilityAuthenticatedReactiveBase } from \"../../components\";\nimport { debug } from \"../../utility\";\nimport { Config, ConfigRedirect, ReactivesearchResultProps } from \"../../types\";\n\nexport type ReactifySearchProviderProps = {\n /** Standard react children */\n children?: React.ReactNode | undefined;\n /** The search area the provider will be used in */\n mode: \"search\" | \"collection\" | \"instant-search\";\n /** Shopify store domain used to resolve the site configuration */\n shopifyPermanentDomain: string;\n /** Market ID used to change merchandising based on market e.g. 7494533198 */\n market?: string;\n /** Callback function for redirects, suitable for headless sites to avoid full page refresh */\n onRedirect?: (type: \"redirect\" | \"search\", url: string) => void;\n /** Render method to display a component when the config is loading */\n renderBooting?: () => JSX.Element | null;\n /** Advanced usage: Override the default Reactify Search id (for telemetry) */\n clientId?: string;\n /** Advanced usage: Override the default Filters selection logic */\n filtersHandle?: string;\n /** Advanced usage: Array of additional component IDs managed outside of Reactify Search */\n additionalComponentIds?: Array<string>;\n /** Advanced usage: Override the default Elasticsearch index */\n index?: string;\n /** Advanced usage: Override the default Reactify Search config (for multi-instance stores) */\n configId?: string;\n /** Advanced usage: Override the default Elasticsearch credentials */\n credentials?: string;\n /** Advanced usage: Override the default ReactiveBase theme */\n theme?: Record<string, unknown>;\n /** Advanced usage: Fields to include in the Elasticsearch response e.g. [\"title\"] */\n includeFields?: Array<string>;\n /** Advanced usage: Fields to exclude from the Elasticsearch response e.g. [\"variant_skus\", \"*price*\"] */\n excludeFields?: Array<string>;\n /** Advanced usage: Flags are used to enable or disable specific features within Reactify Search */\n flags?: Record<string, boolean>;\n /** Advanced usage: Server content for SSR */\n query?: Record<string, string>;\n preload?: {\n config: Config;\n state: unknown;\n }\n} & (\n | {\n mode: \"search\";\n }\n | {\n mode: \"collection\";\n /** Collection object that includes the handle, used to find curations */\n collectionHandle: string;\n }\n | {\n mode: \"instant-search\";\n }\n);\n\nexport const ReactifySearchProvider: React.FC<ReactifySearchProviderProps> = (\n props\n) => {\n React.useEffect(() => {\n debug.log(\"ReactifySearchProvider\", \"props\", props);\n\n GaGtag.install(\"G-DV00Z0X5VP\", {\n cookie_prefix: \"_rs\",\n user_properties: {\n shop: props.shopifyPermanentDomain,\n version: pkg.version,\n },\n });\n }, [props]);\n\n return <ReactifySearchInner {...props} />;\n};\n\nconst ReactifySearchInner: React.FC<ReactifySearchProviderProps> = (props) => {\n const { config } = useConfig(props.shopifyPermanentDomain, props.preload?.config);\n\n const theme = props.theme ?? {\n typography: {\n fontFamily: \"inherit\",\n fontSize: \"inherit\",\n },\n };\n\n const searchTermFromURL = React.useMemo(() => {\n if (typeof window === \"undefined\") {\n return;\n }\n const urlParams = new URLSearchParams(window.location.search);\n return urlParams.get(\"q\") ?? undefined;\n }, [typeof window !== \"undefined\" && window.location.search]);\n\n const searchSortFromURL = React.useMemo(() => {\n if (typeof window === \"undefined\") {\n return props.query?.sort;\n }\n const urlParams = new URLSearchParams(window.location.search);\n return urlParams.get(\"sort\") ?? undefined;\n }, [typeof window !== \"undefined\" && window.location.search]);\n\n const indexFromURL = React.useMemo(() => {\n if (typeof window === \"undefined\") {\n return;\n }\n const urlParams = new URLSearchParams(window.location.search);\n return urlParams.get(\"index\") ?? undefined;\n }, [typeof window !== \"undefined\" && window.location.search]);\n\n const [searchTerm, setSearchTerm] = React.useState<string>(\n searchTermFromURL ?? \"\"\n );\n const searchRedirect = React.useMemo<ConfigRedirect | undefined>(() => {\n return config?.redirects.find((redirect) => redirect.query.trim().toLowerCase() === searchTerm.trim().toLowerCase());\n }, [config?.redirects, searchTerm]);\n const [showInstantSearchResults, setShowInstantSearchResults] =\n React.useState<boolean>(false);\n const [sortOption, setSortOption] = React.useState<string>(\n searchSortFromURL ?? \"\"\n );\n\n const [results, setResults] = React.useState<\n ReactivesearchResultProps[\"data\"]\n >([]);\n const [resultStats, setResultStats] =\n React.useState<ReactivesearchResultProps[\"resultStats\"]>();\n\n React.useEffect(() => {\n if (searchTermFromURL && searchTerm !== searchTermFromURL) {\n setSearchTerm(searchTermFromURL);\n }\n }, [searchTermFromURL]);\n\n React.useEffect(() => {\n if (searchSortFromURL && sortOption !== searchSortFromURL) {\n setSortOption(searchSortFromURL);\n }\n }, [searchSortFromURL]);\n\n const contextValue = React.useMemo(() => {\n if (!config) {\n return;\n }\n\n return {\n config: config,\n options: {\n ...props,\n clientId: props.clientId ?? \"theme\",\n index: props.index ?? indexFromURL ?? config.index,\n filtersHandle: props.filtersHandle,\n credentials: props.credentials,\n theme: theme,\n },\n search: {\n searchTerm: searchTerm,\n setSearchTerm: setSearchTerm,\n showInstantSearchResults: showInstantSearchResults,\n setShowInstantSearchResults: setShowInstantSearchResults,\n searchRedirect: searchRedirect,\n },\n sortby: {\n sortOption: sortOption,\n setSortOption: setSortOption,\n },\n results: {\n results: results,\n setResults: setResults,\n resultStats: resultStats,\n setResultStats: setResultStats,\n },\n };\n }, [\n config,\n props,\n searchTerm,\n showInstantSearchResults,\n searchRedirect,\n sortOption,\n results,\n resultStats,\n ]);\n\n debug.hook(\"ReactifySearchProvider\", \"context\", contextValue);\n\n if (!contextValue) {\n if (props.renderBooting) {\n return props.renderBooting();\n }\n return null;\n }\n\n return (\n <ReactifySearchContext.Provider value={contextValue}>\n <UtilityAuthenticatedReactiveBase>\n {props.children}\n </UtilityAuthenticatedReactiveBase>\n </ReactifySearchContext.Provider>\n );\n};\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import { Config, ReactivesearchResultProps } from "../types";
|
|
2
|
+
import { Config, ConfigRedirect, ReactivesearchResultProps } from "../types";
|
|
3
3
|
import { Flags } from "./useFlags";
|
|
4
4
|
export declare type ReactifySearchMode = "search" | "collection" | "instant-search";
|
|
5
5
|
export declare const ReactifySearchContext: React.Context<{
|
|
@@ -36,6 +36,7 @@ export declare const ReactifySearchContext: React.Context<{
|
|
|
36
36
|
setSearchTerm: React.Dispatch<React.SetStateAction<string>>;
|
|
37
37
|
showInstantSearchResults: boolean;
|
|
38
38
|
setShowInstantSearchResults: React.Dispatch<React.SetStateAction<boolean>>;
|
|
39
|
+
searchRedirect: ConfigRedirect | undefined;
|
|
39
40
|
};
|
|
40
41
|
sortby: {
|
|
41
42
|
sortOption: string;
|
|
@@ -82,6 +83,7 @@ export declare const useReactifySearchContext: () => {
|
|
|
82
83
|
setSearchTerm: React.Dispatch<React.SetStateAction<string>>;
|
|
83
84
|
showInstantSearchResults: boolean;
|
|
84
85
|
setShowInstantSearchResults: React.Dispatch<React.SetStateAction<boolean>>;
|
|
86
|
+
searchRedirect: ConfigRedirect | undefined;
|
|
85
87
|
};
|
|
86
88
|
sortby: {
|
|
87
89
|
sortOption: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useReactifySearchContext.js","sourceRoot":"","sources":["../../../src/hooks/useReactifySearchContext.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAOb,QAAA,qBAAqB,GAAG,eAAK,CAAC,aAAa,
|
|
1
|
+
{"version":3,"file":"useReactifySearchContext.js","sourceRoot":"","sources":["../../../src/hooks/useReactifySearchContext.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAOb,QAAA,qBAAqB,GAAG,eAAK,CAAC,aAAa,CA6DtD,SAAS,CAAC,CAAC;AAEN,MAAM,wBAAwB,GAAG,GAAG,EAAE,CAC3C,eAAK,CAAC,UAAU,CAAC,6BAAqB,CAAE,CAAC;AAD9B,QAAA,wBAAwB,4BACM","sourcesContent":["import React from \"react\";\n\nimport { Config, ConfigRedirect, ReactivesearchResultProps } from \"../types\";\nimport { Flags } from \"./useFlags\";\n\nexport type ReactifySearchMode = \"search\" | \"collection\" | \"instant-search\";\n\nexport const ReactifySearchContext = React.createContext<\n | {\n config: Config;\n options: {\n clientId: string;\n mode: ReactifySearchMode;\n index: string;\n shopifyPermanentDomain: string;\n market?: string;\n filtersHandle?: string;\n credentials?: string;\n onRedirect?: (type: \"redirect\" | \"search\", url: string) => void;\n theme: Record<string, unknown>;\n additionalComponentIds?: Array<string>;\n includeFields?: Array<string>;\n excludeFields?: Array<string>;\n flags?: Record<Flags, boolean>;\n query?: Record<string, string>;\n preload?: {\n config: Config;\n state: unknown;\n };\n } & (\n | {\n mode: \"search\";\n }\n | {\n mode: \"collection\";\n collectionHandle: string;\n }\n | {\n mode: \"instant-search\";\n }\n );\n search: {\n searchTerm: string;\n setSearchTerm: React.Dispatch<React.SetStateAction<string>>;\n showInstantSearchResults: boolean;\n setShowInstantSearchResults: React.Dispatch<\n React.SetStateAction<boolean>\n >;\n searchRedirect: ConfigRedirect | undefined;\n };\n sortby: {\n sortOption: string;\n setSortOption: React.Dispatch<React.SetStateAction<string>>;\n };\n results: {\n results: ReactivesearchResultProps[\"data\"];\n setResults: React.Dispatch<\n React.SetStateAction<ReactivesearchResultProps[\"data\"]>\n >;\n resultStats?: ReactivesearchResultProps[\"resultStats\"];\n setResultStats: React.Dispatch<\n React.SetStateAction<\n ReactivesearchResultProps[\"resultStats\"] | undefined\n >\n >;\n };\n }\n | undefined\n>(undefined);\n\nexport const useReactifySearchContext = () =>\n React.useContext(ReactifySearchContext)!;\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
+
import { ConfigRedirect, ElasticProduct } from "../types";
|
|
2
3
|
export declare const useSearch: () => {
|
|
3
4
|
/** The current search term */
|
|
4
5
|
searchTerm: string;
|
|
@@ -6,8 +7,12 @@ export declare const useSearch: () => {
|
|
|
6
7
|
setSearchTerm: (searchTerm?: string | undefined) => void;
|
|
7
8
|
/** Function for navigating to the search page, includes logic for redirects */
|
|
8
9
|
submitSearchTerm: (searchTerm?: string | undefined) => void;
|
|
10
|
+
/** Function for retrieving search results for an opensearch query */
|
|
11
|
+
submitSearchQuery: (searchBody: any, searchHeaders: Record<string, string>) => Promise<Array<ElasticProduct>>;
|
|
9
12
|
/** Used to determine if instant search results should be displayed */
|
|
10
13
|
showInstantSearchResults: boolean;
|
|
11
14
|
/** Used to manually set the "showInstantSearchResults" value */
|
|
12
15
|
setShowInstantSearchResults: React.Dispatch<React.SetStateAction<boolean>>;
|
|
16
|
+
/** Contains any matching redirect for the current search term */
|
|
17
|
+
searchRedirect: ConfigRedirect | undefined;
|
|
13
18
|
};
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
13
|
};
|
|
@@ -9,6 +18,27 @@ const hooks_1 = require("../hooks");
|
|
|
9
18
|
const utility_1 = require("../utility");
|
|
10
19
|
const useSearch = () => {
|
|
11
20
|
const { options, config, search } = (0, hooks_1.useReactifySearchContext)();
|
|
21
|
+
const handleSubmitSearchQuery = react_1.default.useCallback((searchBody, searchHeaders) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
var _a, _b;
|
|
23
|
+
utility_1.debug.log("useSearch", "submitSearchQuery[body]", searchBody);
|
|
24
|
+
utility_1.debug.log("useSearch", "submitSearchQuery[headers]", searchHeaders);
|
|
25
|
+
const response = yield fetch(`${config.endpoint}/${config.index}/_search`, {
|
|
26
|
+
method: "POST",
|
|
27
|
+
body: JSON.stringify(searchBody),
|
|
28
|
+
headers: searchHeaders,
|
|
29
|
+
}).then((response) => {
|
|
30
|
+
utility_1.debug.log("useSearch", "submitSearchQuery[status]", response.status);
|
|
31
|
+
return response.json();
|
|
32
|
+
}).catch((error) => {
|
|
33
|
+
utility_1.debug.log("useSearch", "submitSearchQuery[error]", error);
|
|
34
|
+
return error;
|
|
35
|
+
});
|
|
36
|
+
utility_1.debug.log("useSearch", "submitSearchQuery[response]", response);
|
|
37
|
+
if (((_b = (_a = response === null || response === void 0 ? void 0 : response.hits) === null || _a === void 0 ? void 0 : _a.hits) === null || _b === void 0 ? void 0 : _b.length) > 0) {
|
|
38
|
+
return response.hits.hits.map((item) => item._source);
|
|
39
|
+
}
|
|
40
|
+
return [];
|
|
41
|
+
}), [config.endpoint, config.index]);
|
|
12
42
|
const handleSubmitSearchTerm = react_1.default.useCallback((searchTerm = search.searchTerm) => {
|
|
13
43
|
utility_1.debug.log("useSearch", "submitSearchTerm[query]", searchTerm);
|
|
14
44
|
if (!searchTerm) {
|
|
@@ -40,8 +70,10 @@ const useSearch = () => {
|
|
|
40
70
|
searchTerm: search.searchTerm,
|
|
41
71
|
setSearchTerm: handleSetSearchTerm,
|
|
42
72
|
submitSearchTerm: handleSubmitSearchTerm,
|
|
73
|
+
submitSearchQuery: handleSubmitSearchQuery,
|
|
43
74
|
showInstantSearchResults: !!search.showInstantSearchResults && !!search.searchTerm,
|
|
44
75
|
setShowInstantSearchResults: search.setShowInstantSearchResults,
|
|
76
|
+
searchRedirect: search.searchRedirect,
|
|
45
77
|
};
|
|
46
78
|
};
|
|
47
79
|
exports.useSearch = useSearch;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSearch.js","sourceRoot":"","sources":["../../../src/hooks/useSearch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useSearch.js","sourceRoot":"","sources":["../../../src/hooks/useSearch.ts"],"names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AE,CAAC;SACtE;IACH,CAAC,EACD,CAAC,MAAM,CAAC,UAAU,CAAC,CACpB,CAAC;IAEF,MAAM,mBAAmB,GAAG,eAAK,CAAC,WAAW,CAC3C,CAAC,aAAqB,EAAE,EAAE,EAAE;QAC1B,eAAK,CAAC,GAAG,CAAC,WAAW,EAAE,2BAA2B,EAAE,UAAU,CAAC,CAAC;QAEhE,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;IACnC,CAAC,EACD,CAAC,MAAM,CAAC,UAAU,CAAC,CACpB,CAAC;IAEF,OAAO;QACL,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,aAAa,EAAE,mBAAmB;QAClC,gBAAgB,EAAE,sBAAsB;QACxC,iBAAiB,EAAE,uBAAuB;QAC1C,wBAAwB,EAAE,CAAC,CAAC,MAAM,CAAC,wBAAwB,IAAI,CAAC,CAAC,MAAM,CAAC,UAAU;QAClF,2BAA2B,EAAE,MAAM,CAAC,2BAA2B;QAC/D,cAAc,EAAE,MAAM,CAAC,cAAc;KACtC,CAAC;AACJ,CAAC,CAAC;AAtGW,QAAA,SAAS,aAsGpB","sourcesContent":["import React from \"react\";\n\nimport { useReactifySearchContext } from \"../hooks\";\nimport { debug } from \"../utility\";\nimport { ConfigRedirect, ElasticProduct } from \"../types\";\n\nexport const useSearch = (): {\n /** The current search term */\n searchTerm: string;\n /** Function for changing the current search term */\n setSearchTerm: (searchTerm?: string) => void;\n /** Function for navigating to the search page, includes logic for redirects */\n submitSearchTerm: (searchTerm?: string) => void;\n /** Function for retrieving search results for an opensearch query */\n submitSearchQuery: (searchBody: any, searchHeaders: Record<string, string>) => Promise<Array<ElasticProduct>>;\n /** Used to determine if instant search results should be displayed */\n showInstantSearchResults: boolean;\n /** Used to manually set the \"showInstantSearchResults\" value */\n setShowInstantSearchResults: React.Dispatch<React.SetStateAction<boolean>>;\n /** Contains any matching redirect for the current search term */\n searchRedirect: ConfigRedirect | undefined;\n} => {\n const { options, config, search } = useReactifySearchContext();\n\n const handleSubmitSearchQuery = React.useCallback(\n async (searchBody: any, searchHeaders: Record<string, string>) => {\n debug.log(\"useSearch\", \"submitSearchQuery[body]\", searchBody);\n debug.log(\"useSearch\", \"submitSearchQuery[headers]\", searchHeaders);\n \n const response = await fetch(`${config.endpoint}/${config.index}/_search`, {\n method: \"POST\",\n body: JSON.stringify(searchBody),\n headers: searchHeaders,\n }).then((response) => {\n debug.log(\"useSearch\", \"submitSearchQuery[status]\", response.status);\n \n return response.json();\n }).catch((error) => {\n debug.log(\"useSearch\", \"submitSearchQuery[error]\", error);\n\n return error;\n })\n\n debug.log(\"useSearch\", \"submitSearchQuery[response]\", response);\n\n if (response?.hits?.hits?.length > 0) {\n return response.hits.hits.map((item: { _source: ElasticProduct; }) => item._source);\n }\n\n return [];\n },\n [config.endpoint, config.index]\n );\n\n const handleSubmitSearchTerm = React.useCallback(\n (searchTerm: string = search.searchTerm) => {\n debug.log(\"useSearch\", \"submitSearchTerm[query]\", searchTerm);\n\n if (!searchTerm) {\n return;\n }\n\n const redirect = config.redirects.find(\n (redirect) =>\n redirect.query.trim().toLowerCase() ===\n searchTerm.trim().toLowerCase()\n );\n\n if (redirect) {\n debug.log(\"useSearch\", \"submitSearchTerm[redirect]\", redirect);\n }\n\n search.setShowInstantSearchResults(false);\n\n if (options.onRedirect) {\n options.onRedirect(\n redirect ? \"redirect\" : \"search\",\n redirect\n ? redirect.url\n : `/search?q=${encodeURIComponent(searchTerm)}`\n );\n } else if (redirect) {\n window.location.href = redirect.url;\n } else {\n window.location.href = `/search?q=${encodeURIComponent(searchTerm)}`;\n }\n },\n [search.searchTerm]\n );\n\n const handleSetSearchTerm = React.useCallback(\n (searchTerm: string = \"\") => {\n debug.log(\"useSearch\", \"setSearchTerm[searchTerm]\", searchTerm);\n\n search.setSearchTerm(searchTerm);\n },\n [search.searchTerm]\n );\n\n return {\n searchTerm: search.searchTerm,\n setSearchTerm: handleSetSearchTerm,\n submitSearchTerm: handleSubmitSearchTerm,\n submitSearchQuery: handleSubmitSearchQuery,\n showInstantSearchResults: !!search.showInstantSearchResults && !!search.searchTerm,\n setShowInstantSearchResults: search.setShowInstantSearchResults,\n searchRedirect: search.searchRedirect,\n };\n};\n"]}
|