@nosto/nosto-react 0.4.1 → 0.4.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.
- package/README.md +5 -2
- package/dist/{index.es.client.js → index.es.js} +123 -164
- package/dist/index.umd.js +9 -0
- package/package.json +15 -10
- package/src/components/Nosto404.tsx +47 -0
- package/src/components/{Category/index.client.tsx → NostoCategory.tsx} +18 -39
- package/src/components/NostoCheckout.tsx +47 -0
- package/src/components/{Home/index.client.tsx → NostoHome.tsx} +17 -36
- package/src/components/NostoOrder.tsx +55 -0
- package/src/components/NostoOther.tsx +46 -0
- package/src/components/{Placement/index.client.tsx → NostoPlacement.tsx} +5 -8
- package/src/components/{Product/index.client.tsx → NostoProduct.tsx} +37 -81
- package/src/components/NostoProvider.tsx +220 -0
- package/src/components/{Search/index.client.tsx → NostoSearch.tsx} +18 -39
- package/src/components/{Session/index.client.tsx → NostoSession.tsx} +14 -17
- package/src/components/context.ts +55 -0
- package/src/components/index.ts +14 -0
- package/src/index.ts +3 -0
- package/src/types.ts +112 -97
- package/src/utils/compare.ts +9 -9
- package/src/utils/hooks.ts +28 -8
- package/src/utils/object.ts +10 -11
- package/src/utils/snakeize.ts +11 -11
- package/dist/index.umd.client.js +0 -9
- package/src/components/Checkout/index.client.tsx +0 -66
- package/src/components/Fohofo/index.client.tsx +0 -66
- package/src/components/Order/index.client.tsx +0 -72
- package/src/components/Other/index.client.tsx +0 -64
- package/src/components/Provider/context.client.ts +0 -45
- package/src/components/Provider/index.client.tsx +0 -222
- package/src/index.client.ts +0 -33
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import React, { useEffect, isValidElement, useState, useRef } from "react";
|
|
2
|
-
import { NostoContext } from "./context.client";
|
|
3
|
-
import { createRoot } from "react-dom/client";
|
|
4
|
-
import { Recommendation } from "../../types";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* This widget is what we call the Nosto root widget, which is responsible for adding the actual Nosto script and the JS API stub.
|
|
8
|
-
* This widget wraps all other React Nosto widgets.
|
|
9
|
-
*
|
|
10
|
-
* ```
|
|
11
|
-
* <NostoProvider account="your-nosto-account-id" recommendationComponent={<NostoSlot />}>
|
|
12
|
-
* <App />
|
|
13
|
-
* </NostoProvider>
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* **Note:** the component also accepts a prop to configure the host `host="connect.nosto.com"`.
|
|
17
|
-
* In advanced use-cases, the need to configure the host may surface.
|
|
18
|
-
*
|
|
19
|
-
* In order to implement client-side rendering, the requires a designated component to render the recommendations provided by Nosto.
|
|
20
|
-
* This component should be capable of processing the JSON response received from our backend.
|
|
21
|
-
* Notice the `recommendationComponent` prop passed to `<NostoProvider>` above.
|
|
22
|
-
*
|
|
23
|
-
* Learn more [here](https://github.com/Nosto/shopify-hydrogen/blob/main/README.md#client-side-rendering-for-recommendations) and see a [live example](https://github.com/Nosto/shopify-hydrogen-demo) on our demo store.
|
|
24
|
-
*
|
|
25
|
-
* @group Essential Functions
|
|
26
|
-
*/
|
|
27
|
-
export default function NostoProvider(props: {
|
|
28
|
-
/**
|
|
29
|
-
* Indicates merchant id
|
|
30
|
-
*/
|
|
31
|
-
account: string;
|
|
32
|
-
/**
|
|
33
|
-
* Indicates currency
|
|
34
|
-
*/
|
|
35
|
-
currentVariation?: string;
|
|
36
|
-
/**
|
|
37
|
-
* Indicates an url of a server
|
|
38
|
-
*/
|
|
39
|
-
host?: string;
|
|
40
|
-
children: React.ReactElement;
|
|
41
|
-
/**
|
|
42
|
-
* Indicates if merchant uses multiple currencies
|
|
43
|
-
*/
|
|
44
|
-
multiCurrency?: boolean;
|
|
45
|
-
/**
|
|
46
|
-
* Recommendation component which holds nostoRecommendation object
|
|
47
|
-
*/
|
|
48
|
-
recommendationComponent?: any;
|
|
49
|
-
/**
|
|
50
|
-
* Enables Shopify markets with language and market id
|
|
51
|
-
*/
|
|
52
|
-
shopifyMarkets?: {
|
|
53
|
-
language?: string;
|
|
54
|
-
marketId?: string | number;
|
|
55
|
-
}
|
|
56
|
-
}): JSX.Element {
|
|
57
|
-
let {
|
|
58
|
-
account,
|
|
59
|
-
currentVariation = "",
|
|
60
|
-
multiCurrency = false,
|
|
61
|
-
host,
|
|
62
|
-
children,
|
|
63
|
-
recommendationComponent,
|
|
64
|
-
shopifyMarkets
|
|
65
|
-
} = props;
|
|
66
|
-
const [clientScriptLoadedState, setClientScriptLoadedState] =
|
|
67
|
-
React.useState(false);
|
|
68
|
-
const clientScriptLoaded = React.useMemo(
|
|
69
|
-
() => clientScriptLoadedState,
|
|
70
|
-
[clientScriptLoadedState]
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
//Pass currentVariation as empty string if multiCurrency is disabled
|
|
74
|
-
currentVariation = multiCurrency ? currentVariation : "";
|
|
75
|
-
|
|
76
|
-
// Set responseMode for loading campaigns:
|
|
77
|
-
const responseMode = isValidElement(recommendationComponent)
|
|
78
|
-
? "JSON_ORIGINAL"
|
|
79
|
-
: "HTML";
|
|
80
|
-
|
|
81
|
-
// RecommendationComponent for client-side rendering:
|
|
82
|
-
function RecommendationComponentWrapper(props: {
|
|
83
|
-
nostoRecommendation: Recommendation;
|
|
84
|
-
}) {
|
|
85
|
-
return React.cloneElement(recommendationComponent!, {
|
|
86
|
-
nostoRecommendation: props.nostoRecommendation,
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// custom hook for rendering campaigns (CSR/SSR):
|
|
91
|
-
const [pageType, setPageType] = useState("");
|
|
92
|
-
const useRenderCampaigns: any = function (type: string = "") {
|
|
93
|
-
const placementRefs: any = useRef({});
|
|
94
|
-
useEffect(() => {
|
|
95
|
-
if (pageType != type) {
|
|
96
|
-
setPageType(type);
|
|
97
|
-
}
|
|
98
|
-
}, []);
|
|
99
|
-
|
|
100
|
-
const pageTypeUpdated = type == pageType;
|
|
101
|
-
|
|
102
|
-
function renderCampaigns(
|
|
103
|
-
data: {
|
|
104
|
-
recommendations: any;
|
|
105
|
-
campaigns: {
|
|
106
|
-
recommendations: {
|
|
107
|
-
[key: string]: any;
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
},
|
|
111
|
-
api: {
|
|
112
|
-
placements: {
|
|
113
|
-
injectCampaigns: (recommendations: any) => void;
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
) {
|
|
117
|
-
if (responseMode == "HTML") {
|
|
118
|
-
// inject content campaigns as usual:
|
|
119
|
-
api.placements.injectCampaigns(data.recommendations);
|
|
120
|
-
} else {
|
|
121
|
-
// render recommendation component into placements:
|
|
122
|
-
const recommendations = data.campaigns.recommendations;
|
|
123
|
-
for (const key in recommendations) {
|
|
124
|
-
let recommendation = recommendations[key];
|
|
125
|
-
let placementSelector = "#" + key;
|
|
126
|
-
let placement: Function = () =>
|
|
127
|
-
document.querySelector(placementSelector);
|
|
128
|
-
|
|
129
|
-
if (!!placement()) {
|
|
130
|
-
if (!placementRefs.current[key])
|
|
131
|
-
placementRefs.current[key] = createRoot(placement());
|
|
132
|
-
const root = placementRefs.current[key];
|
|
133
|
-
root.render(
|
|
134
|
-
<RecommendationComponentWrapper
|
|
135
|
-
nostoRecommendation={recommendation}
|
|
136
|
-
></RecommendationComponentWrapper>
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return { renderCampaigns, pageTypeUpdated };
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
useEffect(() => {
|
|
147
|
-
if (!window.nostojs) {
|
|
148
|
-
window.nostojs = (cb: Function) => {
|
|
149
|
-
(window.nostojs.q = window.nostojs.q || []).push(cb);
|
|
150
|
-
};
|
|
151
|
-
window.nostojs((api) => api.setAutoLoad(false));
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (!document.querySelectorAll("[nosto-client-script]").length && !shopifyMarkets) {
|
|
155
|
-
const script = document.createElement("script");
|
|
156
|
-
script.type = "text/javascript";
|
|
157
|
-
script.src = "//" + (host || "connect.nosto.com") + "/include/" + account;
|
|
158
|
-
script.async = true;
|
|
159
|
-
script.setAttribute("nosto-client-script", "");
|
|
160
|
-
|
|
161
|
-
script.onload = () => {
|
|
162
|
-
if (typeof jest !== "undefined") {
|
|
163
|
-
window.nosto?.reload({
|
|
164
|
-
site: "localhost",
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
setClientScriptLoadedState(true);
|
|
168
|
-
};
|
|
169
|
-
document.body.appendChild(script);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
//Enable Shopify markets functionality:
|
|
173
|
-
if (!!shopifyMarkets) {
|
|
174
|
-
|
|
175
|
-
const existingScript = document.querySelector("[nosto-client-script]");
|
|
176
|
-
const nostoSandbox = document.querySelector('#nosto-sandbox');
|
|
177
|
-
|
|
178
|
-
if (!existingScript || existingScript?.getAttribute('nosto-language') != shopifyMarkets?.language || existingScript?.getAttribute('nosto-market-id') != shopifyMarkets?.marketId) {
|
|
179
|
-
if (clientScriptLoadedState) { setClientScriptLoadedState(false) };
|
|
180
|
-
|
|
181
|
-
existingScript?.parentNode?.removeChild(existingScript)
|
|
182
|
-
nostoSandbox?.parentNode?.removeChild(nostoSandbox)
|
|
183
|
-
|
|
184
|
-
const script = document.createElement("script");
|
|
185
|
-
script.type = "text/javascript";
|
|
186
|
-
script.src = "//" + (host || "connect.nosto.com") + `/script/shopify/market/nosto.js?merchant=${account}&market=${shopifyMarkets.marketId || ''}&locale=${shopifyMarkets?.language?.toLowerCase() || ''}`
|
|
187
|
-
script.async = true;
|
|
188
|
-
script.setAttribute("nosto-client-script", "");
|
|
189
|
-
script.setAttribute("nosto-language", shopifyMarkets?.language || '');
|
|
190
|
-
script.setAttribute("nosto-market-id", String(shopifyMarkets?.marketId));
|
|
191
|
-
|
|
192
|
-
script.onload = () => {
|
|
193
|
-
if (typeof jest !== "undefined") {
|
|
194
|
-
window.nosto?.reload({
|
|
195
|
-
site: "localhost",
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
setClientScriptLoadedState(true);
|
|
199
|
-
};
|
|
200
|
-
document.body.appendChild(script);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
}, [clientScriptLoadedState, shopifyMarkets]);
|
|
206
|
-
|
|
207
|
-
return (
|
|
208
|
-
<NostoContext.Provider
|
|
209
|
-
value={{
|
|
210
|
-
account,
|
|
211
|
-
clientScriptLoaded,
|
|
212
|
-
currentVariation,
|
|
213
|
-
responseMode,
|
|
214
|
-
recommendationComponent,
|
|
215
|
-
useRenderCampaigns,
|
|
216
|
-
pageType,
|
|
217
|
-
}}
|
|
218
|
-
>
|
|
219
|
-
{children}
|
|
220
|
-
</NostoContext.Provider>
|
|
221
|
-
);
|
|
222
|
-
}
|
package/src/index.client.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
Buyer, Cart, Customer, Item, Product, Purchase, Recommendation, SKU
|
|
3
|
-
} from "./types";
|
|
4
|
-
// noinspection JSUnusedGlobalSymbols
|
|
5
|
-
export { default as Nosto404 } from "./components/Fohofo/index.client";
|
|
6
|
-
// noinspection JSUnusedGlobalSymbols
|
|
7
|
-
export { default as NostoOther } from "./components/Other/index.client";
|
|
8
|
-
// noinspection JSUnusedGlobalSymbols
|
|
9
|
-
export { default as NostoCheckout } from "./components/Checkout/index.client";
|
|
10
|
-
// noinspection JSUnusedGlobalSymbols
|
|
11
|
-
export { default as NostoProduct } from "./components/Product/index.client";
|
|
12
|
-
// noinspection JSUnusedGlobalSymbols
|
|
13
|
-
export { default as NostoCategory } from "./components/Category/index.client";
|
|
14
|
-
// noinspection JSUnusedGlobalSymbols
|
|
15
|
-
export { default as NostoSearch } from "./components/Search/index.client";
|
|
16
|
-
// noinspection JSUnusedGlobalSymbols
|
|
17
|
-
export { default as NostoOrder } from "./components/Order/index.client";
|
|
18
|
-
// noinspection JSUnusedGlobalSymbols
|
|
19
|
-
export { default as NostoHome } from "./components/Home/index.client";
|
|
20
|
-
// noinspection JSUnusedGlobalSymbols
|
|
21
|
-
export { default as NostoPlacement } from "./components/Placement/index.client";
|
|
22
|
-
// noinspection JSUnusedGlobalSymbols
|
|
23
|
-
export { default as NostoProvider } from "./components/Provider/index.client";
|
|
24
|
-
// noinspection JSUnusedGlobalSymbols
|
|
25
|
-
export {
|
|
26
|
-
NostoContext,
|
|
27
|
-
useNostoContext,
|
|
28
|
-
} from "./components/Provider/context.client";
|
|
29
|
-
export type {
|
|
30
|
-
NostoContextType,
|
|
31
|
-
} from "./components/Provider/context.client";
|
|
32
|
-
// noinspection JSUnusedGlobalSymbols
|
|
33
|
-
export { default as NostoSession } from "./components/Session/index.client";
|