@nosto/nosto-react 0.4.0 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/README.md +5 -2
  2. package/dist/{index.es.client.js → index.es.js} +127 -168
  3. package/dist/index.umd.js +9 -0
  4. package/package.json +15 -10
  5. package/src/components/Nosto404.tsx +47 -0
  6. package/src/components/{Category/index.client.tsx → NostoCategory.tsx} +18 -38
  7. package/src/components/NostoCheckout.tsx +47 -0
  8. package/src/components/{Home/index.client.tsx → NostoHome.tsx} +17 -34
  9. package/src/components/{Order/index.client.tsx → NostoOrder.tsx} +22 -38
  10. package/src/components/NostoOther.tsx +46 -0
  11. package/src/components/{Placement/index.client.tsx → NostoPlacement.tsx} +5 -8
  12. package/src/components/{Product/index.client.tsx → NostoProduct.tsx} +37 -80
  13. package/src/components/NostoProvider.tsx +220 -0
  14. package/src/components/{Search/index.client.tsx → NostoSearch.tsx} +18 -36
  15. package/src/components/{Session/index.client.tsx → NostoSession.tsx} +14 -17
  16. package/src/components/context.ts +55 -0
  17. package/src/components/index.ts +14 -0
  18. package/src/index.ts +3 -0
  19. package/src/types.ts +112 -97
  20. package/src/utils/compare.ts +9 -9
  21. package/src/utils/hooks.ts +28 -8
  22. package/src/utils/object.ts +10 -11
  23. package/src/utils/snakeize.ts +11 -11
  24. package/dist/index.umd.client.js +0 -9
  25. package/src/components/Checkout/index.client.tsx +0 -64
  26. package/src/components/Fohofo/index.client.tsx +0 -64
  27. package/src/components/Other/index.client.tsx +0 -63
  28. package/src/components/Provider/context.client.ts +0 -45
  29. package/src/components/Provider/index.client.tsx +0 -222
  30. package/src/index.client.ts +0 -33
@@ -1,5 +1,5 @@
1
- import { useEffect } from "react";
2
- import { useNostoContext } from "../Provider/context.client";
1
+ import { useNostoContext } from "./context"
2
+ import { useNostoApi } from "../utils/hooks"
3
3
 
4
4
  /**
5
5
  * You can personalise your category and collection pages by using the NostoCategory component.
@@ -21,44 +21,24 @@ import { useNostoContext } from "../Provider/context.client";
21
21
  * If the category being viewed is `Mens >> Jackets`, you must provide the name as `/Mens/Jackets`.
22
22
  * You must ensure that the category path provided here matches that of the categories tagged in your products.
23
23
  *
24
- * @group Personalisation Components
24
+ * @group Components
25
25
  */
26
- export default function NostoCategory(props: {
27
- category: string;
28
- }): JSX.Element {
29
- const { category } = props;
30
- const {
31
- clientScriptLoaded,
32
- currentVariation,
33
- responseMode,
34
- recommendationComponent,
35
- useRenderCampaigns,
36
- } = useNostoContext();
26
+ export default function NostoCategory(props: { category: string; placements?: string[] }) {
27
+ const { category, placements } = props
28
+ const { recommendationComponent, useRenderCampaigns } = useNostoContext()
37
29
 
38
- const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("home");
30
+ const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("home")
39
31
 
40
- useEffect(() => {
41
- if (clientScriptLoaded && pageTypeUpdated) {
42
- window.nostojs((api) => {
43
- api
44
- .defaultSession()
45
- .setVariation(currentVariation)
46
- .setResponseMode(responseMode)
47
- .viewCategory(category)
48
- .setPlacements(api.placements.getPlacements())
49
- .load()
50
- .then((data) => {
51
- renderCampaigns(data, api);
52
- });
53
- });
54
- }
55
- }, [
56
- clientScriptLoaded,
57
- category,
58
- currentVariation,
59
- recommendationComponent,
60
- pageTypeUpdated,
61
- ]);
32
+ useNostoApi(
33
+ async (api) => {
34
+ const data = await api.defaultSession()
35
+ .viewCategory(category)
36
+ .setPlacements(placements || api.placements.getPlacements())
37
+ .load()
38
+ renderCampaigns(data, api)
39
+ },
40
+ [category, recommendationComponent, pageTypeUpdated]
41
+ )
62
42
 
63
43
  return (
64
44
  <>
@@ -69,5 +49,5 @@ export default function NostoCategory(props: {
69
49
  {category}
70
50
  </div>
71
51
  </>
72
- );
52
+ )
73
53
  }
@@ -0,0 +1,47 @@
1
+ import { useNostoContext } from "./context"
2
+ import { useNostoApi } from "../utils/hooks"
3
+
4
+ /**
5
+ * You can personalise your cart and checkout pages by using the NostoCheckout component.
6
+ * The component does not require any props.
7
+ *
8
+ * By default, your account, when created, has two cart-page placements named `categorypage-nosto-1` and `categorypage-nosto-2`.
9
+ * You may omit these and use any identifier you need.
10
+ * The identifiers used here are simply provided to illustrate the example.
11
+ *
12
+ * @example
13
+ * ```
14
+ * <div className="checkout-page">
15
+ * <NostoPlacement id="checkout-nosto-1" />
16
+ * <NostoPlacement id="checkout-nosto-2" />
17
+ * <NostoCheckout />
18
+ * </div>
19
+ * ```
20
+ *
21
+ * @group Components
22
+ */
23
+
24
+ export default function NostoCheckout(props: { placements?: string[] }) {
25
+ const { recommendationComponent, useRenderCampaigns } = useNostoContext()
26
+
27
+ const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("checkout")
28
+
29
+ useNostoApi(
30
+ async (api) => {
31
+ const data = await api.defaultSession()
32
+ .viewCart()
33
+ .setPlacements(props.placements || api.placements.getPlacements())
34
+ .load()
35
+ renderCampaigns(data, api)
36
+ },
37
+ [recommendationComponent, pageTypeUpdated]
38
+ )
39
+
40
+ return (
41
+ <>
42
+ <div className="nosto_page_type" style={{ display: "none" }}>
43
+ cart
44
+ </div>
45
+ </>
46
+ )
47
+ }
@@ -1,5 +1,5 @@
1
- import { useEffect } from "react";
2
- import { useNostoContext } from "../Provider/context.client";
1
+ import { useNostoContext } from "./context"
2
+ import { useNostoApi } from "../utils/hooks"
3
3
 
4
4
  /**
5
5
  * The `NostoHome` component must be used to personalise the home page. The component does not require any props.
@@ -22,40 +22,23 @@ import { useNostoContext } from "../Provider/context.client";
22
22
  * </div>
23
23
  * ```
24
24
  *
25
- * @group Personalisation Components
25
+ * @group Components
26
26
  */
27
- export default function NostoHome(): JSX.Element {
28
- const {
29
- clientScriptLoaded,
30
- currentVariation,
31
- responseMode,
32
- recommendationComponent,
33
- useRenderCampaigns,
34
- } = useNostoContext();
27
+ export default function NostoHome(props: { placements?: string[] }) {
28
+ const { recommendationComponent, useRenderCampaigns } = useNostoContext()
35
29
 
36
- const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("home");
30
+ const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("home")
37
31
 
38
- useEffect(() => {
39
- if (clientScriptLoaded && pageTypeUpdated) {
40
- window.nostojs((api) => {
41
- api
42
- .defaultSession()
43
- .setVariation(currentVariation)
44
- .setResponseMode(responseMode)
45
- .viewFrontPage()
46
- .setPlacements(api.placements.getPlacements())
47
- .load()
48
- .then((data) => {
49
- renderCampaigns(data, api);
50
- });
51
- });
52
- }
53
- }, [
54
- clientScriptLoaded,
55
- currentVariation,
56
- recommendationComponent,
57
- pageTypeUpdated,
58
- ]);
32
+ useNostoApi(
33
+ async (api) => {
34
+ const data = await api.defaultSession()
35
+ .viewFrontPage()
36
+ .setPlacements(props.placements || api.placements.getPlacements())
37
+ .load()
38
+ renderCampaigns(data, api)
39
+ },
40
+ [recommendationComponent, pageTypeUpdated]
41
+ )
59
42
 
60
43
  return (
61
44
  <>
@@ -63,5 +46,5 @@ export default function NostoHome(): JSX.Element {
63
46
  front
64
47
  </div>
65
48
  </>
66
- );
49
+ )
67
50
  }
@@ -1,7 +1,7 @@
1
- import { Purchase } from "../../types";
2
- import { useEffect } from "react";
3
- import { useNostoContext } from "../Provider/context.client";
4
- import { snakeize } from "../../utils/snakeize";
1
+ import { Purchase } from "../types"
2
+ import { useNostoContext } from "./context"
3
+ import { snakeize } from "../utils/snakeize"
4
+ import { useNostoApi } from "../utils/hooks"
5
5
 
6
6
  /**
7
7
  * You can personalise your order-confirmation/thank-you page by using the `NostoOrder` component.
@@ -20,43 +20,27 @@ import { snakeize } from "../../utils/snakeize";
20
20
  * </div>
21
21
  * ```
22
22
  *
23
- * @group Personalisation Components
23
+ * @group Components
24
24
  */
25
25
  export default function NostoOrder(props: {
26
- order: { purchase: Purchase };
27
- }): JSX.Element {
28
- const { order } = props;
29
- const {
30
- clientScriptLoaded,
31
- currentVariation,
32
- responseMode,
33
- recommendationComponent,
34
- useRenderCampaigns,
35
- } = useNostoContext();
26
+ order: { purchase: Purchase }
27
+ placements?: string[]
28
+ }) {
29
+ const { order, placements } = props
30
+ const { recommendationComponent, useRenderCampaigns } = useNostoContext()
36
31
 
37
- const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("order");
32
+ const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("order")
38
33
 
39
- useEffect(() => {
40
- if (clientScriptLoaded && pageTypeUpdated) {
41
- window.nostojs((api) => {
42
- api
43
- .defaultSession()
44
- .setVariation(currentVariation)
45
- .setResponseMode(responseMode)
46
- .addOrder(snakeize(order))
47
- .setPlacements(api.placements.getPlacements())
48
- .load()
49
- .then((data) => {
50
- renderCampaigns(data, api);
51
- });
52
- });
53
- }
54
- }, [
55
- clientScriptLoaded,
56
- currentVariation,
57
- recommendationComponent,
58
- pageTypeUpdated,
59
- ]);
34
+ useNostoApi(
35
+ async (api) => {
36
+ const data = await api.defaultSession()
37
+ .addOrder(snakeize(order))
38
+ .setPlacements(placements || api.placements.getPlacements())
39
+ .load()
40
+ renderCampaigns(data, api)
41
+ },
42
+ [recommendationComponent, pageTypeUpdated]
43
+ )
60
44
 
61
45
  return (
62
46
  <>
@@ -67,5 +51,5 @@ export default function NostoOrder(props: {
67
51
  {order.purchase.number}
68
52
  </div>
69
53
  </>
70
- );
54
+ )
71
55
  }
@@ -0,0 +1,46 @@
1
+ import { useNostoContext } from "./context"
2
+ import { useNostoApi } from "../utils/hooks"
3
+
4
+ /**
5
+ * You can personalise your miscellaneous pages by using the NostoOther component.
6
+ * The component does not require any props.
7
+ *
8
+ * By default, your account, when created, has two other-page placements named `other-nosto-1` and `other-nosto-2`.
9
+ * You may omit these and use any identifier you need.
10
+ * The identifiers used here are simply provided to illustrate the example.
11
+ *
12
+ * @example
13
+ * ```
14
+ * <div className="other-page">
15
+ * <NostoPlacement id="other-nosto-1" />
16
+ * <NostoPlacement id="other-nosto-2" />
17
+ * <NostoOther />
18
+ * </div>;
19
+ * ```
20
+ *
21
+ * @group Components
22
+ */
23
+ export default function NostoOther(props: { placements?: string[] }) {
24
+ const { recommendationComponent, useRenderCampaigns } = useNostoContext()
25
+
26
+ const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("other")
27
+
28
+ useNostoApi(
29
+ async (api) => {
30
+ const data = await api.defaultSession()
31
+ .viewOther()
32
+ .setPlacements(props.placements || api.placements.getPlacements())
33
+ .load()
34
+ renderCampaigns(data, api)
35
+ },
36
+ [recommendationComponent, pageTypeUpdated]
37
+ )
38
+
39
+ return (
40
+ <>
41
+ <div className="nosto_page_type" style={{ display: "none" }}>
42
+ other
43
+ </div>
44
+ </>
45
+ )
46
+ }
@@ -1,4 +1,4 @@
1
- import React from "react";
1
+ import React from "react"
2
2
 
3
3
  /**
4
4
  * Nosto React has a special component called NostoPlacement.
@@ -11,12 +11,9 @@ import React from "react";
11
11
  * <NostoPlacement id="frontpage-nosto-1" />
12
12
  * ```
13
13
  *
14
- * @group Personalisation Components
14
+ * @group Components
15
15
  */
16
- export default function NostoPlacement(props: {
17
- id: string;
18
- pageType?: string;
19
- }): JSX.Element {
20
- const { id, pageType } = props;
21
- return <div className="nosto_element" id={id} key={id + (pageType || "")} />;
16
+ export default function NostoPlacement(props: { id: string; pageType?: string }) {
17
+ const { id, pageType } = props
18
+ return <div className="nosto_element" id={id} key={id + (pageType || "")} />
22
19
  }
@@ -1,6 +1,6 @@
1
- import { Product } from "../../types";
2
- import { useNostoContext } from "../Provider/context.client";
3
- import { useDeepCompareEffect } from "../../utils/hooks";
1
+ import { Product } from "../types"
2
+ import { useNostoContext } from "./context"
3
+ import { useNostoApi } from "../utils/hooks"
4
4
 
5
5
  /**
6
6
  * The NostoProduct component must be used to personalise the product page.
@@ -24,44 +24,29 @@ import { useDeepCompareEffect } from "../../utils/hooks";
24
24
  * </div>
25
25
  * ```
26
26
  *
27
- * @group Personalisation Components
27
+ * @group Components
28
28
  */
29
29
  export default function NostoProduct(props: {
30
- product: string;
31
- tagging?: Product;
32
- }): JSX.Element {
33
- const { product, tagging } = props;
34
- const {
35
- clientScriptLoaded,
36
- currentVariation,
37
- responseMode,
38
- recommendationComponent,
39
- useRenderCampaigns,
40
- } = useNostoContext();
30
+ product: string
31
+ tagging?: Product
32
+ placements?: string[]
33
+ }) {
34
+ const { product, tagging, placements } = props
35
+ const { recommendationComponent, useRenderCampaigns } = useNostoContext()
41
36
 
42
- const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("product");
37
+ const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("product")
43
38
 
44
- useDeepCompareEffect(() => {
45
- if (clientScriptLoaded && pageTypeUpdated) {
46
- window.nostojs((api) => {
47
- api
48
- .defaultSession()
49
- .setResponseMode(responseMode)
50
- .viewProduct(product)
51
- .setPlacements(api.placements.getPlacements())
52
- .load()
53
- .then((data) => {
54
- renderCampaigns(data, api);
55
- });
56
- });
57
- }
58
- }, [
59
- clientScriptLoaded,
60
- currentVariation,
61
- product,
62
- recommendationComponent,
63
- pageTypeUpdated,
64
- ]);
39
+ useNostoApi(
40
+ async (api) => {
41
+ const data = await api.defaultSession()
42
+ .viewProduct(product)
43
+ .setPlacements(placements || api.placements.getPlacements())
44
+ .load()
45
+ renderCampaigns(data, api)
46
+ },
47
+ [product, recommendationComponent, pageTypeUpdated],
48
+ { deep: true }
49
+ )
65
50
 
66
51
  return (
67
52
  <>
@@ -69,41 +54,23 @@ export default function NostoProduct(props: {
69
54
  product
70
55
  </div>
71
56
  <div className="nosto_product" style={{ display: "none" }}>
72
- {tagging?.variationId && (
73
- <span className="variation_id">{tagging.variationId}</span>
74
- )}
57
+ {tagging?.variationId && <span className="variation_id">{tagging.variationId}</span>}
75
58
  {product && <span className="product_id">{product}</span>}
76
59
  {tagging?.name && <span className="name">{tagging.name}</span>}
77
60
  {tagging?.url && <span className="url">{tagging.url.toString()}</span>}
78
- {tagging?.imageUrl && (
79
- <span className="image_url">{tagging.imageUrl.toString()}</span>
80
- )}
81
- {tagging?.availability && (
82
- <span className="availability">{tagging.availability}</span>
83
- )}
61
+ {tagging?.imageUrl && <span className="image_url">{tagging.imageUrl.toString()}</span>}
62
+ {tagging?.availability && <span className="availability">{tagging.availability}</span>}
84
63
  {tagging?.price && <span className="price">{tagging.price}</span>}
85
- {tagging?.listPrice && (
86
- <span className="list_price">{tagging.listPrice}</span>
87
- )}
64
+ {tagging?.listPrice && <span className="list_price">{tagging.listPrice}</span>}
88
65
  {tagging?.priceCurrencyCode && (
89
- <span className="price_currency_code">
90
- {tagging.priceCurrencyCode}
91
- </span>
66
+ <span className="price_currency_code">{tagging.priceCurrencyCode}</span>
92
67
  )}
93
68
  {tagging?.brand && <span className="brand">{tagging.brand}</span>}
94
- {tagging?.description && (
95
- <span className="description">{tagging.description}</span>
96
- )}
97
- {tagging?.googleCategory && (
98
- <span className="description">{tagging.googleCategory}</span>
99
- )}
100
- {tagging?.condition && (
101
- <span className="condition">{tagging.condition}</span>
102
- )}
69
+ {tagging?.description && <span className="description">{tagging.description}</span>}
70
+ {tagging?.googleCategory && <span className="description">{tagging.googleCategory}</span>}
71
+ {tagging?.condition && <span className="condition">{tagging.condition}</span>}
103
72
  {tagging?.gender && <span className="gender">{tagging.gender}</span>}
104
- {tagging?.ageGroup && (
105
- <span className="age_group">{tagging.ageGroup}</span>
106
- )}
73
+ {tagging?.ageGroup && <span className="age_group">{tagging.ageGroup}</span>}
107
74
  {tagging?.gtin && <span className="gtin">{tagging.gtin}</span>}
108
75
  {tagging?.category &&
109
76
  tagging?.category.map((category, index) => (
@@ -129,12 +96,8 @@ export default function NostoProduct(props: {
129
96
  {tag}
130
97
  </span>
131
98
  ))}
132
- {tagging?.ratingValue && (
133
- <span className="rating_value">{tagging.ratingValue}</span>
134
- )}
135
- {tagging?.reviewCount && (
136
- <span className="review_count">{tagging.reviewCount}</span>
137
- )}
99
+ {tagging?.ratingValue && <span className="rating_value">{tagging.ratingValue}</span>}
100
+ {tagging?.reviewCount && <span className="review_count">{tagging.reviewCount}</span>}
138
101
  {tagging?.alternateImageUrls &&
139
102
  tagging.alternateImageUrls.map((url, index) => (
140
103
  <span className="alternate_image_url" key={index}>
@@ -157,17 +120,11 @@ export default function NostoProduct(props: {
157
120
  {sku?.id && <span className="product_id">{sku.id}</span>}
158
121
  {sku?.name && <span className="name">{sku.name}</span>}
159
122
  {sku?.price && <span className="price">{sku.price}</span>}
160
- {sku?.listPrice && (
161
- <span className="list_price">{sku.listPrice}</span>
162
- )}
123
+ {sku?.listPrice && <span className="list_price">{sku.listPrice}</span>}
163
124
  {sku?.url && <span className="url">{sku.url.toString()}</span>}
164
- {sku?.imageUrl && (
165
- <span className="image_url">{sku.imageUrl.toString()}</span>
166
- )}
125
+ {sku?.imageUrl && <span className="image_url">{sku.imageUrl.toString()}</span>}
167
126
  {sku?.gtin && <span className="gtin">{sku.gtin}</span>}
168
- {sku?.availability && (
169
- <span className="availability">{sku.availability}</span>
170
- )}
127
+ {sku?.availability && <span className="availability">{sku.availability}</span>}
171
128
  {sku?.customFields &&
172
129
  Object.keys(sku.customFields).map(
173
130
  (field, index) =>
@@ -182,5 +139,5 @@ export default function NostoProduct(props: {
182
139
  ))}
183
140
  </div>
184
141
  </>
185
- );
142
+ )
186
143
  }