@usereactify/search 3.4.1 → 3.4.4
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/dist/hooks/useLiveConfig.d.ts +1 -2
- package/dist/hooks/useLiveConfig.js +37 -43
- package/dist/provider.d.ts +7 -1
- package/dist/provider.js +26 -6
- package/dist/result/ResultList.d.ts +0 -1
- package/dist/result/ResultList.js +1 -8
- package/dist/sensor/SensorSearch.js +3 -2
- package/package.json +1 -1
|
@@ -17,54 +17,48 @@ const react_1 = __importDefault(require("react"));
|
|
|
17
17
|
const debug = require("debug")("reactify-search:useLiveConfig");
|
|
18
18
|
// 5 minute cache expiry
|
|
19
19
|
const CACHE_EXPIRY = 5 * 60 * 1000;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// skip checking cache if url search param "nocache" is set
|
|
25
|
-
const skipCache = new URLSearchParams(window.location.href.split("?")[1]).get("nocache") !== null;
|
|
26
|
-
if (skipCache) {
|
|
27
|
-
debug("skipping cache");
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
const sessionConfig = JSON.parse((_a = window.sessionStorage.getItem("reactify-search:config")) !== null && _a !== void 0 ? _a : "null");
|
|
31
|
-
if (sessionConfig) {
|
|
32
|
-
const sessionConfigTtl = sessionConfig.expiresAt - Date.now();
|
|
33
|
-
debug(`found ${sessionConfigTtl > 0 ? "cached" : "expired"} config`);
|
|
34
|
-
if (sessionConfigTtl > 0) {
|
|
35
|
-
debug(`config expires in ${(sessionConfigTtl / 1000).toFixed()} seconds`);
|
|
36
|
-
return sessionConfig.config;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
20
|
+
// synchronously returns cached and non-expired config from session storage
|
|
21
|
+
function getCachedConfig() {
|
|
22
|
+
var _a;
|
|
23
|
+
if (typeof window === "undefined") {
|
|
39
24
|
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const json = yield fetch(`https://config.search.reactify.app/?shop=${shopifyPermanentDomain}`).then((response) => response.json());
|
|
56
|
-
setConfig(json.body);
|
|
57
|
-
setConfigLoading(false);
|
|
58
|
-
window.sessionStorage.setItem("reactify-search:config", JSON.stringify({
|
|
59
|
-
expiresAt: new Date().getTime() + CACHE_EXPIRY,
|
|
60
|
-
config: json.body,
|
|
61
|
-
}));
|
|
62
|
-
}))();
|
|
25
|
+
}
|
|
26
|
+
// skip checking cache if url search param "nocache" is set
|
|
27
|
+
const skipCache = new URLSearchParams(window.location.href.split("?")[1]).get("nocache") !==
|
|
28
|
+
null;
|
|
29
|
+
if (skipCache) {
|
|
30
|
+
debug("skipping cache");
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const sessionConfig = JSON.parse((_a = window.sessionStorage.getItem("reactify-search:config")) !== null && _a !== void 0 ? _a : "null");
|
|
34
|
+
if (sessionConfig) {
|
|
35
|
+
const sessionConfigTtl = sessionConfig.expiresAt - Date.now();
|
|
36
|
+
debug(`found ${sessionConfigTtl > 0 ? "cached" : "expired"} config`);
|
|
37
|
+
if (sessionConfigTtl > 0) {
|
|
38
|
+
debug(`config expires in ${(sessionConfigTtl / 1000).toFixed()} seconds`);
|
|
39
|
+
return sessionConfig.config;
|
|
63
40
|
}
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const useLiveConfig = (shopifyPermanentDomain) => {
|
|
45
|
+
const [config, setConfig] = react_1.default.useState(getCachedConfig());
|
|
46
|
+
react_1.default.useEffect(() => {
|
|
47
|
+
const cachedConfig = getCachedConfig();
|
|
48
|
+
if (cachedConfig)
|
|
49
|
+
return;
|
|
50
|
+
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
51
|
+
debug("fetching fresh config");
|
|
52
|
+
const json = yield fetch(`https://config.search.reactify.app/?shop=${shopifyPermanentDomain}`).then((response) => response.json());
|
|
53
|
+
setConfig(json.body);
|
|
54
|
+
window.sessionStorage.setItem("reactify-search:config", JSON.stringify({
|
|
55
|
+
expiresAt: new Date().getTime() + CACHE_EXPIRY,
|
|
56
|
+
config: json.body,
|
|
57
|
+
}));
|
|
58
|
+
}))();
|
|
64
59
|
}, [shopifyPermanentDomain]);
|
|
65
60
|
return {
|
|
66
61
|
config,
|
|
67
|
-
configLoading,
|
|
68
62
|
};
|
|
69
63
|
};
|
|
70
64
|
exports.useLiveConfig = useLiveConfig;
|
package/dist/provider.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ import type { Config, ConfigSort, ConfigFilter, ConfigCuration } from "./types/c
|
|
|
3
3
|
declare type Context = {
|
|
4
4
|
index: string;
|
|
5
5
|
config: Config;
|
|
6
|
-
configLoading: boolean;
|
|
7
6
|
shopifyPermanentDomain: string;
|
|
8
7
|
sortOption: ConfigSort | undefined;
|
|
9
8
|
sortOptions: ConfigSort[];
|
|
@@ -34,8 +33,15 @@ declare type Props = {
|
|
|
34
33
|
theme?: Theme;
|
|
35
34
|
/** Array of additional component IDs managed outside of Reactify Search */
|
|
36
35
|
additionalComponentIds?: string[];
|
|
36
|
+
/**
|
|
37
|
+
* Optional render function to display a component when the config is loading.
|
|
38
|
+
*/
|
|
39
|
+
renderBooting?: () => JSX.Element | null;
|
|
37
40
|
};
|
|
38
41
|
export declare const Provider: React.FC<Props>;
|
|
42
|
+
export declare const ConfiguredProvider: React.FC<Omit<Props, "renderBooting"> & {
|
|
43
|
+
config: Config;
|
|
44
|
+
}>;
|
|
39
45
|
export declare const useContext: () => Context;
|
|
40
46
|
export interface Collection {
|
|
41
47
|
id: number;
|
package/dist/provider.js
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
2
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
15
|
};
|
|
5
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.useContext = exports.Provider = void 0;
|
|
17
|
+
exports.useContext = exports.ConfiguredProvider = exports.Provider = void 0;
|
|
7
18
|
const react_1 = __importDefault(require("react"));
|
|
8
19
|
const UtilityAuthenticatedReactiveBase_1 = require("./utility/UtilityAuthenticatedReactiveBase");
|
|
9
20
|
const hooks_1 = require("./hooks");
|
|
@@ -14,9 +25,20 @@ const defaultCredentials = {
|
|
|
14
25
|
password: "password",
|
|
15
26
|
endpoint: "https://api.search.reactify.app",
|
|
16
27
|
};
|
|
17
|
-
const Provider = (
|
|
28
|
+
const Provider = (_a) => {
|
|
29
|
+
var { renderBooting } = _a, props = __rest(_a, ["renderBooting"]);
|
|
30
|
+
const { config } = (0, hooks_1.useLiveConfig)(props.shopifyPermanentDomain);
|
|
31
|
+
if (!config) {
|
|
32
|
+
if (renderBooting)
|
|
33
|
+
return renderBooting();
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
return react_1.default.createElement(exports.ConfiguredProvider, Object.assign({}, props, { config: config }));
|
|
37
|
+
};
|
|
38
|
+
exports.Provider = Provider;
|
|
39
|
+
const ConfiguredProvider = (props) => {
|
|
18
40
|
var _a, _b;
|
|
19
|
-
const { index,
|
|
41
|
+
const { index, config, children, collection, instantSearch, filterStackId, noReactiveBase, shopifyPermanentDomain, additionalComponentIds, } = props;
|
|
20
42
|
const credentials = (_a = props.credentials) !== null && _a !== void 0 ? _a : defaultCredentials;
|
|
21
43
|
const theme = (_b = props.theme) !== null && _b !== void 0 ? _b : {
|
|
22
44
|
typography: {
|
|
@@ -34,7 +56,6 @@ const Provider = (props) => {
|
|
|
34
56
|
react_1.default.useEffect(() => {
|
|
35
57
|
debug("props", props);
|
|
36
58
|
}, [props]);
|
|
37
|
-
const { config, configLoading } = (0, hooks_1.useLiveConfig)(props.shopifyPermanentDomain);
|
|
38
59
|
// @todo make this hackable with a prop
|
|
39
60
|
// https://gitlab.com/reactifyapps/reactify-search-frontend/-/issues/1
|
|
40
61
|
const searchQueryFromURL = react_1.default.useMemo(() => {
|
|
@@ -66,7 +87,6 @@ const Provider = (props) => {
|
|
|
66
87
|
const contextValue = react_1.default.useMemo(() => ({
|
|
67
88
|
index,
|
|
68
89
|
config,
|
|
69
|
-
configLoading,
|
|
70
90
|
shopifyPermanentDomain,
|
|
71
91
|
collection,
|
|
72
92
|
credentials,
|
|
@@ -109,7 +129,7 @@ const Provider = (props) => {
|
|
|
109
129
|
return (react_1.default.createElement(Context.Provider, { value: contextValue },
|
|
110
130
|
react_1.default.createElement(UtilityAuthenticatedReactiveBase_1.UtilityAuthenticatedReactiveBase, null, children)));
|
|
111
131
|
};
|
|
112
|
-
exports.
|
|
132
|
+
exports.ConfiguredProvider = ConfiguredProvider;
|
|
113
133
|
const useContext = () => react_1.default.useContext(Context);
|
|
114
134
|
exports.useContext = useContext;
|
|
115
135
|
const useSortState = (config, collection) => {
|
|
@@ -14,7 +14,6 @@ declare type Props = {
|
|
|
14
14
|
pageSize?: number;
|
|
15
15
|
gridColumns?: number;
|
|
16
16
|
listClassName?: string;
|
|
17
|
-
renderBooting?: () => JSX.Element | null;
|
|
18
17
|
renderLoading?: () => JSX.Element | null;
|
|
19
18
|
renderNoResults?: () => JSX.Element | null;
|
|
20
19
|
renderResultCard?: Parameters<typeof ResultCard>[0]["render"];
|
|
@@ -11,7 +11,6 @@ const ResultPagination_1 = require("./ResultPagination");
|
|
|
11
11
|
const ResultCardCallout_1 = require("./ResultCardCallout");
|
|
12
12
|
const ResultLoadMoreButton_1 = require("./ResultLoadMoreButton");
|
|
13
13
|
const ResultPaginationNextPrev_1 = require("./ResultPaginationNextPrev");
|
|
14
|
-
const provider_1 = require("../provider");
|
|
15
14
|
const hooks_1 = require("../hooks");
|
|
16
15
|
const elastic_1 = require("../types/elastic");
|
|
17
16
|
const ResultList = (props) => {
|
|
@@ -23,10 +22,9 @@ const ResultList = (props) => {
|
|
|
23
22
|
};
|
|
24
23
|
exports.ResultList = ResultList;
|
|
25
24
|
const ResultListInner = (props) => {
|
|
26
|
-
const { gridColumns, renderError, renderAfter, renderBefore,
|
|
25
|
+
const { gridColumns, renderError, renderAfter, renderBefore, renderLoading, listClassName, renderResults, renderNoResults, renderResultCard, renderLoadMoreButton, renderResultCardCallout, reactivesearchResultProps, } = props;
|
|
27
26
|
const filterStack = (0, hooks_1.useFilterStack)();
|
|
28
27
|
const { instantSearch } = (0, hooks_1.useSearch)();
|
|
29
|
-
const { configLoading } = (0, provider_1.useContext)();
|
|
30
28
|
const initialSearchHasRun = react_1.default.useMemo(() => "undefined" !==
|
|
31
29
|
typeof reactivesearchResultProps.resultStats.numberOfResults, [reactivesearchResultProps]);
|
|
32
30
|
const styleProp = react_1.default.useMemo(() => ({
|
|
@@ -34,11 +32,6 @@ const ResultListInner = (props) => {
|
|
|
34
32
|
gridTemplateColumns: `repeat(${gridColumns !== null && gridColumns !== void 0 ? gridColumns : 4}, minmax(0, 1fr))`,
|
|
35
33
|
}), [gridColumns]);
|
|
36
34
|
const resultProps = react_1.default.useMemo(() => (Object.assign(Object.assign({}, reactivesearchResultProps), { products: reactivesearchResultProps.data.filter((document) => elastic_1.ElasticDocumentType.Product === document.type || !document.type), callouts: reactivesearchResultProps.data.filter((document) => elastic_1.ElasticDocumentType.Callout === document.type) })), [reactivesearchResultProps]);
|
|
37
|
-
if (configLoading) {
|
|
38
|
-
if (renderBooting)
|
|
39
|
-
return renderBooting();
|
|
40
|
-
return react_1.default.createElement("div", null, "Loading...");
|
|
41
|
-
}
|
|
42
35
|
if (reactivesearchResultProps.error) {
|
|
43
36
|
if (renderError)
|
|
44
37
|
return renderError({ error: reactivesearchResultProps.error });
|
|
@@ -11,9 +11,10 @@ const SensorSearch = () => {
|
|
|
11
11
|
const { fields } = (0, hooks_1.useConfig)();
|
|
12
12
|
const { searchQuery } = (0, hooks_1.useSearch)();
|
|
13
13
|
// ignore search fields only set for instant search
|
|
14
|
-
const searchFields = react_1.default.useMemo(() => fields.filter((field) => ["always_search", "search_page"].includes(field.searchType)), []);
|
|
15
|
-
if (!searchQuery)
|
|
14
|
+
const searchFields = react_1.default.useMemo(() => fields.filter((field) => ["always_search", "search_page"].includes(field.searchType)), [fields]);
|
|
15
|
+
if (!searchQuery || !searchFields.length) {
|
|
16
16
|
return null;
|
|
17
|
+
}
|
|
17
18
|
return (react_1.default.createElement(react_1.default.Fragment, null,
|
|
18
19
|
react_1.default.createElement(DataSearch_1.default, { fuzziness: 1, queryFormat: "and", autosuggest: false, value: searchQuery, componentId: "SensorSearch", style: { display: "none" }, dataField: searchFields.map((field) => field.field), fieldWeights: searchFields.map((field) => field.importance), customQuery: (value, props) => {
|
|
19
20
|
const { dataField, fieldWeights } = props;
|