@nosto/nosto-react 0.1.3 → 0.1.6

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,16 @@
1
1
  import * as React from "react";
2
- import React__default, { useEffect, createContext, useContext } from "react";
2
+ import React__default, { createContext, useContext, useEffect } from "react";
3
+ const NostoContext = createContext({
4
+ account: void 0,
5
+ currentVariation: ""
6
+ });
7
+ function useNostoContext() {
8
+ const context = useContext(NostoContext);
9
+ if (!context) {
10
+ throw new Error("No nosto context found");
11
+ }
12
+ return context;
13
+ }
3
14
  var jsxRuntime = { exports: {} };
4
15
  var reactJsxRuntime_production_min = {};
5
16
  /**
@@ -14,14 +25,14 @@ var reactJsxRuntime_production_min = {};
14
25
  var f = React__default, k = Symbol.for("react.element"), l = Symbol.for("react.fragment"), m = Object.prototype.hasOwnProperty, n = f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, p = { key: true, ref: true, __self: true, __source: true };
15
26
  function q(c, a, g) {
16
27
  var b, d = {}, e = null, h = null;
17
- g !== void 0 && (e = "" + g);
18
- a.key !== void 0 && (e = "" + a.key);
19
- a.ref !== void 0 && (h = a.ref);
28
+ void 0 !== g && (e = "" + g);
29
+ void 0 !== a.key && (e = "" + a.key);
30
+ void 0 !== a.ref && (h = a.ref);
20
31
  for (b in a)
21
32
  m.call(a, b) && !p.hasOwnProperty(b) && (d[b] = a[b]);
22
33
  if (c && c.defaultProps)
23
34
  for (b in a = c.defaultProps, a)
24
- d[b] === void 0 && (d[b] = a[b]);
35
+ void 0 === d[b] && (d[b] = a[b]);
25
36
  return { $$typeof: k, type: c, key: e, ref: h, props: d, _owner: n.current };
26
37
  }
27
38
  reactJsxRuntime_production_min.Fragment = l;
@@ -34,13 +45,19 @@ const jsx = jsxRuntime.exports.jsx;
34
45
  const jsxs = jsxRuntime.exports.jsxs;
35
46
  const Fragment = jsxRuntime.exports.Fragment;
36
47
  const NostoFohofo = () => {
48
+ const {
49
+ clientScriptLoaded,
50
+ currentVariation
51
+ } = useNostoContext();
37
52
  useEffect(() => {
38
- window.nostojs((api) => {
39
- api.defaultSession().setResponseMode("HTML").viewNotFound().setPlacements(api.placements.getPlacements()).load().then((data) => {
40
- api.placements.injectCampaigns(data.recommendations);
53
+ if (clientScriptLoaded) {
54
+ window.nostojs((api) => {
55
+ api.defaultSession().setVariation(currentVariation).setResponseMode("HTML").viewNotFound().setPlacements(api.placements.getPlacements()).load().then((data) => {
56
+ api.placements.injectCampaigns(data.recommendations);
57
+ });
41
58
  });
42
- });
43
- }, []);
59
+ }
60
+ }, [clientScriptLoaded, currentVariation]);
44
61
  return /* @__PURE__ */ jsx(Fragment, {
45
62
  children: /* @__PURE__ */ jsx("div", {
46
63
  className: "nosto_page_type",
@@ -52,13 +69,19 @@ const NostoFohofo = () => {
52
69
  });
53
70
  };
54
71
  const NostoOther = () => {
72
+ const {
73
+ clientScriptLoaded,
74
+ currentVariation
75
+ } = useNostoContext();
55
76
  useEffect(() => {
56
- window.nostojs((api) => {
57
- api.defaultSession().setResponseMode("HTML").viewOther().setPlacements(api.placements.getPlacements()).load().then((data) => {
58
- api.placements.injectCampaigns(data.recommendations);
77
+ if (clientScriptLoaded) {
78
+ window.nostojs((api) => {
79
+ api.defaultSession().setVariation(currentVariation).setResponseMode("HTML").viewOther().setPlacements(api.placements.getPlacements()).load().then((data) => {
80
+ api.placements.injectCampaigns(data.recommendations);
81
+ });
59
82
  });
60
- });
61
- }, []);
83
+ }
84
+ }, [clientScriptLoaded, currentVariation]);
62
85
  return /* @__PURE__ */ jsx(Fragment, {
63
86
  children: /* @__PURE__ */ jsx("div", {
64
87
  className: "nosto_page_type",
@@ -70,13 +93,19 @@ const NostoOther = () => {
70
93
  });
71
94
  };
72
95
  const NostoCheckout = () => {
96
+ const {
97
+ clientScriptLoaded,
98
+ currentVariation
99
+ } = useNostoContext();
73
100
  useEffect(() => {
74
- window.nostojs((api) => {
75
- api.defaultSession().setResponseMode("HTML").viewCart().setPlacements(api.placements.getPlacements()).load().then((data) => {
76
- api.placements.injectCampaigns(data.recommendations);
101
+ if (clientScriptLoaded) {
102
+ window.nostojs((api) => {
103
+ api.defaultSession().setVariation(currentVariation).setResponseMode("HTML").viewCart().setPlacements(api.placements.getPlacements()).load().then((data) => {
104
+ api.placements.injectCampaigns(data.recommendations);
105
+ });
77
106
  });
78
- });
79
- }, []);
107
+ }
108
+ }, [clientScriptLoaded, currentVariation]);
80
109
  return /* @__PURE__ */ jsx(Fragment, {
81
110
  children: /* @__PURE__ */ jsx("div", {
82
111
  className: "nosto_page_type",
@@ -87,17 +116,6 @@ const NostoCheckout = () => {
87
116
  })
88
117
  });
89
118
  };
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;
100
- }
101
119
  const NostoProduct = ({
102
120
  product,
103
121
  tagging
@@ -112,9 +130,9 @@ const NostoProduct = ({
112
130
  api.defaultSession().setVariation(currentVariation).setResponseMode("HTML").viewProduct(product).setPlacements(api.placements.getPlacements()).load().then((data) => {
113
131
  api.placements.injectCampaigns(data.recommendations);
114
132
  });
115
- });
133
+ }, [clientScriptLoaded, currentVariation, product]);
116
134
  }
117
- }, [clientScriptLoaded, currentVariation]);
135
+ });
118
136
  return /* @__PURE__ */ jsxs(Fragment, {
119
137
  children: [/* @__PURE__ */ jsx("div", {
120
138
  className: "nosto_page_type",
@@ -268,13 +286,19 @@ const NostoCategory = ({
268
286
  const NostoSearch = ({
269
287
  query
270
288
  }) => {
289
+ const {
290
+ clientScriptLoaded,
291
+ currentVariation
292
+ } = useNostoContext();
271
293
  useEffect(() => {
272
- window.nostojs((api) => {
273
- api.defaultSession().setResponseMode("HTML").viewSearch(query).setPlacements(api.placements.getPlacements()).load().then((data) => {
274
- api.placements.injectCampaigns(data.recommendations);
294
+ if (clientScriptLoaded) {
295
+ window.nostojs((api) => {
296
+ api.defaultSession().setVariation(currentVariation).setResponseMode("HTML").viewSearch(query).setPlacements(api.placements.getPlacements()).load().then((data) => {
297
+ api.placements.injectCampaigns(data.recommendations);
298
+ });
275
299
  });
276
- });
277
- }, []);
300
+ }
301
+ }, [clientScriptLoaded, currentVariation, query]);
278
302
  return /* @__PURE__ */ jsxs(Fragment, {
279
303
  children: [/* @__PURE__ */ jsx("div", {
280
304
  className: "nosto_page_type",
@@ -315,13 +339,19 @@ var isRegex = function(obj) {
315
339
  const NostoOrder = ({
316
340
  order
317
341
  }) => {
342
+ const {
343
+ clientScriptLoaded,
344
+ currentVariation
345
+ } = useNostoContext();
318
346
  useEffect(() => {
319
- window.nostojs((api) => {
320
- api.defaultSession().setResponseMode("HTML").addOrder(snakeize(order)).setPlacements(api.placements.getPlacements()).load().then((data) => {
321
- api.placements.injectCampaigns(data.recommendations);
347
+ if (clientScriptLoaded) {
348
+ window.nostojs((api) => {
349
+ api.defaultSession().setVariation(currentVariation).setResponseMode("HTML").addOrder(snakeize(order)).setPlacements(api.placements.getPlacements()).load().then((data) => {
350
+ api.placements.injectCampaigns(data.recommendations);
351
+ });
322
352
  });
323
- });
324
- }, []);
353
+ }
354
+ }, [clientScriptLoaded, currentVariation]);
325
355
  return /* @__PURE__ */ jsxs(Fragment, {
326
356
  children: [/* @__PURE__ */ jsx("div", {
327
357
  className: "nosto_page_type",
@@ -522,15 +552,18 @@ const NostoSession = ({
522
552
  cart,
523
553
  customer
524
554
  }) => {
555
+ const {
556
+ clientScriptLoaded
557
+ } = useNostoContext();
525
558
  useDeepCompareEffect(() => {
526
- const currentUser = customer ? customer : void 0;
527
- console.debug(currentUser);
528
- const currentCart = cart ? cart : void 0;
529
- console.debug(currentCart);
530
- window.nostojs((api) => {
531
- api.defaultSession().setResponseMode("HTML").setCart(snakeize(currentCart)).viewOther().load();
532
- });
533
- }, [cart || [], customer || {}]);
534
- return /* @__PURE__ */ jsx("div", {});
559
+ const currentCart = cart ? snakeize(cart) : void 0;
560
+ const currentCustomer = customer ? snakeize(customer) : void 0;
561
+ if (clientScriptLoaded) {
562
+ window.nostojs((api) => {
563
+ api.defaultSession().setResponseMode("HTML").setCart(currentCart).setCustomer(currentCustomer).viewOther().load();
564
+ });
565
+ }
566
+ }, [clientScriptLoaded, cart || [], customer || {}]);
567
+ return /* @__PURE__ */ jsx(Fragment, {});
535
568
  };
536
569
  export { NostoFohofo as Nosto404, NostoCategory, NostoCheckout, NostoContext, NostoHome, NostoOrder, NostoOther, NostoPlacement, NostoProduct, NostoProvider, NostoSearch, NostoSession, useNostoContext };
@@ -1,4 +1,4 @@
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={};/**
1
+ (function(l,i){typeof exports=="object"&&typeof module!="undefined"?i(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],i):(l=typeof globalThis!="undefined"?globalThis:l||self,i(l["@nosto/nosto-react"]={},l.React))})(this,function(l,i){"use strict";function L(t){return t&&typeof t=="object"&&"default"in t?t:{default:t}}function O(t){if(t&&t.__esModule)return t;var e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});return t&&Object.keys(t).forEach(function(n){if(n!=="default"){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}}),e.default=t,Object.freeze(e)}var _=L(i),h=O(i);const v=i.createContext({account:void 0,currentVariation:""});function a(){const t=i.useContext(v);if(!t)throw new Error("No nosto context found");return t}var y={exports:{}},N={};/**
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 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"}})});
9
+ */var M=_.default,E=Symbol.for("react.element"),V=Symbol.for("react.fragment"),T=Object.prototype.hasOwnProperty,F=M.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,x={key:!0,ref:!0,__self:!0,__source:!0};function C(t,e,n){var r,s={},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)T.call(e,r)&&!x.hasOwnProperty(r)&&(s[r]=e[r]);if(t&&t.defaultProps)for(r in e=t.defaultProps,e)s[r]===void 0&&(s[r]=e[r]);return{$$typeof:E,type:t,key:c,ref:d,props:s,_owner:F.current}}N.Fragment=V,N.jsx=C,N.jsxs=C,y.exports=N;const o=y.exports.jsx,u=y.exports.jsxs,p=y.exports.Fragment,R=()=>{const{clientScriptLoaded:t,currentVariation:e}=a();return i.useEffect(()=>{t&&window.nostojs(n=>{n.defaultSession().setVariation(e).setResponseMode("HTML").viewNotFound().setPlacements(n.placements.getPlacements()).load().then(r=>{n.placements.injectCampaigns(r.recommendations)})})},[t,e]),o(p,{children:o("div",{className:"nosto_page_type",style:{display:"none"},children:"notfound"})})},A=()=>{const{clientScriptLoaded:t,currentVariation:e}=a();return i.useEffect(()=>{t&&window.nostojs(n=>{n.defaultSession().setVariation(e).setResponseMode("HTML").viewOther().setPlacements(n.placements.getPlacements()).load().then(r=>{n.placements.injectCampaigns(r.recommendations)})})},[t,e]),o(p,{children:o("div",{className:"nosto_page_type",style:{display:"none"},children:"other"})})},D=()=>{const{clientScriptLoaded:t,currentVariation:e}=a();return i.useEffect(()=>{t&&window.nostojs(n=>{n.defaultSession().setVariation(e).setResponseMode("HTML").viewCart().setPlacements(n.placements.getPlacements()).load().then(r=>{n.placements.injectCampaigns(r.recommendations)})})},[t,e]),o(p,{children:o("div",{className:"nosto_page_type",style:{display:"none"},children:"cart"})})},H=({product:t,tagging:e})=>{const{clientScriptLoaded:n,currentVariation:r}=a();return i.useEffect(()=>{n&&window.nostojs(s=>{s.defaultSession().setVariation(r).setResponseMode("HTML").viewProduct(t).setPlacements(s.placements.getPlacements()).load().then(c=>{s.placements.injectCampaigns(c.recommendations)})},[n,r,t])}),u(p,{children:[o("div",{className:"nosto_page_type",style:{display:"none"},children:"product"}),u("div",{className:"nosto_product",style:{display:"none"},children:[(e==null?void 0:e.variationId)&&o("span",{className:"variation_id",children:e.variationId}),t&&o("span",{className:"product_id",children:t}),(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((s,c)=>o("span",{className:"category",children:s},c))),(e==null?void 0:e.tags1)&&e.tags1.map((s,c)=>o("span",{className:"tag1",children:s},c)),(e==null?void 0:e.tags2)&&e.tags2.map((s,c)=>o("span",{className:"tag2",children:s},c)),(e==null?void 0:e.tags3)&&e.tags3.map((s,c)=>o("span",{className:"tag3",children:s},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((s,c)=>o("span",{className:"alternate_image_url",children:s.toString()},c)),(e==null?void 0:e.customFields)&&Object.keys(e.customFields).map((s,c)=>e.customFields&&e.customFields[s]&&o("span",{className:s,children:e.customFields[s]},c)),(e==null?void 0:e.skus)&&e.skus.map((s,c)=>u("span",{className:"nosto_sku",children:[(s==null?void 0:s.id)&&o("span",{className:"product_id",children:s.id}),(s==null?void 0:s.name)&&o("span",{className:"name",children:s.name}),(s==null?void 0:s.price)&&o("span",{className:"price",children:s.price}),(s==null?void 0:s.listPrice)&&o("span",{className:"list_price",children:s.listPrice}),(s==null?void 0:s.url)&&o("span",{className:"url",children:s.url.toString()}),(s==null?void 0:s.imageUrl)&&o("span",{className:"image_url",children:s.imageUrl.toString()}),(s==null?void 0:s.gtin)&&o("span",{className:"gtin",children:s.gtin}),(s==null?void 0:s.availability)&&o("span",{className:"availability",children:s.availability}),(s==null?void 0:s.customFields)&&Object.keys(s.customFields).map((d,S)=>s.customFields&&s.customFields[d]&&o("span",{className:d,children:s.customFields[d]},S))]},c))]})]})},U=({category:t})=>{const{clientScriptLoaded:e,currentVariation:n}=a();return i.useEffect(()=>{e&&window.nostojs(r=>{r.defaultSession().setVariation(n).setResponseMode("HTML").viewCategory(t).setPlacements(r.placements.getPlacements()).load().then(s=>{r.placements.injectCampaigns(s.recommendations)})})},[e,t,n]),u(p,{children:[o("div",{className:"nosto_page_type",style:{display:"none"},children:"category"}),o("div",{className:"nosto_category",style:{display:"none"},children:t})]})},b=({query:t})=>{const{clientScriptLoaded:e,currentVariation:n}=a();return i.useEffect(()=>{e&&window.nostojs(r=>{r.defaultSession().setVariation(n).setResponseMode("HTML").viewSearch(t).setPlacements(r.placements.getPlacements()).load().then(s=>{r.placements.injectCampaigns(s.recommendations)})})},[e,n,t]),u(p,{children:[o("div",{className:"nosto_page_type",style:{display:"none"},children:"search"}),o("div",{className:"nosto_search",style:{display:"none"},children:t})]})};var w=function t(e){return!e||typeof e!="object"||I(e)||z(e)?e:Array.isArray(e)?e.map(t):Object.keys(e).reduce(function(n,r){var s=r[0].toLowerCase()+r.slice(1).replace(/([A-Z]+)/g,function(c,d){return"_"+d.toLowerCase()});return n[s]=t(e[r]),n},{})},I=function(t){return Object.prototype.toString.call(t)==="[object Date]"},z=function(t){return Object.prototype.toString.call(t)==="[object RegExp]"};const q=({order:t})=>{const{clientScriptLoaded:e,currentVariation:n}=a();return i.useEffect(()=>{e&&window.nostojs(r=>{r.defaultSession().setVariation(n).setResponseMode("HTML").addOrder(w(t)).setPlacements(r.placements.getPlacements()).load().then(s=>{r.placements.injectCampaigns(s.recommendations)})})},[e,n]),u(p,{children:[o("div",{className:"nosto_page_type",style:{display:"none"},children:"order"}),o("div",{className:"nosto_order",style:{display:"none"},children:t.purchase.number})]})},B=()=>{const{clientScriptLoaded:t,currentVariation:e}=a();return i.useEffect(()=>{e&&console.log("currentVariation: ",e),t&&window.nostojs(n=>{n.defaultSession().setVariation(e).setResponseMode("HTML").viewFrontPage().setPlacements(n.placements.getPlacements()).load().then(r=>{n.placements.injectCampaigns(r.recommendations)})})},[t,e]),o(p,{children:o("div",{className:"nosto_page_type",style:{display:"none"},children:"front"})})},G=({id:t})=>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:t}),$=({account:t,currentVariation:e="",multiCurrency:n=!1,host:r,children:s})=>{const[c,d]=_.default.useState(!1),S=_.default.useMemo(()=>c,[c]);return e=n?e:"",i.useEffect(()=>{if(!document.querySelectorAll("[nosto-client-script]").length){const m=document.createElement("script");m.type="text/javascript",m.src="//"+(r||"connect.nosto.com")+"/include/"+t,m.async=!0,m.setAttribute("nosto-client-script",""),m.onload=()=>{console.log("Nosto client script loaded"),d(!0)},document.head.appendChild(m)}window.nostojs=m=>(window.nostojs.q=window.nostojs.q||[]).push(m),window.nostojs(m=>m.setAutoLoad(!1))},[]),o(v.Provider,{value:{account:t,clientScriptLoaded:S,currentVariation:e},children:s})};var j=Object.prototype.hasOwnProperty;function P(t,e,n){for(n of t.keys())if(f(n,e))return n}function f(t,e){var n,r,s;if(t===e)return!0;if(t&&e&&(n=t.constructor)===e.constructor){if(n===Date)return t.getTime()===e.getTime();if(n===RegExp)return t.toString()===e.toString();if(n===Array){if((r=t.length)===e.length)for(;r--&&f(t[r],e[r]););return r===-1}if(n===Set){if(t.size!==e.size)return!1;for(r of t)if(s=r,s&&typeof s=="object"&&(s=P(e,s),!s)||!e.has(s))return!1;return!0}if(n===Map){if(t.size!==e.size)return!1;for(r of t)if(s=r[0],s&&typeof s=="object"&&(s=P(e,s),!s)||!f(r[1],e.get(s)))return!1;return!0}if(n===ArrayBuffer)t=new Uint8Array(t),e=new Uint8Array(e);else if(n===DataView){if((r=t.byteLength)===e.byteLength)for(;r--&&t.getInt8(r)===e.getInt8(r););return r===-1}if(ArrayBuffer.isView(t)){if((r=t.byteLength)===e.byteLength)for(;r--&&t[r]===e[r];);return r===-1}if(!n||typeof t=="object"){r=0;for(n in t)if(j.call(t,n)&&++r&&!j.call(e,n)||!(n in e)||!f(t[n],e[n]))return!1;return Object.keys(e).length===r}}return t!==t&&e!==e}function J(t){var e=h.useRef(t),n=h.useRef(0);return f(t,e.current)||(e.current=t,n.current+=1),h.useMemo(function(){return e.current},[n.current])}function W(t,e){return h.useEffect(t,J(e))}const Y=({cart:t,customer:e})=>{const{clientScriptLoaded:n}=a();return W(()=>{const r=t?w(t):void 0,s=e?w(e):void 0;n&&window.nostojs(c=>{c.defaultSession().setResponseMode("HTML").setCart(r).setCustomer(s).viewOther().load()})},[n,t||[],e||{}]),o(p,{})};l.Nosto404=R,l.NostoCategory=U,l.NostoCheckout=D,l.NostoContext=v,l.NostoHome=B,l.NostoOrder=q,l.NostoOther=A,l.NostoPlacement=G,l.NostoProduct=H,l.NostoProvider=$,l.NostoSearch=b,l.NostoSession=Y,l.useNostoContext=a,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.3",
4
+ "version": "0.1.6",
5
5
  "author": "Mridang Agarwalla, Dominik Gilg",
6
6
  "license": "ISC",
7
7
  "repository": {
@@ -1,21 +1,26 @@
1
1
  import React, { useEffect } from "react";
2
+ import { useNostoContext } from "../Provider/context.client";
2
3
 
3
4
  const NostoCheckout: React.FC = () => {
5
+ const { clientScriptLoaded, currentVariation } = useNostoContext();
4
6
  useEffect(() => {
5
7
  // @ts-ignore
6
- window.nostojs((api) => {
7
- api
8
- .defaultSession()
9
- .setResponseMode("HTML")
10
- .viewCart()
11
- .setPlacements(api.placements.getPlacements())
12
- .load()
13
- .then((data: object) => {
14
- // @ts-ignore
15
- api.placements.injectCampaigns(data.recommendations);
16
- });
17
- });
18
- }, []);
8
+ if (clientScriptLoaded) {
9
+ window.nostojs((api: any) => {
10
+ api
11
+ .defaultSession()
12
+ .setVariation(currentVariation)
13
+ .setResponseMode("HTML")
14
+ .viewCart()
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, currentVariation]);
19
24
 
20
25
  return (
21
26
  <>
@@ -1,21 +1,26 @@
1
1
  import React, { useEffect } from "react";
2
+ import { useNostoContext } from "../Provider/context.client";
2
3
 
3
4
  const NostoFohofo: React.FC = () => {
5
+ const { clientScriptLoaded, currentVariation } = useNostoContext();
4
6
  useEffect(() => {
5
7
  // @ts-ignore
6
- window.nostojs((api) => {
7
- api
8
- .defaultSession()
9
- .setResponseMode("HTML")
10
- .viewNotFound()
11
- .setPlacements(api.placements.getPlacements())
12
- .load()
13
- .then((data: object) => {
14
- // @ts-ignore
15
- api.placements.injectCampaigns(data.recommendations);
16
- });
17
- });
18
- }, []);
8
+ if (clientScriptLoaded) {
9
+ window.nostojs((api: any) => {
10
+ api
11
+ .defaultSession()
12
+ .setVariation(currentVariation)
13
+ .setResponseMode("HTML")
14
+ .viewNotFound()
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, currentVariation]);
19
24
 
20
25
  return (
21
26
  <>
@@ -1,27 +1,32 @@
1
1
  import { Purchase } from "../../types";
2
2
  import React, { useEffect } from "react";
3
3
  import snakeize from "snakeize";
4
+ import { useNostoContext } from "../Provider/context.client";
4
5
 
5
6
  export interface OrderProps {
6
7
  purchase: Purchase;
7
8
  }
8
9
 
9
10
  const NostoOrder: React.FC<{ order: OrderProps }> = ({ order }) => {
11
+ const { clientScriptLoaded, currentVariation } = useNostoContext();
10
12
  useEffect(() => {
11
13
  // @ts-ignore
12
- window.nostojs((api) => {
13
- api
14
- .defaultSession()
15
- .setResponseMode("HTML")
16
- .addOrder(snakeize(order))
17
- .setPlacements(api.placements.getPlacements())
18
- .load()
19
- .then((data: object) => {
20
- // @ts-ignore
21
- api.placements.injectCampaigns(data.recommendations);
22
- });
23
- });
24
- }, []);
14
+ if (clientScriptLoaded) {
15
+ window.nostojs((api: any) => {
16
+ api
17
+ .defaultSession()
18
+ .setVariation(currentVariation)
19
+ .setResponseMode("HTML")
20
+ .addOrder(snakeize(order))
21
+ .setPlacements(api.placements.getPlacements())
22
+ .load()
23
+ .then((data: object) => {
24
+ // @ts-ignore
25
+ api.placements.injectCampaigns(data.recommendations);
26
+ });
27
+ });
28
+ }
29
+ }, [clientScriptLoaded, currentVariation]);
25
30
 
26
31
  return (
27
32
  <>
@@ -1,21 +1,26 @@
1
1
  import React, { useEffect } from "react";
2
+ import { useNostoContext } from "../Provider/context.client";
2
3
 
3
4
  const NostoOther: React.FC = () => {
5
+ const { clientScriptLoaded, currentVariation } = useNostoContext();
4
6
  useEffect(() => {
5
7
  // @ts-ignore
6
- window.nostojs((api) => {
7
- api
8
- .defaultSession()
9
- .setResponseMode("HTML")
10
- .viewOther()
11
- .setPlacements(api.placements.getPlacements())
12
- .load()
13
- .then((data: object) => {
14
- // @ts-ignore
15
- api.placements.injectCampaigns(data.recommendations);
16
- });
17
- });
18
- }, []);
8
+ if (clientScriptLoaded) {
9
+ window.nostojs((api: any) => {
10
+ api
11
+ .defaultSession()
12
+ .setVariation(currentVariation)
13
+ .setResponseMode("HTML")
14
+ .viewOther()
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, currentVariation]);
19
24
 
20
25
  return (
21
26
  <>
@@ -10,21 +10,24 @@ const NostoProduct: React.FC<{ product: string; tagging: Product }> = ({
10
10
  useEffect(() => {
11
11
  // @ts-ignore
12
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
- });
13
+ window.nostojs(
14
+ (api: any) => {
15
+ api
16
+ .defaultSession()
17
+ .setVariation(currentVariation)
18
+ .setResponseMode("HTML")
19
+ .viewProduct(product)
20
+ .setPlacements(api.placements.getPlacements())
21
+ .load()
22
+ .then((data: object) => {
23
+ // @ts-ignore
24
+ api.placements.injectCampaigns(data.recommendations);
25
+ });
26
+ },
27
+ [clientScriptLoaded, currentVariation, product]
28
+ );
26
29
  }
27
- }, [clientScriptLoaded, currentVariation]);
30
+ });
28
31
 
29
32
  return (
30
33
  <>
@@ -1,21 +1,26 @@
1
1
  import React, { useEffect } from "react";
2
+ import { useNostoContext } from "../Provider/context.client";
2
3
 
3
4
  const NostoSearch: React.FC<{ query: string }> = ({ query }) => {
5
+ const { clientScriptLoaded, currentVariation } = useNostoContext();
4
6
  useEffect(() => {
5
7
  // @ts-ignore
6
- window.nostojs((api) => {
7
- api
8
- .defaultSession()
9
- .setResponseMode("HTML")
10
- .viewSearch(query)
11
- .setPlacements(api.placements.getPlacements())
12
- .load()
13
- .then((data: object) => {
14
- // @ts-ignore
15
- api.placements.injectCampaigns(data.recommendations);
16
- });
17
- });
18
- }, []);
8
+ if (clientScriptLoaded) {
9
+ window.nostojs((api: any) => {
10
+ api
11
+ .defaultSession()
12
+ .setVariation(currentVariation)
13
+ .setResponseMode("HTML")
14
+ .viewSearch(query)
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, currentVariation, query]);
19
24
 
20
25
  return (
21
26
  <>
@@ -1,5 +1,6 @@
1
1
  import React from "react";
2
2
  import snakeize from "snakeize";
3
+ import { useNostoContext } from "../Provider/context.client";
3
4
 
4
5
  import useDeepCompareEffect from "use-deep-compare-effect";
5
6
  import { Cart, Customer } from "../../types";
@@ -10,24 +11,26 @@ interface NostoSessionProps {
10
11
  }
11
12
 
12
13
  const NostoSession: React.FC<NostoSessionProps> = ({ cart, customer }) => {
14
+ const { clientScriptLoaded } = useNostoContext();
13
15
  useDeepCompareEffect(() => {
14
- const currentUser = customer ? customer : undefined;
15
- console.debug(currentUser);
16
- const currentCart = cart ? cart : undefined;
17
- console.debug(currentCart);
16
+ const currentCart = cart ? snakeize(cart) : undefined;
17
+ const currentCustomer = customer ? snakeize(customer) : undefined;
18
18
 
19
19
  // @ts-ignore
20
- window.nostojs((api) => {
21
- api
22
- .defaultSession()
23
- .setResponseMode("HTML")
24
- .setCart(snakeize(currentCart))
25
- .viewOther()
26
- .load();
27
- });
28
- }, [cart || [], customer || {}]);
20
+ if (clientScriptLoaded) {
21
+ window.nostojs((api: any) => {
22
+ api
23
+ .defaultSession()
24
+ .setResponseMode("HTML")
25
+ .setCart(currentCart)
26
+ .setCustomer(currentCustomer)
27
+ .viewOther()
28
+ .load();
29
+ });
30
+ }
31
+ }, [clientScriptLoaded, cart || [], customer || {}]);
29
32
 
30
- return <div />;
33
+ return <></>;
31
34
  };
32
35
 
33
36
  export default NostoSession;