@nosto/nosto-react 0.1.2 → 0.1.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.
@@ -1,5 +1,5 @@
1
1
  import * as React from "react";
2
- import React__default, { useEffect, createContext } from "react";
2
+ import React__default, { useEffect, createContext, useContext } from "react";
3
3
  var jsxRuntime = { exports: {} };
4
4
  var reactJsxRuntime_production_min = {};
5
5
  /**
@@ -87,50 +87,34 @@ const NostoCheckout = () => {
87
87
  })
88
88
  });
89
89
  };
90
- function stringinate(obj) {
91
- Object.keys(obj).forEach((property) => {
92
- if (obj[property] instanceof URL) {
93
- obj[property] = obj[property].toString();
94
- }
95
- if (typeof obj[property] === "object") {
96
- stringinate(obj[property]);
97
- }
98
- obj[property] = obj[property];
99
- });
100
- return obj;
90
+ const NostoContext = createContext({
91
+ account: void 0,
92
+ currentVariation: ""
93
+ });
94
+ function useNostoContext() {
95
+ const context = useContext(NostoContext);
96
+ if (!context) {
97
+ throw new Error("No nosto context found");
98
+ }
99
+ return context;
101
100
  }
102
- var snakeize = function walk(obj) {
103
- if (!obj || typeof obj !== "object")
104
- return obj;
105
- if (isDate(obj) || isRegex(obj))
106
- return obj;
107
- if (Array.isArray(obj))
108
- return obj.map(walk);
109
- return Object.keys(obj).reduce(function(acc, key) {
110
- var camel = key[0].toLowerCase() + key.slice(1).replace(/([A-Z]+)/g, function(m2, x) {
111
- return "_" + x.toLowerCase();
112
- });
113
- acc[camel] = walk(obj[key]);
114
- return acc;
115
- }, {});
116
- };
117
- var isDate = function(obj) {
118
- return Object.prototype.toString.call(obj) === "[object Date]";
119
- };
120
- var isRegex = function(obj) {
121
- return Object.prototype.toString.call(obj) === "[object RegExp]";
122
- };
123
101
  const NostoProduct = ({
124
102
  product,
125
103
  tagging
126
104
  }) => {
105
+ const {
106
+ clientScriptLoaded,
107
+ currentVariation
108
+ } = useNostoContext();
127
109
  useEffect(() => {
128
- window.nostojs((api) => {
129
- api.defaultSession().setResponseMode("HTML").viewProduct(snakeize(stringinate(tagging))).setPlacements(api.placements.getPlacements()).load().then((data) => {
130
- api.placements.injectCampaigns(data.recommendations);
110
+ if (clientScriptLoaded) {
111
+ window.nostojs((api) => {
112
+ api.defaultSession().setVariation(currentVariation).setResponseMode("HTML").viewProduct(product).setPlacements(api.placements.getPlacements()).load().then((data) => {
113
+ api.placements.injectCampaigns(data.recommendations);
114
+ });
131
115
  });
132
- });
133
- }, []);
116
+ }
117
+ }, [clientScriptLoaded, currentVariation]);
134
118
  return /* @__PURE__ */ jsxs(Fragment, {
135
119
  children: [/* @__PURE__ */ jsx("div", {
136
120
  className: "nosto_page_type",
@@ -143,105 +127,105 @@ const NostoProduct = ({
143
127
  style: {
144
128
  display: "none"
145
129
  },
146
- children: [tagging.variationId && /* @__PURE__ */ jsx("span", {
130
+ children: [(tagging == null ? void 0 : tagging.variationId) && /* @__PURE__ */ jsx("span", {
147
131
  className: "variation_id",
148
132
  children: tagging.variationId
149
- }), tagging.productId && /* @__PURE__ */ jsx("span", {
133
+ }), product && /* @__PURE__ */ jsx("span", {
150
134
  className: "product_id",
151
- children: tagging.productId
152
- }), tagging.name && /* @__PURE__ */ jsx("span", {
135
+ children: product
136
+ }), (tagging == null ? void 0 : tagging.name) && /* @__PURE__ */ jsx("span", {
153
137
  className: "name",
154
138
  children: tagging.name
155
- }), tagging.url && /* @__PURE__ */ jsx("span", {
139
+ }), (tagging == null ? void 0 : tagging.url) && /* @__PURE__ */ jsx("span", {
156
140
  className: "url",
157
141
  children: tagging.url.toString()
158
- }), tagging.imageUrl && /* @__PURE__ */ jsx("span", {
142
+ }), (tagging == null ? void 0 : tagging.imageUrl) && /* @__PURE__ */ jsx("span", {
159
143
  className: "image_url",
160
144
  children: tagging.imageUrl.toString()
161
- }), tagging.availability && /* @__PURE__ */ jsx("span", {
145
+ }), (tagging == null ? void 0 : tagging.availability) && /* @__PURE__ */ jsx("span", {
162
146
  className: "availability",
163
147
  children: tagging.availability
164
- }), tagging.price && /* @__PURE__ */ jsx("span", {
148
+ }), (tagging == null ? void 0 : tagging.price) && /* @__PURE__ */ jsx("span", {
165
149
  className: "price",
166
150
  children: tagging.price
167
- }), tagging.listPrice && /* @__PURE__ */ jsx("span", {
151
+ }), (tagging == null ? void 0 : tagging.listPrice) && /* @__PURE__ */ jsx("span", {
168
152
  className: "list_price",
169
153
  children: tagging.listPrice
170
- }), tagging.priceCurrencyCode && /* @__PURE__ */ jsx("span", {
154
+ }), (tagging == null ? void 0 : tagging.priceCurrencyCode) && /* @__PURE__ */ jsx("span", {
171
155
  className: "price_currency_code",
172
156
  children: tagging.priceCurrencyCode
173
- }), tagging.brand && /* @__PURE__ */ jsx("span", {
157
+ }), (tagging == null ? void 0 : tagging.brand) && /* @__PURE__ */ jsx("span", {
174
158
  className: "brand",
175
159
  children: tagging.brand
176
- }), tagging.description && /* @__PURE__ */ jsx("span", {
160
+ }), (tagging == null ? void 0 : tagging.description) && /* @__PURE__ */ jsx("span", {
177
161
  className: "description",
178
162
  children: tagging.description
179
- }), tagging.googleCategory && /* @__PURE__ */ jsx("span", {
163
+ }), (tagging == null ? void 0 : tagging.googleCategory) && /* @__PURE__ */ jsx("span", {
180
164
  className: "description",
181
165
  children: tagging.googleCategory
182
- }), tagging.condition && /* @__PURE__ */ jsx("span", {
166
+ }), (tagging == null ? void 0 : tagging.condition) && /* @__PURE__ */ jsx("span", {
183
167
  className: "condition",
184
168
  children: tagging.condition
185
- }), tagging.gender && /* @__PURE__ */ jsx("span", {
169
+ }), (tagging == null ? void 0 : tagging.gender) && /* @__PURE__ */ jsx("span", {
186
170
  className: "gender",
187
171
  children: tagging.gender
188
- }), tagging.ageGroup && /* @__PURE__ */ jsx("span", {
172
+ }), (tagging == null ? void 0 : tagging.ageGroup) && /* @__PURE__ */ jsx("span", {
189
173
  className: "age_group",
190
174
  children: tagging.ageGroup
191
- }), tagging.gtin && /* @__PURE__ */ jsx("span", {
175
+ }), (tagging == null ? void 0 : tagging.gtin) && /* @__PURE__ */ jsx("span", {
192
176
  className: "gtin",
193
177
  children: tagging.gtin
194
- }), tagging.category && tagging.category.map((category, index) => /* @__PURE__ */ jsx("span", {
178
+ }), (tagging == null ? void 0 : tagging.category) && (tagging == null ? void 0 : tagging.category.map((category, index) => /* @__PURE__ */ jsx("span", {
195
179
  className: "category",
196
180
  children: category
197
- }, index)), tagging.tags1 && tagging.tags1.map((tag, index) => /* @__PURE__ */ jsx("span", {
181
+ }, index))), (tagging == null ? void 0 : tagging.tags1) && tagging.tags1.map((tag, index) => /* @__PURE__ */ jsx("span", {
198
182
  className: "tag1",
199
183
  children: tag
200
- }, index)), tagging.tags2 && tagging.tags2.map((tag, index) => /* @__PURE__ */ jsx("span", {
184
+ }, index)), (tagging == null ? void 0 : tagging.tags2) && tagging.tags2.map((tag, index) => /* @__PURE__ */ jsx("span", {
201
185
  className: "tag2",
202
186
  children: tag
203
- }, index)), tagging.tags3 && tagging.tags3.map((tag, index) => /* @__PURE__ */ jsx("span", {
187
+ }, index)), (tagging == null ? void 0 : tagging.tags3) && tagging.tags3.map((tag, index) => /* @__PURE__ */ jsx("span", {
204
188
  className: "tag3",
205
189
  children: tag
206
- }, index)), tagging.ratingValue && /* @__PURE__ */ jsx("span", {
190
+ }, index)), (tagging == null ? void 0 : tagging.ratingValue) && /* @__PURE__ */ jsx("span", {
207
191
  className: "rating_value",
208
192
  children: tagging.ratingValue
209
- }), tagging.reviewCount && /* @__PURE__ */ jsx("span", {
193
+ }), (tagging == null ? void 0 : tagging.reviewCount) && /* @__PURE__ */ jsx("span", {
210
194
  className: "review_count",
211
195
  children: tagging.reviewCount
212
- }), tagging.alternateImageUrls && tagging.alternateImageUrls.map((url, index) => /* @__PURE__ */ jsx("span", {
196
+ }), (tagging == null ? void 0 : tagging.alternateImageUrls) && tagging.alternateImageUrls.map((url, index) => /* @__PURE__ */ jsx("span", {
213
197
  className: "alternate_image_url",
214
198
  children: url.toString()
215
- }, index)), tagging.customFields && Object.keys(tagging.customFields).map((field, index) => tagging.customFields && tagging.customFields[field] && /* @__PURE__ */ jsx("span", {
199
+ }, index)), (tagging == null ? void 0 : tagging.customFields) && Object.keys(tagging.customFields).map((field, index) => tagging.customFields && tagging.customFields[field] && /* @__PURE__ */ jsx("span", {
216
200
  className: field,
217
201
  children: tagging.customFields[field]
218
- }, index)), tagging.skus && tagging.skus.map((sku, index) => /* @__PURE__ */ jsxs("span", {
202
+ }, index)), (tagging == null ? void 0 : tagging.skus) && tagging.skus.map((sku, index) => /* @__PURE__ */ jsxs("span", {
219
203
  className: "nosto_sku",
220
- children: [sku.id && /* @__PURE__ */ jsx("span", {
204
+ children: [(sku == null ? void 0 : sku.id) && /* @__PURE__ */ jsx("span", {
221
205
  className: "product_id",
222
206
  children: sku.id
223
- }), sku.name && /* @__PURE__ */ jsx("span", {
207
+ }), (sku == null ? void 0 : sku.name) && /* @__PURE__ */ jsx("span", {
224
208
  className: "name",
225
209
  children: sku.name
226
- }), sku.price && /* @__PURE__ */ jsx("span", {
210
+ }), (sku == null ? void 0 : sku.price) && /* @__PURE__ */ jsx("span", {
227
211
  className: "price",
228
212
  children: sku.price
229
- }), sku.listPrice && /* @__PURE__ */ jsx("span", {
213
+ }), (sku == null ? void 0 : sku.listPrice) && /* @__PURE__ */ jsx("span", {
230
214
  className: "list_price",
231
215
  children: sku.listPrice
232
- }), sku.url && /* @__PURE__ */ jsx("span", {
216
+ }), (sku == null ? void 0 : sku.url) && /* @__PURE__ */ jsx("span", {
233
217
  className: "url",
234
218
  children: sku.url.toString()
235
- }), sku.imageUrl && /* @__PURE__ */ jsx("span", {
219
+ }), (sku == null ? void 0 : sku.imageUrl) && /* @__PURE__ */ jsx("span", {
236
220
  className: "image_url",
237
221
  children: sku.imageUrl.toString()
238
- }), sku.gtin && /* @__PURE__ */ jsx("span", {
222
+ }), (sku == null ? void 0 : sku.gtin) && /* @__PURE__ */ jsx("span", {
239
223
  className: "gtin",
240
224
  children: sku.gtin
241
- }), sku.availability && /* @__PURE__ */ jsx("span", {
225
+ }), (sku == null ? void 0 : sku.availability) && /* @__PURE__ */ jsx("span", {
242
226
  className: "availability",
243
227
  children: sku.availability
244
- }), sku.customFields && Object.keys(sku.customFields).map((field, index2) => sku.customFields && sku.customFields[field] && /* @__PURE__ */ jsx("span", {
228
+ }), (sku == null ? void 0 : sku.customFields) && Object.keys(sku.customFields).map((field, index2) => sku.customFields && sku.customFields[field] && /* @__PURE__ */ jsx("span", {
245
229
  className: field,
246
230
  children: sku.customFields[field]
247
231
  }, index2))]
@@ -252,13 +236,19 @@ const NostoProduct = ({
252
236
  const NostoCategory = ({
253
237
  category
254
238
  }) => {
239
+ const {
240
+ clientScriptLoaded,
241
+ currentVariation
242
+ } = useNostoContext();
255
243
  useEffect(() => {
256
- window.nostojs((api) => {
257
- api.defaultSession().setResponseMode("HTML").viewCategory(category).setPlacements(api.placements.getPlacements()).load().then((data) => {
258
- api.placements.injectCampaigns(data.recommendations);
244
+ if (clientScriptLoaded) {
245
+ window.nostojs((api) => {
246
+ api.defaultSession().setVariation(currentVariation).setResponseMode("HTML").viewCategory(category).setPlacements(api.placements.getPlacements()).load().then((data) => {
247
+ api.placements.injectCampaigns(data.recommendations);
248
+ });
259
249
  });
260
- });
261
- }, []);
250
+ }
251
+ }, [clientScriptLoaded, category, currentVariation]);
262
252
  return /* @__PURE__ */ jsxs(Fragment, {
263
253
  children: [/* @__PURE__ */ jsx("div", {
264
254
  className: "nosto_page_type",
@@ -301,6 +291,27 @@ const NostoSearch = ({
301
291
  })]
302
292
  });
303
293
  };
294
+ var snakeize = function walk(obj) {
295
+ if (!obj || typeof obj !== "object")
296
+ return obj;
297
+ if (isDate(obj) || isRegex(obj))
298
+ return obj;
299
+ if (Array.isArray(obj))
300
+ return obj.map(walk);
301
+ return Object.keys(obj).reduce(function(acc, key) {
302
+ var camel = key[0].toLowerCase() + key.slice(1).replace(/([A-Z]+)/g, function(m2, x) {
303
+ return "_" + x.toLowerCase();
304
+ });
305
+ acc[camel] = walk(obj[key]);
306
+ return acc;
307
+ }, {});
308
+ };
309
+ var isDate = function(obj) {
310
+ return Object.prototype.toString.call(obj) === "[object Date]";
311
+ };
312
+ var isRegex = function(obj) {
313
+ return Object.prototype.toString.call(obj) === "[object RegExp]";
314
+ };
304
315
  const NostoOrder = ({
305
316
  order
306
317
  }) => {
@@ -328,13 +339,22 @@ const NostoOrder = ({
328
339
  });
329
340
  };
330
341
  const NostoHome = () => {
342
+ const {
343
+ clientScriptLoaded,
344
+ currentVariation
345
+ } = useNostoContext();
331
346
  useEffect(() => {
332
- window.nostojs((api) => {
333
- api.defaultSession().setResponseMode("HTML").viewFrontPage().setPlacements(api.placements.getPlacements()).load().then((data) => {
334
- api.placements.injectCampaigns(data.recommendations);
347
+ if (currentVariation) {
348
+ console.log("currentVariation: ", currentVariation);
349
+ }
350
+ if (clientScriptLoaded) {
351
+ window.nostojs((api) => {
352
+ api.defaultSession().setVariation(currentVariation).setResponseMode("HTML").viewFrontPage().setPlacements(api.placements.getPlacements()).load().then((data) => {
353
+ api.placements.injectCampaigns(data.recommendations);
354
+ });
335
355
  });
336
- });
337
- }, []);
356
+ }
357
+ }, [clientScriptLoaded, currentVariation]);
338
358
  return /* @__PURE__ */ jsx(Fragment, {
339
359
  children: /* @__PURE__ */ jsx("div", {
340
360
  className: "nosto_page_type",
@@ -363,27 +383,37 @@ const NostoPlacement = ({
363
383
  id
364
384
  });
365
385
  };
366
- const NostoContext = createContext({
367
- account: void 0
368
- });
369
386
  const NostoProvider = ({
370
387
  account,
371
- currentVariation,
388
+ currentVariation = "",
389
+ multiCurrency = false,
372
390
  host,
373
391
  children
374
392
  }) => {
393
+ const [clientScriptLoadedState, setClientScriptLoadedState] = React__default.useState(false);
394
+ const clientScriptLoaded = React__default.useMemo(() => clientScriptLoadedState, [clientScriptLoadedState]);
395
+ currentVariation = multiCurrency ? currentVariation : "";
375
396
  useEffect(() => {
376
- const script = document.createElement("script");
377
- script.type = "text/javascript";
378
- script.src = "//" + (host || "connect.nosto.com") + "/include/" + account;
379
- script.async = true;
380
- document.head.appendChild(script);
397
+ if (!document.querySelectorAll("[nosto-client-script]").length) {
398
+ const script = document.createElement("script");
399
+ script.type = "text/javascript";
400
+ script.src = "//" + (host || "connect.nosto.com") + "/include/" + account;
401
+ script.async = true;
402
+ script.setAttribute("nosto-client-script", "");
403
+ script.onload = () => {
404
+ console.log("Nosto client script loaded");
405
+ setClientScriptLoadedState(true);
406
+ };
407
+ document.head.appendChild(script);
408
+ }
381
409
  window.nostojs = (cb) => (window.nostojs.q = window.nostojs.q || []).push(cb);
382
410
  window.nostojs((api) => api.setAutoLoad(false));
383
411
  }, []);
384
412
  return /* @__PURE__ */ jsx(NostoContext.Provider, {
385
413
  value: {
386
- account
414
+ account,
415
+ clientScriptLoaded,
416
+ currentVariation
387
417
  },
388
418
  children
389
419
  });
@@ -503,4 +533,4 @@ const NostoSession = ({
503
533
  }, [cart || [], customer || {}]);
504
534
  return /* @__PURE__ */ jsx("div", {});
505
535
  };
506
- export { NostoFohofo as Nosto404, NostoCategory, NostoCheckout, NostoHome, NostoOrder, NostoOther, NostoPlacement, NostoProduct, NostoProvider, NostoSearch, NostoSession };
536
+ export { NostoFohofo as Nosto404, NostoCategory, NostoCheckout, NostoContext, NostoHome, NostoOrder, NostoOther, NostoPlacement, NostoProduct, NostoProvider, NostoSearch, NostoSession, useNostoContext };
@@ -1,4 +1,4 @@
1
- (function(c,a){typeof exports=="object"&&typeof module!="undefined"?a(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],a):(c=typeof globalThis!="undefined"?globalThis:c||self,a(c["@nosto/nosto-react"]={},c.React))})(this,function(c,a){"use strict";function P(t){return t&&typeof t=="object"&&"default"in t?t:{default:t}}function j(t){if(t&&t.__esModule)return t;var e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});return t&&Object.keys(t).forEach(function(s){if(s!=="default"){var n=Object.getOwnPropertyDescriptor(t,s);Object.defineProperty(e,s,n.get?n:{enumerable:!0,get:function(){return t[s]}})}}),e.default=t,Object.freeze(e)}var C=P(a),m=j(a),p={exports:{}},f={};/**
1
+ (function(l,a){typeof exports=="object"&&typeof module!="undefined"?a(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],a):(l=typeof globalThis!="undefined"?globalThis:l||self,a(l["@nosto/nosto-react"]={},l.React))})(this,function(l,a){"use strict";function O(s){return s&&typeof s=="object"&&"default"in s?s:{default:s}}function L(s){if(s&&s.__esModule)return s;var e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});return s&&Object.keys(s).forEach(function(n){if(n!=="default"){var r=Object.getOwnPropertyDescriptor(s,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return s[n]}})}}),e.default=s,Object.freeze(e)}var v=O(a),f=L(a),h={exports:{}},y={};/**
2
2
  * @license React
3
3
  * react-jsx-runtime.production.min.js
4
4
  *
@@ -6,4 +6,4 @@
6
6
  *
7
7
  * This source code is licensed under the MIT license found in the
8
8
  * LICENSE file in the root directory of this source tree.
9
- */var S=C.default,O=Symbol.for("react.element"),M=Symbol.for("react.fragment"),L=Object.prototype.hasOwnProperty,E=S.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,T={key:!0,ref:!0,__self:!0,__source:!0};function N(t,e,s){var n,o={},l=null,h=null;s!==void 0&&(l=""+s),e.key!==void 0&&(l=""+e.key),e.ref!==void 0&&(h=e.ref);for(n in e)L.call(e,n)&&!T.hasOwnProperty(n)&&(o[n]=e[n]);if(t&&t.defaultProps)for(n in e=t.defaultProps,e)o[n]===void 0&&(o[n]=e[n]);return{$$typeof:O,type:t,key:l,ref:h,props:o,_owner:E.current}}f.Fragment=M,f.jsx=N,f.jsxs=N,p.exports=f;const r=p.exports.jsx,d=p.exports.jsxs,i=p.exports.Fragment,F=()=>(a.useEffect(()=>{window.nostojs(t=>{t.defaultSession().setResponseMode("HTML").viewNotFound().setPlacements(t.placements.getPlacements()).load().then(e=>{t.placements.injectCampaigns(e.recommendations)})})},[]),r(i,{children:r("div",{className:"nosto_page_type",style:{display:"none"},children:"notfound"})})),R=()=>(a.useEffect(()=>{window.nostojs(t=>{t.defaultSession().setResponseMode("HTML").viewOther().setPlacements(t.placements.getPlacements()).load().then(e=>{t.placements.injectCampaigns(e.recommendations)})})},[]),r(i,{children:r("div",{className:"nosto_page_type",style:{display:"none"},children:"other"})})),U=()=>(a.useEffect(()=>{window.nostojs(t=>{t.defaultSession().setResponseMode("HTML").viewCart().setPlacements(t.placements.getPlacements()).load().then(e=>{t.placements.injectCampaigns(e.recommendations)})})},[]),r(i,{children:r("div",{className:"nosto_page_type",style:{display:"none"},children:"cart"})}));function v(t){return Object.keys(t).forEach(e=>{t[e]instanceof URL&&(t[e]=t[e].toString()),typeof t[e]=="object"&&v(t[e]),t[e]=t[e]}),t}var y=function t(e){return!e||typeof e!="object"||x(e)||D(e)?e:Array.isArray(e)?e.map(t):Object.keys(e).reduce(function(s,n){var o=n[0].toLowerCase()+n.slice(1).replace(/([A-Z]+)/g,function(l,h){return"_"+h.toLowerCase()});return s[o]=t(e[n]),s},{})},x=function(t){return Object.prototype.toString.call(t)==="[object Date]"},D=function(t){return Object.prototype.toString.call(t)==="[object RegExp]"};const H=({product:t,tagging:e})=>(a.useEffect(()=>{window.nostojs(s=>{s.defaultSession().setResponseMode("HTML").viewProduct(y(v(e))).setPlacements(s.placements.getPlacements()).load().then(n=>{s.placements.injectCampaigns(n.recommendations)})})},[]),d(i,{children:[r("div",{className:"nosto_page_type",style:{display:"none"},children:"product"}),d("div",{className:"nosto_product",style:{display:"none"},children:[e.variationId&&r("span",{className:"variation_id",children:e.variationId}),e.productId&&r("span",{className:"product_id",children:e.productId}),e.name&&r("span",{className:"name",children:e.name}),e.url&&r("span",{className:"url",children:e.url.toString()}),e.imageUrl&&r("span",{className:"image_url",children:e.imageUrl.toString()}),e.availability&&r("span",{className:"availability",children:e.availability}),e.price&&r("span",{className:"price",children:e.price}),e.listPrice&&r("span",{className:"list_price",children:e.listPrice}),e.priceCurrencyCode&&r("span",{className:"price_currency_code",children:e.priceCurrencyCode}),e.brand&&r("span",{className:"brand",children:e.brand}),e.description&&r("span",{className:"description",children:e.description}),e.googleCategory&&r("span",{className:"description",children:e.googleCategory}),e.condition&&r("span",{className:"condition",children:e.condition}),e.gender&&r("span",{className:"gender",children:e.gender}),e.ageGroup&&r("span",{className:"age_group",children:e.ageGroup}),e.gtin&&r("span",{className:"gtin",children:e.gtin}),e.category&&e.category.map((s,n)=>r("span",{className:"category",children:s},n)),e.tags1&&e.tags1.map((s,n)=>r("span",{className:"tag1",children:s},n)),e.tags2&&e.tags2.map((s,n)=>r("span",{className:"tag2",children:s},n)),e.tags3&&e.tags3.map((s,n)=>r("span",{className:"tag3",children:s},n)),e.ratingValue&&r("span",{className:"rating_value",children:e.ratingValue}),e.reviewCount&&r("span",{className:"review_count",children:e.reviewCount}),e.alternateImageUrls&&e.alternateImageUrls.map((s,n)=>r("span",{className:"alternate_image_url",children:s.toString()},n)),e.customFields&&Object.keys(e.customFields).map((s,n)=>e.customFields&&e.customFields[s]&&r("span",{className:s,children:e.customFields[s]},n)),e.skus&&e.skus.map((s,n)=>d("span",{className:"nosto_sku",children:[s.id&&r("span",{className:"product_id",children:s.id}),s.name&&r("span",{className:"name",children:s.name}),s.price&&r("span",{className:"price",children:s.price}),s.listPrice&&r("span",{className:"list_price",children:s.listPrice}),s.url&&r("span",{className:"url",children:s.url.toString()}),s.imageUrl&&r("span",{className:"image_url",children:s.imageUrl.toString()}),s.gtin&&r("span",{className:"gtin",children:s.gtin}),s.availability&&r("span",{className:"availability",children:s.availability}),s.customFields&&Object.keys(s.customFields).map((o,l)=>s.customFields&&s.customFields[o]&&r("span",{className:o,children:s.customFields[o]},l))]},n))]})]})),I=({category:t})=>(a.useEffect(()=>{window.nostojs(e=>{e.defaultSession().setResponseMode("HTML").viewCategory(t).setPlacements(e.placements.getPlacements()).load().then(s=>{e.placements.injectCampaigns(s.recommendations)})})},[]),d(i,{children:[r("div",{className:"nosto_page_type",style:{display:"none"},children:"category"}),r("div",{className:"nosto_category",style:{display:"none"},children:t})]})),A=({query:t})=>(a.useEffect(()=>{window.nostojs(e=>{e.defaultSession().setResponseMode("HTML").viewSearch(t).setPlacements(e.placements.getPlacements()).load().then(s=>{e.placements.injectCampaigns(s.recommendations)})})},[]),d(i,{children:[r("div",{className:"nosto_page_type",style:{display:"none"},children:"search"}),r("div",{className:"nosto_search",style:{display:"none"},children:t})]})),z=({order:t})=>(a.useEffect(()=>{window.nostojs(e=>{e.defaultSession().setResponseMode("HTML").addOrder(y(t)).setPlacements(e.placements.getPlacements()).load().then(s=>{e.placements.injectCampaigns(s.recommendations)})})},[]),d(i,{children:[r("div",{className:"nosto_page_type",style:{display:"none"},children:"order"}),r("div",{className:"nosto_order",style:{display:"none"},children:t.purchase.number})]})),V=()=>(a.useEffect(()=>{window.nostojs(t=>{t.defaultSession().setResponseMode("HTML").viewFrontPage().setPlacements(t.placements.getPlacements()).load().then(e=>{t.placements.injectCampaigns(e.recommendations)})})},[]),r(i,{children:r("div",{className:"nosto_page_type",style:{display:"none"},children:"front"})})),b=({id:t})=>r("div",{className:"nosto_element",onClick:s=>{const n=s.target.closest("a");if(n)s.preventDefault(),location.href=n.href.toString().replace(new URL(n.href).origin,"");else return},id:t}),q=a.createContext({account:void 0}),B=({account:t,currentVariation:e,host:s,children:n})=>(a.useEffect(()=>{const o=document.createElement("script");o.type="text/javascript",o.src="//"+(s||"connect.nosto.com")+"/include/"+t,o.async=!0,document.head.appendChild(o),window.nostojs=l=>(window.nostojs.q=window.nostojs.q||[]).push(l),window.nostojs(l=>l.setAutoLoad(!1))},[]),r(q.Provider,{value:{account:t},children:n}));var _=Object.prototype.hasOwnProperty;function w(t,e,s){for(s of t.keys())if(u(s,e))return s}function u(t,e){var s,n,o;if(t===e)return!0;if(t&&e&&(s=t.constructor)===e.constructor){if(s===Date)return t.getTime()===e.getTime();if(s===RegExp)return t.toString()===e.toString();if(s===Array){if((n=t.length)===e.length)for(;n--&&u(t[n],e[n]););return n===-1}if(s===Set){if(t.size!==e.size)return!1;for(n of t)if(o=n,o&&typeof o=="object"&&(o=w(e,o),!o)||!e.has(o))return!1;return!0}if(s===Map){if(t.size!==e.size)return!1;for(n of t)if(o=n[0],o&&typeof o=="object"&&(o=w(e,o),!o)||!u(n[1],e.get(o)))return!1;return!0}if(s===ArrayBuffer)t=new Uint8Array(t),e=new Uint8Array(e);else if(s===DataView){if((n=t.byteLength)===e.byteLength)for(;n--&&t.getInt8(n)===e.getInt8(n););return n===-1}if(ArrayBuffer.isView(t)){if((n=t.byteLength)===e.byteLength)for(;n--&&t[n]===e[n];);return n===-1}if(!s||typeof t=="object"){n=0;for(s in t)if(_.call(t,s)&&++n&&!_.call(e,s)||!(s in e)||!u(t[s],e[s]))return!1;return Object.keys(e).length===n}}return t!==t&&e!==e}function G(t){var e=m.useRef(t),s=m.useRef(0);return u(t,e.current)||(e.current=t,s.current+=1),m.useMemo(function(){return e.current},[s.current])}function $(t,e){return m.useEffect(t,G(e))}const J=({cart:t,customer:e})=>($(()=>{console.debug(e||void 0);const n=t||void 0;console.debug(n),window.nostojs(o=>{o.defaultSession().setResponseMode("HTML").setCart(y(n)).viewOther().load()})},[t||[],e||{}]),r("div",{}));c.Nosto404=F,c.NostoCategory=I,c.NostoCheckout=U,c.NostoHome=V,c.NostoOrder=z,c.NostoOther=R,c.NostoPlacement=b,c.NostoProduct=H,c.NostoProvider=B,c.NostoSearch=A,c.NostoSession=J,Object.defineProperties(c,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
9
+ */var M=v.default,E=Symbol.for("react.element"),T=Symbol.for("react.fragment"),F=Object.prototype.hasOwnProperty,x=M.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,R={key:!0,ref:!0,__self:!0,__source:!0};function S(s,e,n){var r,t={},c=null,d=null;n!==void 0&&(c=""+n),e.key!==void 0&&(c=""+e.key),e.ref!==void 0&&(d=e.ref);for(r in e)F.call(e,r)&&!R.hasOwnProperty(r)&&(t[r]=e[r]);if(s&&s.defaultProps)for(r in e=s.defaultProps,e)t[r]===void 0&&(t[r]=e[r]);return{$$typeof:E,type:s,key:c,ref:d,props:t,_owner:x.current}}y.Fragment=T,y.jsx=S,y.jsxs=S,h.exports=y;const o=h.exports.jsx,p=h.exports.jsxs,m=h.exports.Fragment,U=()=>(a.useEffect(()=>{window.nostojs(s=>{s.defaultSession().setResponseMode("HTML").viewNotFound().setPlacements(s.placements.getPlacements()).load().then(e=>{s.placements.injectCampaigns(e.recommendations)})})},[]),o(m,{children:o("div",{className:"nosto_page_type",style:{display:"none"},children:"notfound"})})),A=()=>(a.useEffect(()=>{window.nostojs(s=>{s.defaultSession().setResponseMode("HTML").viewOther().setPlacements(s.placements.getPlacements()).load().then(e=>{s.placements.injectCampaigns(e.recommendations)})})},[]),o(m,{children:o("div",{className:"nosto_page_type",style:{display:"none"},children:"other"})})),b=()=>(a.useEffect(()=>{window.nostojs(s=>{s.defaultSession().setResponseMode("HTML").viewCart().setPlacements(s.placements.getPlacements()).load().then(e=>{s.placements.injectCampaigns(e.recommendations)})})},[]),o(m,{children:o("div",{className:"nosto_page_type",style:{display:"none"},children:"cart"})})),_=a.createContext({account:void 0,currentVariation:""});function N(){const s=a.useContext(_);if(!s)throw new Error("No nosto context found");return s}const D=({product:s,tagging:e})=>{const{clientScriptLoaded:n,currentVariation:r}=N();return a.useEffect(()=>{n&&window.nostojs(t=>{t.defaultSession().setVariation(r).setResponseMode("HTML").viewProduct(s).setPlacements(t.placements.getPlacements()).load().then(c=>{t.placements.injectCampaigns(c.recommendations)})})},[n,r]),p(m,{children:[o("div",{className:"nosto_page_type",style:{display:"none"},children:"product"}),p("div",{className:"nosto_product",style:{display:"none"},children:[(e==null?void 0:e.variationId)&&o("span",{className:"variation_id",children:e.variationId}),s&&o("span",{className:"product_id",children:s}),(e==null?void 0:e.name)&&o("span",{className:"name",children:e.name}),(e==null?void 0:e.url)&&o("span",{className:"url",children:e.url.toString()}),(e==null?void 0:e.imageUrl)&&o("span",{className:"image_url",children:e.imageUrl.toString()}),(e==null?void 0:e.availability)&&o("span",{className:"availability",children:e.availability}),(e==null?void 0:e.price)&&o("span",{className:"price",children:e.price}),(e==null?void 0:e.listPrice)&&o("span",{className:"list_price",children:e.listPrice}),(e==null?void 0:e.priceCurrencyCode)&&o("span",{className:"price_currency_code",children:e.priceCurrencyCode}),(e==null?void 0:e.brand)&&o("span",{className:"brand",children:e.brand}),(e==null?void 0:e.description)&&o("span",{className:"description",children:e.description}),(e==null?void 0:e.googleCategory)&&o("span",{className:"description",children:e.googleCategory}),(e==null?void 0:e.condition)&&o("span",{className:"condition",children:e.condition}),(e==null?void 0:e.gender)&&o("span",{className:"gender",children:e.gender}),(e==null?void 0:e.ageGroup)&&o("span",{className:"age_group",children:e.ageGroup}),(e==null?void 0:e.gtin)&&o("span",{className:"gtin",children:e.gtin}),(e==null?void 0:e.category)&&(e==null?void 0:e.category.map((t,c)=>o("span",{className:"category",children:t},c))),(e==null?void 0:e.tags1)&&e.tags1.map((t,c)=>o("span",{className:"tag1",children:t},c)),(e==null?void 0:e.tags2)&&e.tags2.map((t,c)=>o("span",{className:"tag2",children:t},c)),(e==null?void 0:e.tags3)&&e.tags3.map((t,c)=>o("span",{className:"tag3",children:t},c)),(e==null?void 0:e.ratingValue)&&o("span",{className:"rating_value",children:e.ratingValue}),(e==null?void 0:e.reviewCount)&&o("span",{className:"review_count",children:e.reviewCount}),(e==null?void 0:e.alternateImageUrls)&&e.alternateImageUrls.map((t,c)=>o("span",{className:"alternate_image_url",children:t.toString()},c)),(e==null?void 0:e.customFields)&&Object.keys(e.customFields).map((t,c)=>e.customFields&&e.customFields[t]&&o("span",{className:t,children:e.customFields[t]},c)),(e==null?void 0:e.skus)&&e.skus.map((t,c)=>p("span",{className:"nosto_sku",children:[(t==null?void 0:t.id)&&o("span",{className:"product_id",children:t.id}),(t==null?void 0:t.name)&&o("span",{className:"name",children:t.name}),(t==null?void 0:t.price)&&o("span",{className:"price",children:t.price}),(t==null?void 0:t.listPrice)&&o("span",{className:"list_price",children:t.listPrice}),(t==null?void 0:t.url)&&o("span",{className:"url",children:t.url.toString()}),(t==null?void 0:t.imageUrl)&&o("span",{className:"image_url",children:t.imageUrl.toString()}),(t==null?void 0:t.gtin)&&o("span",{className:"gtin",children:t.gtin}),(t==null?void 0:t.availability)&&o("span",{className:"availability",children:t.availability}),(t==null?void 0:t.customFields)&&Object.keys(t.customFields).map((d,w)=>t.customFields&&t.customFields[d]&&o("span",{className:d,children:t.customFields[d]},w))]},c))]})]})},H=({category:s})=>{const{clientScriptLoaded:e,currentVariation:n}=N();return a.useEffect(()=>{e&&window.nostojs(r=>{r.defaultSession().setVariation(n).setResponseMode("HTML").viewCategory(s).setPlacements(r.placements.getPlacements()).load().then(t=>{r.placements.injectCampaigns(t.recommendations)})})},[e,s,n]),p(m,{children:[o("div",{className:"nosto_page_type",style:{display:"none"},children:"category"}),o("div",{className:"nosto_category",style:{display:"none"},children:s})]})},V=({query:s})=>(a.useEffect(()=>{window.nostojs(e=>{e.defaultSession().setResponseMode("HTML").viewSearch(s).setPlacements(e.placements.getPlacements()).load().then(n=>{e.placements.injectCampaigns(n.recommendations)})})},[]),p(m,{children:[o("div",{className:"nosto_page_type",style:{display:"none"},children:"search"}),o("div",{className:"nosto_search",style:{display:"none"},children:s})]}));var j=function s(e){return!e||typeof e!="object"||I(e)||z(e)?e:Array.isArray(e)?e.map(s):Object.keys(e).reduce(function(n,r){var t=r[0].toLowerCase()+r.slice(1).replace(/([A-Z]+)/g,function(c,d){return"_"+d.toLowerCase()});return n[t]=s(e[r]),n},{})},I=function(s){return Object.prototype.toString.call(s)==="[object Date]"},z=function(s){return Object.prototype.toString.call(s)==="[object RegExp]"};const q=({order:s})=>(a.useEffect(()=>{window.nostojs(e=>{e.defaultSession().setResponseMode("HTML").addOrder(j(s)).setPlacements(e.placements.getPlacements()).load().then(n=>{e.placements.injectCampaigns(n.recommendations)})})},[]),p(m,{children:[o("div",{className:"nosto_page_type",style:{display:"none"},children:"order"}),o("div",{className:"nosto_order",style:{display:"none"},children:s.purchase.number})]})),B=()=>{const{clientScriptLoaded:s,currentVariation:e}=N();return a.useEffect(()=>{e&&console.log("currentVariation: ",e),s&&window.nostojs(n=>{n.defaultSession().setVariation(e).setResponseMode("HTML").viewFrontPage().setPlacements(n.placements.getPlacements()).load().then(r=>{n.placements.injectCampaigns(r.recommendations)})})},[s,e]),o(m,{children:o("div",{className:"nosto_page_type",style:{display:"none"},children:"front"})})},G=({id:s})=>o("div",{className:"nosto_element",onClick:n=>{const r=n.target.closest("a");if(r)n.preventDefault(),location.href=r.href.toString().replace(new URL(r.href).origin,"");else return},id:s}),$=({account:s,currentVariation:e="",multiCurrency:n=!1,host:r,children:t})=>{const[c,d]=v.default.useState(!1),w=v.default.useMemo(()=>c,[c]);return e=n?e:"",a.useEffect(()=>{if(!document.querySelectorAll("[nosto-client-script]").length){const i=document.createElement("script");i.type="text/javascript",i.src="//"+(r||"connect.nosto.com")+"/include/"+s,i.async=!0,i.setAttribute("nosto-client-script",""),i.onload=()=>{console.log("Nosto client script loaded"),d(!0)},document.head.appendChild(i)}window.nostojs=i=>(window.nostojs.q=window.nostojs.q||[]).push(i),window.nostojs(i=>i.setAutoLoad(!1))},[]),o(_.Provider,{value:{account:s,clientScriptLoaded:w,currentVariation:e},children:t})};var C=Object.prototype.hasOwnProperty;function P(s,e,n){for(n of s.keys())if(u(n,e))return n}function u(s,e){var n,r,t;if(s===e)return!0;if(s&&e&&(n=s.constructor)===e.constructor){if(n===Date)return s.getTime()===e.getTime();if(n===RegExp)return s.toString()===e.toString();if(n===Array){if((r=s.length)===e.length)for(;r--&&u(s[r],e[r]););return r===-1}if(n===Set){if(s.size!==e.size)return!1;for(r of s)if(t=r,t&&typeof t=="object"&&(t=P(e,t),!t)||!e.has(t))return!1;return!0}if(n===Map){if(s.size!==e.size)return!1;for(r of s)if(t=r[0],t&&typeof t=="object"&&(t=P(e,t),!t)||!u(r[1],e.get(t)))return!1;return!0}if(n===ArrayBuffer)s=new Uint8Array(s),e=new Uint8Array(e);else if(n===DataView){if((r=s.byteLength)===e.byteLength)for(;r--&&s.getInt8(r)===e.getInt8(r););return r===-1}if(ArrayBuffer.isView(s)){if((r=s.byteLength)===e.byteLength)for(;r--&&s[r]===e[r];);return r===-1}if(!n||typeof s=="object"){r=0;for(n in s)if(C.call(s,n)&&++r&&!C.call(e,n)||!(n in e)||!u(s[n],e[n]))return!1;return Object.keys(e).length===r}}return s!==s&&e!==e}function J(s){var e=f.useRef(s),n=f.useRef(0);return u(s,e.current)||(e.current=s,n.current+=1),f.useMemo(function(){return e.current},[n.current])}function W(s,e){return f.useEffect(s,J(e))}const Y=({cart:s,customer:e})=>(W(()=>{console.debug(e||void 0);const r=s||void 0;console.debug(r),window.nostojs(t=>{t.defaultSession().setResponseMode("HTML").setCart(j(r)).viewOther().load()})},[s||[],e||{}]),o("div",{}));l.Nosto404=U,l.NostoCategory=H,l.NostoCheckout=b,l.NostoContext=_,l.NostoHome=B,l.NostoOrder=q,l.NostoOther=A,l.NostoPlacement=G,l.NostoProduct=D,l.NostoProvider=$,l.NostoSearch=V,l.NostoSession=Y,l.useNostoContext=N,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nosto/nosto-react",
3
3
  "description": "Component library to simply implementing Nosto on React.",
4
- "version": "0.1.2",
4
+ "version": "0.1.3",
5
5
  "author": "Mridang Agarwalla, Dominik Gilg",
6
6
  "license": "ISC",
7
7
  "repository": {
@@ -0,0 +1,37 @@
1
+ import React, { useEffect } from "react";
2
+ import { useNostoContext } from "../Provider/context.client";
3
+
4
+ const NostoCategory: React.FC<{ category: string }> = ({ category }) => {
5
+ const { clientScriptLoaded, currentVariation } = useNostoContext();
6
+ useEffect(() => {
7
+ // @ts-ignore
8
+ if (clientScriptLoaded) {
9
+ window.nostojs((api: any) => {
10
+ api
11
+ .defaultSession()
12
+ .setVariation(currentVariation)
13
+ .setResponseMode("HTML")
14
+ .viewCategory(category)
15
+ .setPlacements(api.placements.getPlacements())
16
+ .load()
17
+ .then((data: object) => {
18
+ // @ts-ignore
19
+ api.placements.injectCampaigns(data.recommendations);
20
+ });
21
+ });
22
+ }
23
+ }, [clientScriptLoaded, category, currentVariation]);
24
+
25
+ return (
26
+ <>
27
+ <div className="nosto_page_type" style={{ display: "none" }}>
28
+ category
29
+ </div>
30
+ <div className="nosto_category" style={{ display: "none" }}>
31
+ {category}
32
+ </div>
33
+ </>
34
+ );
35
+ };
36
+
37
+ export default NostoCategory;
@@ -0,0 +1,37 @@
1
+ import React, { useEffect } from "react";
2
+ import { useNostoContext } from "../Provider/context.client";
3
+
4
+ const NostoHome: React.FC = () => {
5
+ const { clientScriptLoaded, currentVariation } = useNostoContext();
6
+ useEffect(() => {
7
+ if (currentVariation) {
8
+ console.log("currentVariation: ", currentVariation);
9
+ }
10
+ // @ts-ignore
11
+ if (clientScriptLoaded) {
12
+ window.nostojs((api: any) => {
13
+ api
14
+ .defaultSession()
15
+ .setVariation(currentVariation)
16
+ .setResponseMode("HTML")
17
+ .viewFrontPage()
18
+ .setPlacements(api.placements.getPlacements())
19
+ .load()
20
+ .then((data: object) => {
21
+ // @ts-ignore
22
+ api.placements.injectCampaigns(data.recommendations);
23
+ });
24
+ });
25
+ }
26
+ }, [clientScriptLoaded, currentVariation]);
27
+
28
+ return (
29
+ <>
30
+ <div className="nosto_page_type" style={{ display: "none" }}>
31
+ front
32
+ </div>
33
+ </>
34
+ );
35
+ };
36
+
37
+ export default NostoHome;
@@ -1,27 +1,30 @@
1
1
  import { Product } from "../../types";
2
- import stringinate from "../../utils/stringinate";
3
2
  import React, { useEffect } from "react";
4
- import snakeize from "snakeize";
3
+ import { useNostoContext } from "../Provider/context.client";
5
4
 
6
5
  const NostoProduct: React.FC<{ product: string; tagging: Product }> = ({
7
6
  product,
8
7
  tagging,
9
8
  }) => {
9
+ const { clientScriptLoaded, currentVariation } = useNostoContext();
10
10
  useEffect(() => {
11
11
  // @ts-ignore
12
- window.nostojs((api) => {
13
- api
14
- .defaultSession()
15
- .setResponseMode("HTML")
16
- .viewProduct(snakeize(stringinate(tagging)))
17
- .setPlacements(api.placements.getPlacements())
18
- .load()
19
- .then((data: object) => {
20
- // @ts-ignore
21
- api.placements.injectCampaigns(data.recommendations);
22
- });
23
- });
24
- }, []);
12
+ if (clientScriptLoaded) {
13
+ window.nostojs((api: any) => {
14
+ api
15
+ .defaultSession()
16
+ .setVariation(currentVariation)
17
+ .setResponseMode("HTML")
18
+ .viewProduct(product)
19
+ .setPlacements(api.placements.getPlacements())
20
+ .load()
21
+ .then((data: object) => {
22
+ // @ts-ignore
23
+ api.placements.injectCampaigns(data.recommendations);
24
+ });
25
+ });
26
+ }
27
+ }, [clientScriptLoaded, currentVariation]);
25
28
 
26
29
  return (
27
30
  <>
@@ -29,81 +32,79 @@ const NostoProduct: React.FC<{ product: string; tagging: Product }> = ({
29
32
  product
30
33
  </div>
31
34
  <div className="nosto_product" style={{ display: "none" }}>
32
- {tagging.variationId && (
35
+ {tagging?.variationId && (
33
36
  <span className="variation_id">{tagging.variationId}</span>
34
37
  )}
35
- {tagging.productId && (
36
- <span className="product_id">{tagging.productId}</span>
37
- )}
38
- {tagging.name && <span className="name">{tagging.name}</span>}
39
- {tagging.url && <span className="url">{tagging.url.toString()}</span>}
40
- {tagging.imageUrl && (
38
+ {product && <span className="product_id">{product}</span>}
39
+ {tagging?.name && <span className="name">{tagging.name}</span>}
40
+ {tagging?.url && <span className="url">{tagging.url.toString()}</span>}
41
+ {tagging?.imageUrl && (
41
42
  <span className="image_url">{tagging.imageUrl.toString()}</span>
42
43
  )}
43
- {tagging.availability && (
44
+ {tagging?.availability && (
44
45
  <span className="availability">{tagging.availability}</span>
45
46
  )}
46
- {tagging.price && <span className="price">{tagging.price}</span>}
47
- {tagging.listPrice && (
47
+ {tagging?.price && <span className="price">{tagging.price}</span>}
48
+ {tagging?.listPrice && (
48
49
  <span className="list_price">{tagging.listPrice}</span>
49
50
  )}
50
- {tagging.priceCurrencyCode && (
51
+ {tagging?.priceCurrencyCode && (
51
52
  <span className="price_currency_code">
52
53
  {tagging.priceCurrencyCode}
53
54
  </span>
54
55
  )}
55
- {tagging.brand && <span className="brand">{tagging.brand}</span>}
56
- {tagging.description && (
56
+ {tagging?.brand && <span className="brand">{tagging.brand}</span>}
57
+ {tagging?.description && (
57
58
  <span className="description">{tagging.description}</span>
58
59
  )}
59
- {tagging.googleCategory && (
60
+ {tagging?.googleCategory && (
60
61
  <span className="description">{tagging.googleCategory}</span>
61
62
  )}
62
- {tagging.condition && (
63
+ {tagging?.condition && (
63
64
  <span className="condition">{tagging.condition}</span>
64
65
  )}
65
- {tagging.gender && <span className="gender">{tagging.gender}</span>}
66
- {tagging.ageGroup && (
66
+ {tagging?.gender && <span className="gender">{tagging.gender}</span>}
67
+ {tagging?.ageGroup && (
67
68
  <span className="age_group">{tagging.ageGroup}</span>
68
69
  )}
69
- {tagging.gtin && <span className="gtin">{tagging.gtin}</span>}
70
- {tagging.category &&
71
- tagging.category.map((category, index) => (
70
+ {tagging?.gtin && <span className="gtin">{tagging.gtin}</span>}
71
+ {tagging?.category &&
72
+ tagging?.category.map((category, index) => (
72
73
  <span className="category" key={index}>
73
74
  {category}
74
75
  </span>
75
76
  ))}
76
- {tagging.tags1 &&
77
+ {tagging?.tags1 &&
77
78
  tagging.tags1.map((tag, index) => (
78
79
  <span className="tag1" key={index}>
79
80
  {tag}
80
81
  </span>
81
82
  ))}
82
- {tagging.tags2 &&
83
+ {tagging?.tags2 &&
83
84
  tagging.tags2.map((tag, index) => (
84
85
  <span className="tag2" key={index}>
85
86
  {tag}
86
87
  </span>
87
88
  ))}
88
- {tagging.tags3 &&
89
+ {tagging?.tags3 &&
89
90
  tagging.tags3.map((tag, index) => (
90
91
  <span className="tag3" key={index}>
91
92
  {tag}
92
93
  </span>
93
94
  ))}
94
- {tagging.ratingValue && (
95
+ {tagging?.ratingValue && (
95
96
  <span className="rating_value">{tagging.ratingValue}</span>
96
97
  )}
97
- {tagging.reviewCount && (
98
+ {tagging?.reviewCount && (
98
99
  <span className="review_count">{tagging.reviewCount}</span>
99
100
  )}
100
- {tagging.alternateImageUrls &&
101
+ {tagging?.alternateImageUrls &&
101
102
  tagging.alternateImageUrls.map((url, index) => (
102
103
  <span className="alternate_image_url" key={index}>
103
104
  {url.toString()}
104
105
  </span>
105
106
  ))}
106
- {tagging.customFields &&
107
+ {tagging?.customFields &&
107
108
  Object.keys(tagging.customFields).map(
108
109
  (field, index) =>
109
110
  tagging.customFields &&
@@ -113,24 +114,24 @@ const NostoProduct: React.FC<{ product: string; tagging: Product }> = ({
113
114
  </span>
114
115
  )
115
116
  )}
116
- {tagging.skus &&
117
+ {tagging?.skus &&
117
118
  tagging.skus.map((sku, index) => (
118
119
  <span className="nosto_sku" key={index}>
119
- {sku.id && <span className="product_id">{sku.id}</span>}
120
- {sku.name && <span className="name">{sku.name}</span>}
121
- {sku.price && <span className="price">{sku.price}</span>}
122
- {sku.listPrice && (
120
+ {sku?.id && <span className="product_id">{sku.id}</span>}
121
+ {sku?.name && <span className="name">{sku.name}</span>}
122
+ {sku?.price && <span className="price">{sku.price}</span>}
123
+ {sku?.listPrice && (
123
124
  <span className="list_price">{sku.listPrice}</span>
124
125
  )}
125
- {sku.url && <span className="url">{sku.url.toString()}</span>}
126
- {sku.imageUrl && (
126
+ {sku?.url && <span className="url">{sku.url.toString()}</span>}
127
+ {sku?.imageUrl && (
127
128
  <span className="image_url">{sku.imageUrl.toString()}</span>
128
129
  )}
129
- {sku.gtin && <span className="gtin">{sku.gtin}</span>}
130
- {sku.availability && (
130
+ {sku?.gtin && <span className="gtin">{sku.gtin}</span>}
131
+ {sku?.availability && (
131
132
  <span className="availability">{sku.availability}</span>
132
133
  )}
133
- {sku.customFields &&
134
+ {sku?.customFields &&
134
135
  Object.keys(sku.customFields).map(
135
136
  (field, index) =>
136
137
  sku.customFields &&
@@ -0,0 +1,25 @@
1
+ import { createContext, useContext } from "react";
2
+
3
+ export interface NostoInterface {
4
+ account: string;
5
+ clientScriptLoaded: boolean;
6
+ currentVariation: string;
7
+ }
8
+
9
+ /* tslint:disable:no-empty */
10
+ export const NostoContext = createContext<NostoInterface>({
11
+ // @ts-ignore
12
+ account: undefined,
13
+ currentVariation: "",
14
+ });
15
+
16
+ /* tslint:enable:no-empty */
17
+ export function useNostoContext() {
18
+ const context = useContext(NostoContext);
19
+
20
+ if (!context) {
21
+ throw new Error("No nosto context found");
22
+ }
23
+
24
+ return context;
25
+ }
@@ -0,0 +1,58 @@
1
+ import React, { useEffect } from "react";
2
+ import { NostoContext } from "./context.client";
3
+
4
+ interface NostoProviderProps {
5
+ account: string;
6
+ currentVariation: string;
7
+ host: string;
8
+ children: React.ReactElement;
9
+ multiCurrency: boolean;
10
+ }
11
+
12
+ const NostoProvider: React.FC<NostoProviderProps> = ({
13
+ account,
14
+ currentVariation = "",
15
+ multiCurrency = false,
16
+ host,
17
+ children,
18
+ }) => {
19
+ const [clientScriptLoadedState, setClientScriptLoadedState] =
20
+ React.useState(false);
21
+ const clientScriptLoaded = React.useMemo(
22
+ () => clientScriptLoadedState,
23
+ [clientScriptLoadedState]
24
+ );
25
+
26
+ //Pass currentVariation as empty string if multiCurrency is disabled
27
+ currentVariation = multiCurrency ? currentVariation : "";
28
+
29
+ useEffect(() => {
30
+ if (!document.querySelectorAll("[nosto-client-script]").length) {
31
+ const script = document.createElement("script");
32
+ script.type = "text/javascript";
33
+ script.src = "//" + (host || "connect.nosto.com") + "/include/" + account;
34
+ script.async = true;
35
+ script.setAttribute("nosto-client-script", "");
36
+ script.onload = () => {
37
+ console.log("Nosto client script loaded");
38
+ setClientScriptLoadedState(true);
39
+ };
40
+ document.head.appendChild(script);
41
+ }
42
+
43
+ window.nostojs = (cb: Function) =>
44
+ (window.nostojs.q = window.nostojs.q || []).push(cb);
45
+ // @ts-ignore
46
+ window.nostojs((api) => api.setAutoLoad(false));
47
+ }, []);
48
+
49
+ return (
50
+ <NostoContext.Provider
51
+ value={{ account, clientScriptLoaded, currentVariation }}
52
+ >
53
+ {children}
54
+ </NostoContext.Provider>
55
+ );
56
+ };
57
+
58
+ export default NostoProvider;
@@ -8,24 +8,30 @@ declare global {
8
8
 
9
9
  export * from "./types";
10
10
  // noinspection JSUnusedGlobalSymbols
11
- export { default as Nosto404 } from "./components/Fohofo";
11
+ export { default as Nosto404 } from "./components/Fohofo/index.client";
12
12
  // noinspection JSUnusedGlobalSymbols
13
- export { default as NostoOther } from "./components/Other";
13
+ export { default as NostoOther } from "./components/Other/index.client";
14
14
  // noinspection JSUnusedGlobalSymbols
15
- export { default as NostoCheckout } from "./components/Checkout";
15
+ export { default as NostoCheckout } from "./components/Checkout/index.client";
16
16
  // noinspection JSUnusedGlobalSymbols
17
- export { default as NostoProduct } from "./components/Product";
17
+ export { default as NostoProduct } from "./components/Product/index.client";
18
18
  // noinspection JSUnusedGlobalSymbols
19
- export { default as NostoCategory } from "./components/Category";
19
+ export { default as NostoCategory } from "./components/Category/index.client";
20
20
  // noinspection JSUnusedGlobalSymbols
21
- export { default as NostoSearch } from "./components/Search";
21
+ export { default as NostoSearch } from "./components/Search/index.client";
22
22
  // noinspection JSUnusedGlobalSymbols
23
- export { default as NostoOrder } from "./components/Order";
23
+ export { default as NostoOrder } from "./components/Order/index.client";
24
24
  // noinspection JSUnusedGlobalSymbols
25
- export { default as NostoHome } from "./components/Home";
25
+ export { default as NostoHome } from "./components/Home/index.client";
26
26
  // noinspection JSUnusedGlobalSymbols
27
- export { default as NostoPlacement } from "./components/Placement";
27
+ export { default as NostoPlacement } from "./components/Placement/index.client";
28
28
  // noinspection JSUnusedGlobalSymbols
29
- export { default as NostoProvider } from "./components/Provider";
29
+ export { default as NostoProvider } from "./components/Provider/index.client";
30
30
  // noinspection JSUnusedGlobalSymbols
31
- export { default as NostoSession } from "./components/Session";
31
+ export {
32
+ NostoContext,
33
+ useNostoContext,
34
+ } from "./components/Provider/context.client";
35
+ // noinspection JSUnusedGlobalSymbols
36
+ export { default as NostoSession } from "./components/Session/index.client";
37
+ //
@@ -1,32 +0,0 @@
1
- import React, { useEffect } from "react";
2
-
3
- const NostoCategory: React.FC<{ category: string }> = ({ category }) => {
4
- useEffect(() => {
5
- // @ts-ignore
6
- window.nostojs((api) => {
7
- api
8
- .defaultSession()
9
- .setResponseMode("HTML")
10
- .viewCategory(category)
11
- .setPlacements(api.placements.getPlacements())
12
- .load()
13
- .then((data: object) => {
14
- // @ts-ignore
15
- api.placements.injectCampaigns(data.recommendations);
16
- });
17
- });
18
- }, []);
19
-
20
- return (
21
- <>
22
- <div className="nosto_page_type" style={{ display: "none" }}>
23
- category
24
- </div>
25
- <div className="nosto_category" style={{ display: "none" }}>
26
- {category}
27
- </div>
28
- </>
29
- );
30
- };
31
-
32
- export default NostoCategory;
@@ -1,29 +0,0 @@
1
- import React, { useEffect } from "react";
2
-
3
- const NostoHome: React.FC = () => {
4
- useEffect(() => {
5
- // @ts-ignore
6
- window.nostojs((api) => {
7
- api
8
- .defaultSession()
9
- .setResponseMode("HTML")
10
- .viewFrontPage()
11
- .setPlacements(api.placements.getPlacements())
12
- .load()
13
- .then((data: object) => {
14
- // @ts-ignore
15
- api.placements.injectCampaigns(data.recommendations);
16
- });
17
- });
18
- }, []);
19
-
20
- return (
21
- <>
22
- <div className="nosto_page_type" style={{ display: "none" }}>
23
- front
24
- </div>
25
- </>
26
- );
27
- };
28
-
29
- export default NostoHome;
@@ -1,12 +0,0 @@
1
- import { createContext } from "react";
2
-
3
- export interface NostoInterface {
4
- account: string;
5
- }
6
-
7
- /* tslint:disable:no-empty */
8
- export const NostoContext = createContext<NostoInterface>({
9
- // @ts-ignore
10
- account: undefined
11
- });
12
- /* tslint:enable:no-empty */
@@ -1,37 +0,0 @@
1
- import React, { useEffect } from "react";
2
- import { NostoContext } from "./context";
3
-
4
- interface NostoProviderProps {
5
- account: string;
6
- currentVariation: string;
7
- host: string;
8
- children: React.ReactElement;
9
- }
10
-
11
- const NostoProvider: React.FC<NostoProviderProps> = ({
12
- account,
13
- currentVariation,
14
- host,
15
- children,
16
- }) => {
17
- useEffect(() => {
18
- const script = document.createElement("script");
19
- script.type = "text/javascript";
20
- script.src = "//" + (host || "connect.nosto.com") + "/include/" + account;
21
- script.async = true;
22
- document.head.appendChild(script);
23
-
24
- window.nostojs = (cb: Function) =>
25
- (window.nostojs.q = window.nostojs.q || []).push(cb);
26
- // @ts-ignore
27
- window.nostojs((api) => api.setAutoLoad(false));
28
- }, []);
29
-
30
- return (
31
- <NostoContext.Provider value={{ account }}>
32
- {children}
33
- </NostoContext.Provider>
34
- );
35
- };
36
-
37
- export default NostoProvider;