@nosto/nosto-react 0.4.1 → 0.5.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/README.md +5 -2
- package/dist/index.d.ts +519 -0
- package/dist/index.es.js +1117 -0
- package/dist/index.umd.js +25 -0
- package/package.json +20 -13
- 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.es.client.js +0 -660
- 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,6 +1,6 @@
|
|
|
1
|
-
import { Product } from "
|
|
2
|
-
import { useNostoContext } from "
|
|
3
|
-
import {
|
|
1
|
+
import { Product } from "../types"
|
|
2
|
+
import { useNostoContext } from "./context"
|
|
3
|
+
import { useNostoApi } from "../utils/hooks"
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* The NostoProduct component must be used to personalise the product page.
|
|
@@ -24,45 +24,29 @@ import { useDeepCompareEffect } from "../../utils/hooks";
|
|
|
24
24
|
* </div>
|
|
25
25
|
* ```
|
|
26
26
|
*
|
|
27
|
-
* @group
|
|
27
|
+
* @group Components
|
|
28
28
|
*/
|
|
29
29
|
export default function NostoProduct(props: {
|
|
30
|
-
product: string
|
|
31
|
-
tagging?: Product
|
|
32
|
-
placements?: string[]
|
|
33
|
-
})
|
|
34
|
-
const { product, tagging } = props
|
|
35
|
-
const {
|
|
36
|
-
clientScriptLoaded,
|
|
37
|
-
currentVariation,
|
|
38
|
-
responseMode,
|
|
39
|
-
recommendationComponent,
|
|
40
|
-
useRenderCampaigns,
|
|
41
|
-
} = useNostoContext();
|
|
30
|
+
product: string
|
|
31
|
+
tagging?: Product
|
|
32
|
+
placements?: string[]
|
|
33
|
+
}) {
|
|
34
|
+
const { product, tagging, placements } = props
|
|
35
|
+
const { recommendationComponent, useRenderCampaigns } = useNostoContext()
|
|
42
36
|
|
|
43
|
-
const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("product")
|
|
37
|
+
const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("product")
|
|
44
38
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}, [
|
|
60
|
-
clientScriptLoaded,
|
|
61
|
-
currentVariation,
|
|
62
|
-
product,
|
|
63
|
-
recommendationComponent,
|
|
64
|
-
pageTypeUpdated,
|
|
65
|
-
]);
|
|
39
|
+
useNostoApi(
|
|
40
|
+
async (api) => {
|
|
41
|
+
const data = await api.defaultSession()
|
|
42
|
+
.viewProduct(product)
|
|
43
|
+
.setPlacements(placements || api.placements.getPlacements())
|
|
44
|
+
.load()
|
|
45
|
+
renderCampaigns(data, api)
|
|
46
|
+
},
|
|
47
|
+
[product, recommendationComponent, pageTypeUpdated],
|
|
48
|
+
{ deep: true }
|
|
49
|
+
)
|
|
66
50
|
|
|
67
51
|
return (
|
|
68
52
|
<>
|
|
@@ -70,41 +54,23 @@ export default function NostoProduct(props: {
|
|
|
70
54
|
product
|
|
71
55
|
</div>
|
|
72
56
|
<div className="nosto_product" style={{ display: "none" }}>
|
|
73
|
-
{tagging?.variationId &&
|
|
74
|
-
<span className="variation_id">{tagging.variationId}</span>
|
|
75
|
-
)}
|
|
57
|
+
{tagging?.variationId && <span className="variation_id">{tagging.variationId}</span>}
|
|
76
58
|
{product && <span className="product_id">{product}</span>}
|
|
77
59
|
{tagging?.name && <span className="name">{tagging.name}</span>}
|
|
78
60
|
{tagging?.url && <span className="url">{tagging.url.toString()}</span>}
|
|
79
|
-
{tagging?.imageUrl && (
|
|
80
|
-
|
|
81
|
-
)}
|
|
82
|
-
{tagging?.availability && (
|
|
83
|
-
<span className="availability">{tagging.availability}</span>
|
|
84
|
-
)}
|
|
61
|
+
{tagging?.imageUrl && <span className="image_url">{tagging.imageUrl.toString()}</span>}
|
|
62
|
+
{tagging?.availability && <span className="availability">{tagging.availability}</span>}
|
|
85
63
|
{tagging?.price && <span className="price">{tagging.price}</span>}
|
|
86
|
-
{tagging?.listPrice &&
|
|
87
|
-
<span className="list_price">{tagging.listPrice}</span>
|
|
88
|
-
)}
|
|
64
|
+
{tagging?.listPrice && <span className="list_price">{tagging.listPrice}</span>}
|
|
89
65
|
{tagging?.priceCurrencyCode && (
|
|
90
|
-
<span className="price_currency_code">
|
|
91
|
-
{tagging.priceCurrencyCode}
|
|
92
|
-
</span>
|
|
66
|
+
<span className="price_currency_code">{tagging.priceCurrencyCode}</span>
|
|
93
67
|
)}
|
|
94
68
|
{tagging?.brand && <span className="brand">{tagging.brand}</span>}
|
|
95
|
-
{tagging?.description &&
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
{tagging?.googleCategory && (
|
|
99
|
-
<span className="description">{tagging.googleCategory}</span>
|
|
100
|
-
)}
|
|
101
|
-
{tagging?.condition && (
|
|
102
|
-
<span className="condition">{tagging.condition}</span>
|
|
103
|
-
)}
|
|
69
|
+
{tagging?.description && <span className="description">{tagging.description}</span>}
|
|
70
|
+
{tagging?.googleCategory && <span className="description">{tagging.googleCategory}</span>}
|
|
71
|
+
{tagging?.condition && <span className="condition">{tagging.condition}</span>}
|
|
104
72
|
{tagging?.gender && <span className="gender">{tagging.gender}</span>}
|
|
105
|
-
{tagging?.ageGroup &&
|
|
106
|
-
<span className="age_group">{tagging.ageGroup}</span>
|
|
107
|
-
)}
|
|
73
|
+
{tagging?.ageGroup && <span className="age_group">{tagging.ageGroup}</span>}
|
|
108
74
|
{tagging?.gtin && <span className="gtin">{tagging.gtin}</span>}
|
|
109
75
|
{tagging?.category &&
|
|
110
76
|
tagging?.category.map((category, index) => (
|
|
@@ -130,12 +96,8 @@ export default function NostoProduct(props: {
|
|
|
130
96
|
{tag}
|
|
131
97
|
</span>
|
|
132
98
|
))}
|
|
133
|
-
{tagging?.ratingValue &&
|
|
134
|
-
|
|
135
|
-
)}
|
|
136
|
-
{tagging?.reviewCount && (
|
|
137
|
-
<span className="review_count">{tagging.reviewCount}</span>
|
|
138
|
-
)}
|
|
99
|
+
{tagging?.ratingValue && <span className="rating_value">{tagging.ratingValue}</span>}
|
|
100
|
+
{tagging?.reviewCount && <span className="review_count">{tagging.reviewCount}</span>}
|
|
139
101
|
{tagging?.alternateImageUrls &&
|
|
140
102
|
tagging.alternateImageUrls.map((url, index) => (
|
|
141
103
|
<span className="alternate_image_url" key={index}>
|
|
@@ -158,17 +120,11 @@ export default function NostoProduct(props: {
|
|
|
158
120
|
{sku?.id && <span className="product_id">{sku.id}</span>}
|
|
159
121
|
{sku?.name && <span className="name">{sku.name}</span>}
|
|
160
122
|
{sku?.price && <span className="price">{sku.price}</span>}
|
|
161
|
-
{sku?.listPrice &&
|
|
162
|
-
<span className="list_price">{sku.listPrice}</span>
|
|
163
|
-
)}
|
|
123
|
+
{sku?.listPrice && <span className="list_price">{sku.listPrice}</span>}
|
|
164
124
|
{sku?.url && <span className="url">{sku.url.toString()}</span>}
|
|
165
|
-
{sku?.imageUrl && (
|
|
166
|
-
<span className="image_url">{sku.imageUrl.toString()}</span>
|
|
167
|
-
)}
|
|
125
|
+
{sku?.imageUrl && <span className="image_url">{sku.imageUrl.toString()}</span>}
|
|
168
126
|
{sku?.gtin && <span className="gtin">{sku.gtin}</span>}
|
|
169
|
-
{sku?.availability &&
|
|
170
|
-
<span className="availability">{sku.availability}</span>
|
|
171
|
-
)}
|
|
127
|
+
{sku?.availability && <span className="availability">{sku.availability}</span>}
|
|
172
128
|
{sku?.customFields &&
|
|
173
129
|
Object.keys(sku.customFields).map(
|
|
174
130
|
(field, index) =>
|
|
@@ -183,5 +139,5 @@ export default function NostoProduct(props: {
|
|
|
183
139
|
))}
|
|
184
140
|
</div>
|
|
185
141
|
</>
|
|
186
|
-
)
|
|
142
|
+
)
|
|
187
143
|
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import React, { useEffect, isValidElement, useState, useRef } from "react"
|
|
2
|
+
import { NostoContext } from "./context"
|
|
3
|
+
import { createRoot, Root } from "react-dom/client"
|
|
4
|
+
import { NostoClient, Recommendation } from "../types"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @group Components
|
|
8
|
+
*/
|
|
9
|
+
export interface NostoProviderProps {
|
|
10
|
+
/**
|
|
11
|
+
* Indicates merchant id
|
|
12
|
+
*/
|
|
13
|
+
account: string
|
|
14
|
+
/**
|
|
15
|
+
* Indicates currency
|
|
16
|
+
*/
|
|
17
|
+
currentVariation?: string
|
|
18
|
+
/**
|
|
19
|
+
* Indicates an url of a server
|
|
20
|
+
*/
|
|
21
|
+
host?: string
|
|
22
|
+
children: React.ReactElement
|
|
23
|
+
/**
|
|
24
|
+
* Indicates if merchant uses multiple currencies
|
|
25
|
+
*/
|
|
26
|
+
multiCurrency?: boolean
|
|
27
|
+
/**
|
|
28
|
+
* Recommendation component which holds nostoRecommendation object
|
|
29
|
+
*/
|
|
30
|
+
recommendationComponent?: React.ReactElement<{
|
|
31
|
+
nostoRecommendation: Recommendation
|
|
32
|
+
}>
|
|
33
|
+
/**
|
|
34
|
+
* Enables Shopify markets with language and market id
|
|
35
|
+
*/
|
|
36
|
+
shopifyMarkets?: {
|
|
37
|
+
language?: string
|
|
38
|
+
marketId?: string | number
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* This widget is what we call the Nosto root widget, which is responsible for adding the actual Nosto script and the JS API stub.
|
|
44
|
+
* This widget wraps all other React Nosto widgets.
|
|
45
|
+
*
|
|
46
|
+
* ```
|
|
47
|
+
* <NostoProvider account="your-nosto-account-id" recommendationComponent={<NostoSlot />}>
|
|
48
|
+
* <App />
|
|
49
|
+
* </NostoProvider>
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* **Note:** the component also accepts a prop to configure the host `host="connect.nosto.com"`.
|
|
53
|
+
* In advanced use-cases, the need to configure the host may surface.
|
|
54
|
+
*
|
|
55
|
+
* In order to implement client-side rendering, the requires a designated component to render the recommendations provided by Nosto.
|
|
56
|
+
* This component should be capable of processing the JSON response received from our backend.
|
|
57
|
+
* Notice the `recommendationComponent` prop passed to `<NostoProvider>` above.
|
|
58
|
+
*
|
|
59
|
+
* 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.
|
|
60
|
+
*
|
|
61
|
+
* @group Components
|
|
62
|
+
*/
|
|
63
|
+
export default function NostoProvider(props: NostoProviderProps) {
|
|
64
|
+
const {
|
|
65
|
+
account,
|
|
66
|
+
multiCurrency = false,
|
|
67
|
+
host,
|
|
68
|
+
children,
|
|
69
|
+
recommendationComponent,
|
|
70
|
+
shopifyMarkets,
|
|
71
|
+
} = props
|
|
72
|
+
const [clientScriptLoadedState, setClientScriptLoadedState] = React.useState(false)
|
|
73
|
+
const clientScriptLoaded = React.useMemo(() => clientScriptLoadedState, [clientScriptLoadedState])
|
|
74
|
+
|
|
75
|
+
// Pass currentVariation as empty string if multiCurrency is disabled
|
|
76
|
+
const currentVariation = multiCurrency ? props.currentVariation : ""
|
|
77
|
+
|
|
78
|
+
// Set responseMode for loading campaigns:
|
|
79
|
+
const responseMode = isValidElement(recommendationComponent) ? "JSON_ORIGINAL" : "HTML"
|
|
80
|
+
|
|
81
|
+
// RecommendationComponent for client-side rendering:
|
|
82
|
+
function RecommendationComponentWrapper(props: { nostoRecommendation: Recommendation }) {
|
|
83
|
+
return React.cloneElement(recommendationComponent!, {
|
|
84
|
+
// eslint-disable-next-line react/prop-types
|
|
85
|
+
nostoRecommendation: props.nostoRecommendation,
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// custom hook for rendering campaigns (CSR/SSR):
|
|
90
|
+
const [pageType, setPageType] = useState("")
|
|
91
|
+
function useRenderCampaigns(type: string = "") {
|
|
92
|
+
const placementRefs = useRef<Record<string, Root>>({})
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
if (pageType !== type) {
|
|
95
|
+
setPageType(type)
|
|
96
|
+
}
|
|
97
|
+
}, [])
|
|
98
|
+
|
|
99
|
+
const pageTypeUpdated = type === pageType
|
|
100
|
+
|
|
101
|
+
function renderCampaigns(
|
|
102
|
+
data: {
|
|
103
|
+
recommendations: Record<string, Recommendation>
|
|
104
|
+
campaigns: {
|
|
105
|
+
recommendations: Record<string, Recommendation>
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
api: NostoClient
|
|
109
|
+
) {
|
|
110
|
+
if (responseMode == "HTML") {
|
|
111
|
+
// inject content campaigns as usual:
|
|
112
|
+
api.placements.injectCampaigns(data.recommendations)
|
|
113
|
+
} else {
|
|
114
|
+
// render recommendation component into placements:
|
|
115
|
+
const recommendations = data.campaigns.recommendations
|
|
116
|
+
for (const key in recommendations) {
|
|
117
|
+
const recommendation = recommendations[key]
|
|
118
|
+
const placementSelector = "#" + key
|
|
119
|
+
const placement = () => document.querySelector(placementSelector)
|
|
120
|
+
|
|
121
|
+
if (placement()) {
|
|
122
|
+
if (!placementRefs.current[key]) placementRefs.current[key] = createRoot(placement()!)
|
|
123
|
+
const root = placementRefs.current[key]!
|
|
124
|
+
root.render(
|
|
125
|
+
<RecommendationComponentWrapper
|
|
126
|
+
nostoRecommendation={recommendation}
|
|
127
|
+
></RecommendationComponentWrapper>
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return { renderCampaigns, pageTypeUpdated }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
if (!window.nostojs) {
|
|
138
|
+
window.nostojs = (cb: (api: NostoClient) => void) => {
|
|
139
|
+
(window.nostojs.q = window.nostojs.q || []).push(cb)
|
|
140
|
+
}
|
|
141
|
+
window.nostojs(api => api.setAutoLoad(false))
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!document.querySelectorAll("[nosto-client-script]").length && !shopifyMarkets) {
|
|
145
|
+
const script = document.createElement("script")
|
|
146
|
+
script.type = "text/javascript"
|
|
147
|
+
script.src = "//" + (host || "connect.nosto.com") + "/include/" + account
|
|
148
|
+
script.async = true
|
|
149
|
+
script.setAttribute("nosto-client-script", "")
|
|
150
|
+
|
|
151
|
+
script.onload = () => {
|
|
152
|
+
if (typeof jest !== "undefined") {
|
|
153
|
+
window.nosto?.reload({
|
|
154
|
+
site: "localhost",
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
setClientScriptLoadedState(true)
|
|
158
|
+
}
|
|
159
|
+
document.body.appendChild(script)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Enable Shopify markets functionality:
|
|
163
|
+
if (shopifyMarkets) {
|
|
164
|
+
const existingScript = document.querySelector("[nosto-client-script]")
|
|
165
|
+
const nostoSandbox = document.querySelector("#nosto-sandbox")
|
|
166
|
+
|
|
167
|
+
if (
|
|
168
|
+
!existingScript ||
|
|
169
|
+
existingScript?.getAttribute("nosto-language") !== shopifyMarkets?.language ||
|
|
170
|
+
existingScript?.getAttribute("nosto-market-id") !== shopifyMarkets?.marketId
|
|
171
|
+
) {
|
|
172
|
+
if (clientScriptLoadedState) {
|
|
173
|
+
setClientScriptLoadedState(false)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
existingScript?.parentNode?.removeChild(existingScript)
|
|
177
|
+
nostoSandbox?.parentNode?.removeChild(nostoSandbox)
|
|
178
|
+
|
|
179
|
+
const script = document.createElement("script")
|
|
180
|
+
script.type = "text/javascript"
|
|
181
|
+
script.src =
|
|
182
|
+
"//" +
|
|
183
|
+
(host || "connect.nosto.com") +
|
|
184
|
+
`/script/shopify/market/nosto.js?merchant=${account}&market=${
|
|
185
|
+
shopifyMarkets.marketId || ""
|
|
186
|
+
}&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
|
+
}, [clientScriptLoadedState, shopifyMarkets])
|
|
204
|
+
|
|
205
|
+
return (
|
|
206
|
+
<NostoContext.Provider
|
|
207
|
+
value={{
|
|
208
|
+
account,
|
|
209
|
+
clientScriptLoaded,
|
|
210
|
+
currentVariation,
|
|
211
|
+
responseMode,
|
|
212
|
+
recommendationComponent,
|
|
213
|
+
useRenderCampaigns,
|
|
214
|
+
pageType,
|
|
215
|
+
}}
|
|
216
|
+
>
|
|
217
|
+
{children}
|
|
218
|
+
</NostoContext.Provider>
|
|
219
|
+
)
|
|
220
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useNostoContext } from "./context"
|
|
2
|
+
import { useNostoApi } from "../utils/hooks"
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* You can personalise your search pages by using the NostoSearch component.
|
|
@@ -22,45 +22,24 @@ import { useNostoContext } from "../Provider/context.client";
|
|
|
22
22
|
* A query for `black shoes` must be provided as-is and not as `black+shoes`.
|
|
23
23
|
* Doing so will lead to invalid results.
|
|
24
24
|
*
|
|
25
|
-
* @group
|
|
25
|
+
* @group Components
|
|
26
26
|
*/
|
|
27
|
-
export default function NostoSearch(props: {
|
|
28
|
-
query
|
|
29
|
-
|
|
30
|
-
}): JSX.Element {
|
|
31
|
-
const { query } = props;
|
|
32
|
-
const {
|
|
33
|
-
clientScriptLoaded,
|
|
34
|
-
currentVariation,
|
|
35
|
-
responseMode,
|
|
36
|
-
recommendationComponent,
|
|
37
|
-
useRenderCampaigns,
|
|
38
|
-
} = useNostoContext();
|
|
27
|
+
export default function NostoSearch(props: { query: string; placements?: string[] }) {
|
|
28
|
+
const { query, placements } = props
|
|
29
|
+
const { recommendationComponent, useRenderCampaigns } = useNostoContext()
|
|
39
30
|
|
|
40
|
-
const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("search")
|
|
31
|
+
const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("search")
|
|
41
32
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
.then((data) => {
|
|
53
|
-
renderCampaigns(data, api);
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}, [
|
|
58
|
-
clientScriptLoaded,
|
|
59
|
-
currentVariation,
|
|
60
|
-
query,
|
|
61
|
-
recommendationComponent,
|
|
62
|
-
pageTypeUpdated,
|
|
63
|
-
]);
|
|
33
|
+
useNostoApi(
|
|
34
|
+
async (api) => {
|
|
35
|
+
const data = await api.defaultSession()
|
|
36
|
+
.viewSearch(query)
|
|
37
|
+
.setPlacements(placements || api.placements.getPlacements())
|
|
38
|
+
.load()
|
|
39
|
+
renderCampaigns(data, api)
|
|
40
|
+
},
|
|
41
|
+
[query, recommendationComponent, pageTypeUpdated]
|
|
42
|
+
)
|
|
64
43
|
|
|
65
44
|
return (
|
|
66
45
|
<>
|
|
@@ -71,5 +50,5 @@ export default function NostoSearch(props: {
|
|
|
71
50
|
{query}
|
|
72
51
|
</div>
|
|
73
52
|
</>
|
|
74
|
-
)
|
|
53
|
+
)
|
|
75
54
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { useNostoContext } from "
|
|
2
|
-
import { Cart, Customer } from "
|
|
3
|
-
import { snakeize } from "
|
|
4
|
-
import { useDeepCompareEffect } from "
|
|
1
|
+
import { useNostoContext } from "./context"
|
|
2
|
+
import { Cart, Customer } from "../types"
|
|
3
|
+
import { snakeize } from "../utils/snakeize"
|
|
4
|
+
import { useDeepCompareEffect } from "../utils/hooks"
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Nosto React requires that you pass it the details of current cart contents and the details of the currently logged-in customer, if any, on every route change.
|
|
@@ -13,29 +13,26 @@ import { useDeepCompareEffect } from "../../utils/hooks";
|
|
|
13
13
|
*
|
|
14
14
|
* @group Essential Functions
|
|
15
15
|
*/
|
|
16
|
-
export default function NostoSession(props: {
|
|
17
|
-
cart
|
|
18
|
-
|
|
19
|
-
}): JSX.Element {
|
|
20
|
-
const { cart, customer } = props;
|
|
21
|
-
const { clientScriptLoaded } = useNostoContext();
|
|
16
|
+
export default function NostoSession(props: { cart: Cart; customer: Customer }) {
|
|
17
|
+
const { cart, customer } = props
|
|
18
|
+
const { clientScriptLoaded } = useNostoContext()
|
|
22
19
|
|
|
23
20
|
useDeepCompareEffect(() => {
|
|
24
|
-
const currentCart = cart ? snakeize(cart) : undefined
|
|
25
|
-
const currentCustomer = customer ? snakeize(customer) : undefined
|
|
21
|
+
const currentCart = cart ? snakeize(cart) : undefined
|
|
22
|
+
const currentCustomer = customer ? snakeize(customer) : undefined
|
|
26
23
|
|
|
27
24
|
if (clientScriptLoaded) {
|
|
28
|
-
window.nostojs(
|
|
25
|
+
window.nostojs(api => {
|
|
29
26
|
api
|
|
30
27
|
.defaultSession()
|
|
31
28
|
.setResponseMode("HTML")
|
|
32
29
|
.setCart(currentCart)
|
|
33
30
|
.setCustomer(currentCustomer)
|
|
34
31
|
.viewOther()
|
|
35
|
-
.load()
|
|
36
|
-
})
|
|
32
|
+
.load()
|
|
33
|
+
})
|
|
37
34
|
}
|
|
38
|
-
}, [clientScriptLoaded, cart, customer])
|
|
35
|
+
}, [clientScriptLoaded, cart, customer])
|
|
39
36
|
|
|
40
|
-
return
|
|
37
|
+
return <></>
|
|
41
38
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { createContext, useContext } from "react"
|
|
2
|
+
import { NostoClient, Recommendation } from "../types"
|
|
3
|
+
|
|
4
|
+
type AnyFunction = (...args: unknown[]) => unknown
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @group Types
|
|
8
|
+
*/
|
|
9
|
+
export interface NostoContextType {
|
|
10
|
+
account: string
|
|
11
|
+
clientScriptLoaded: boolean
|
|
12
|
+
currentVariation?: string
|
|
13
|
+
renderFunction?: AnyFunction
|
|
14
|
+
responseMode: string
|
|
15
|
+
recommendationComponent?: React.ReactElement<{
|
|
16
|
+
nostoRecommendation: Recommendation
|
|
17
|
+
}>
|
|
18
|
+
useRenderCampaigns(type: string): {
|
|
19
|
+
renderCampaigns(data: unknown, api: NostoClient): void
|
|
20
|
+
pageTypeUpdated: boolean
|
|
21
|
+
}
|
|
22
|
+
pageType: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @group Essential Functions
|
|
27
|
+
*/
|
|
28
|
+
export const NostoContext = createContext<NostoContextType>({
|
|
29
|
+
account: "",
|
|
30
|
+
currentVariation: "",
|
|
31
|
+
pageType: "",
|
|
32
|
+
responseMode: "HTML",
|
|
33
|
+
clientScriptLoaded: false,
|
|
34
|
+
useRenderCampaigns: () => {
|
|
35
|
+
return {
|
|
36
|
+
renderCampaigns: () => {},
|
|
37
|
+
pageTypeUpdated: false,
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A hook that allows you to access the NostoContext and retrieve Nosto-related data from it in React components.
|
|
44
|
+
*
|
|
45
|
+
* @group Essential Functions
|
|
46
|
+
*/
|
|
47
|
+
export function useNostoContext(): NostoContextType {
|
|
48
|
+
const context = useContext(NostoContext)
|
|
49
|
+
|
|
50
|
+
if (!context) {
|
|
51
|
+
throw new Error("No nosto context found")
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return context
|
|
55
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { default as Nosto404 } from "./Nosto404"
|
|
2
|
+
export { default as NostoOther } from "./NostoOther"
|
|
3
|
+
export { default as NostoCheckout } from "./NostoCheckout"
|
|
4
|
+
export { default as NostoProduct } from "./NostoProduct"
|
|
5
|
+
export { default as NostoCategory } from "./NostoCategory"
|
|
6
|
+
export { default as NostoSearch } from "./NostoSearch"
|
|
7
|
+
export { default as NostoOrder } from "./NostoOrder"
|
|
8
|
+
export { default as NostoHome } from "./NostoHome"
|
|
9
|
+
export { default as NostoPlacement } from "./NostoPlacement"
|
|
10
|
+
export { default as NostoProvider } from "./NostoProvider"
|
|
11
|
+
|
|
12
|
+
export { NostoContext, useNostoContext } from "./context"
|
|
13
|
+
export type { NostoContextType } from "./context"
|
|
14
|
+
export { default as NostoSession } from "./NostoSession"
|
package/src/index.ts
ADDED