@nosto/nosto-react 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,32 @@
1
- import React, { useEffect } from "react";
1
+ import { useEffect } from "react";
2
2
  import { useNostoContext } from "../Provider/context.client";
3
3
 
4
- const NostoCategory: React.FC<{ category: string }> = ({ category }) => {
4
+ /**
5
+ * You can personalise your category and collection pages by using the NostoCategory component.
6
+ * The component requires that you provide it the the slash-delimited slug representation of the current category.
7
+ *
8
+ * By default, your account, when created, has two category placements named `categorypage-nosto-1` and `categorypage-nosto-2`.
9
+ * You may omit these and use any identifier you need. The identifiers used here are simply provided to illustrate the example.
10
+ *
11
+ * @example
12
+ * ```
13
+ * <div className="category-page">
14
+ * <NostoPlacement id="categorypage-nosto-1" />
15
+ * <NostoPlacement id="categorypage-nosto-2" />
16
+ * <NostoCategory category={category.name} />
17
+ * </div>
18
+ * ```
19
+ *
20
+ * **Note:** Be sure to pass in the correct category representation.
21
+ * If the category being viewed is `Mens >> Jackets`, you must provide the name as `/Mens/Jackets`.
22
+ * You must ensure that the category path provided here matches that of the categories tagged in your products.
23
+ *
24
+ * @group Personalisation Components
25
+ */
26
+ export default function NostoCategory(props: {
27
+ category: string;
28
+ }): JSX.Element {
29
+ const { category } = props;
5
30
  const {
6
31
  clientScriptLoaded,
7
32
  currentVariation,
@@ -13,9 +38,8 @@ const NostoCategory: React.FC<{ category: string }> = ({ category }) => {
13
38
  const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("home");
14
39
 
15
40
  useEffect(() => {
16
- // @ts-ignore
17
41
  if (clientScriptLoaded && pageTypeUpdated) {
18
- window.nostojs((api: any) => {
42
+ window.nostojs((api) => {
19
43
  api
20
44
  .defaultSession()
21
45
  .setVariation(currentVariation)
@@ -23,7 +47,7 @@ const NostoCategory: React.FC<{ category: string }> = ({ category }) => {
23
47
  .viewCategory(category)
24
48
  .setPlacements(api.placements.getPlacements())
25
49
  .load()
26
- .then((data: object) => {
50
+ .then((data) => {
27
51
  renderCampaigns(data, api);
28
52
  });
29
53
  });
@@ -46,6 +70,4 @@ const NostoCategory: React.FC<{ category: string }> = ({ category }) => {
46
70
  </div>
47
71
  </>
48
72
  );
49
- };
50
-
51
- export default NostoCategory;
73
+ }
@@ -1,7 +1,27 @@
1
- import React, { useEffect } from "react";
1
+ import { useEffect } from "react";
2
2
  import { useNostoContext } from "../Provider/context.client";
3
3
 
4
- const NostoCheckout: React.FC = () => {
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 Personalisation Components
22
+ */
23
+
24
+ export default function NostoCheckout(): JSX.Element {
5
25
  const {
6
26
  clientScriptLoaded,
7
27
  currentVariation,
@@ -13,9 +33,8 @@ const NostoCheckout: React.FC = () => {
13
33
  const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("checkout");
14
34
 
15
35
  useEffect(() => {
16
- // @ts-ignore
17
36
  if (clientScriptLoaded && pageTypeUpdated) {
18
- window.nostojs((api: any) => {
37
+ window.nostojs((api) => {
19
38
  api
20
39
  .defaultSession()
21
40
  .setVariation(currentVariation)
@@ -23,7 +42,7 @@ const NostoCheckout: React.FC = () => {
23
42
  .viewCart()
24
43
  .setPlacements(api.placements.getPlacements())
25
44
  .load()
26
- .then((data: object) => {
45
+ .then((data) => {
27
46
  renderCampaigns(data, api);
28
47
  });
29
48
  });
@@ -42,6 +61,4 @@ const NostoCheckout: React.FC = () => {
42
61
  </div>
43
62
  </>
44
63
  );
45
- };
46
-
47
- export default NostoCheckout;
64
+ }
@@ -1,7 +1,27 @@
1
- import React, { useEffect } from "react";
1
+ import { useEffect } from "react";
2
2
  import { useNostoContext } from "../Provider/context.client";
3
3
 
4
- const NostoFohofo: React.FC = () => {
4
+ /**
5
+ * You can personalise your cart and checkout pages by using the `Nosto404` component.
6
+ * The component does not require any props.
7
+ *
8
+ * By default, your account, when created, has three 404-page placements named `notfound-nosto-1`, `notfound-nosto-2` and `notfound-nosto-3`.
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="notfound-page">
15
+ * <NostoPlacement id="notfound-nosto-1" />
16
+ * <NostoPlacement id="notfound-nosto-2" />
17
+ * <NostoPlacement id="notfound-nosto-3" />
18
+ * <Nosto404 />
19
+ * </div>
20
+ * ```
21
+ *
22
+ * @group Personalisation Components
23
+ */
24
+ export default function Nosto404(): JSX.Element {
5
25
  const {
6
26
  clientScriptLoaded,
7
27
  currentVariation,
@@ -13,9 +33,8 @@ const NostoFohofo: React.FC = () => {
13
33
  const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("404");
14
34
 
15
35
  useEffect(() => {
16
- // @ts-ignore
17
36
  if (clientScriptLoaded && pageTypeUpdated) {
18
- window.nostojs((api: any) => {
37
+ window.nostojs((api) => {
19
38
  api
20
39
  .defaultSession()
21
40
  .setVariation(currentVariation)
@@ -23,7 +42,7 @@ const NostoFohofo: React.FC = () => {
23
42
  .viewNotFound()
24
43
  .setPlacements(api.placements.getPlacements())
25
44
  .load()
26
- .then((data: object) => {
45
+ .then((data) => {
27
46
  renderCampaigns(data, api);
28
47
  });
29
48
  });
@@ -42,6 +61,4 @@ const NostoFohofo: React.FC = () => {
42
61
  </div>
43
62
  </>
44
63
  );
45
- };
46
-
47
- export default NostoFohofo;
64
+ }
@@ -1,7 +1,30 @@
1
- import React, { useEffect } from "react";
1
+ import { useEffect } from "react";
2
2
  import { useNostoContext } from "../Provider/context.client";
3
3
 
4
- const NostoHome: React.FC = () => {
4
+ /**
5
+ * The `NostoHome` component must be used to personalise the home page. The component does not require any props.
6
+ *
7
+ * By default, your account, when created, has four front-page placements named `frontpage-nosto-1`, `frontpage-nosto-2`, `frontpage-nosto-3` and `frontpage-nosto-4`.
8
+ * You may omit these and use any identifier you need.
9
+ * The identifiers used here are simply provided to illustrate the example.
10
+ *
11
+ * The `<NostoHome \>` component needs to be added after the placements.
12
+ * Content and recommendations will be rendered through this component.
13
+ *
14
+ * @example
15
+ * ```
16
+ * <div className="front-page">
17
+ * <NostoPlacement id="frontpage-nosto-1" />
18
+ * <NostoPlacement id="frontpage-nosto-2" />
19
+ * <NostoPlacement id="frontpage-nosto-3" />
20
+ * <NostoPlacement id="frontpage-nosto-4" />
21
+ * <NostoHome />
22
+ * </div>
23
+ * ```
24
+ *
25
+ * @group Personalisation Components
26
+ */
27
+ export default function NostoHome(): JSX.Element {
5
28
  const {
6
29
  clientScriptLoaded,
7
30
  currentVariation,
@@ -13,9 +36,8 @@ const NostoHome: React.FC = () => {
13
36
  const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("home");
14
37
 
15
38
  useEffect(() => {
16
- // @ts-ignore
17
39
  if (clientScriptLoaded && pageTypeUpdated) {
18
- window.nostojs((api: any) => {
40
+ window.nostojs((api) => {
19
41
  api
20
42
  .defaultSession()
21
43
  .setVariation(currentVariation)
@@ -23,7 +45,7 @@ const NostoHome: React.FC = () => {
23
45
  .viewFrontPage()
24
46
  .setPlacements(api.placements.getPlacements())
25
47
  .load()
26
- .then((data: object) => {
48
+ .then((data) => {
27
49
  renderCampaigns(data, api);
28
50
  });
29
51
  });
@@ -42,6 +64,4 @@ const NostoHome: React.FC = () => {
42
64
  </div>
43
65
  </>
44
66
  );
45
- };
46
-
47
- export default NostoHome;
67
+ }
@@ -1,13 +1,31 @@
1
1
  import { Purchase } from "../../types";
2
- import React, { useEffect } from "react";
3
- import snakeize from "snakeize";
2
+ import { useEffect } from "react";
4
3
  import { useNostoContext } from "../Provider/context.client";
4
+ import { snakeize } from "../../utils/snakeize";
5
5
 
6
- export interface OrderProps {
7
- purchase: Purchase;
8
- }
9
-
10
- const NostoOrder: React.FC<{ order: OrderProps }> = ({ order }) => {
6
+ /**
7
+ * You can personalise your order-confirmation/thank-you page by using the `NostoOrder` component.
8
+ * The component requires that you provide it with the details of the order.
9
+ *
10
+ * By default, your account, when created, has one other-page placement named `thankyou-nosto-1`.
11
+ * You may omit this and use any identifier you need. The identifier used here is simply provided to illustrate the example.
12
+ *
13
+ * The order prop requires a value that adheres to the type `Purchase`.
14
+ *
15
+ * @example
16
+ * ```
17
+ * <div className="thankyou-page">
18
+ * <NostoPlacement id="thankyou-nosto-1" />
19
+ * <NostoOrder order={{ purchase: toOrder(order) }} />
20
+ * </div>
21
+ * ```
22
+ *
23
+ * @group Personalisation Components
24
+ */
25
+ export default function NostoOrder(props: {
26
+ order: { purchase: Purchase };
27
+ }): JSX.Element {
28
+ const { order } = props;
11
29
  const {
12
30
  clientScriptLoaded,
13
31
  currentVariation,
@@ -19,9 +37,8 @@ const NostoOrder: React.FC<{ order: OrderProps }> = ({ order }) => {
19
37
  const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("order");
20
38
 
21
39
  useEffect(() => {
22
- // @ts-ignore
23
40
  if (clientScriptLoaded && pageTypeUpdated) {
24
- window.nostojs((api: any) => {
41
+ window.nostojs((api) => {
25
42
  api
26
43
  .defaultSession()
27
44
  .setVariation(currentVariation)
@@ -29,7 +46,7 @@ const NostoOrder: React.FC<{ order: OrderProps }> = ({ order }) => {
29
46
  .addOrder(snakeize(order))
30
47
  .setPlacements(api.placements.getPlacements())
31
48
  .load()
32
- .then((data: object) => {
49
+ .then((data) => {
33
50
  renderCampaigns(data, api);
34
51
  });
35
52
  });
@@ -51,6 +68,4 @@ const NostoOrder: React.FC<{ order: OrderProps }> = ({ order }) => {
51
68
  </div>
52
69
  </>
53
70
  );
54
- };
55
-
56
- export default NostoOrder;
71
+ }
@@ -1,7 +1,26 @@
1
1
  import React, { useEffect } from "react";
2
2
  import { useNostoContext } from "../Provider/context.client";
3
3
 
4
- const NostoOther: React.FC = () => {
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 Personalisation Components
22
+ */
23
+ export default function NostoOther(): JSX.Element {
5
24
  const {
6
25
  clientScriptLoaded,
7
26
  currentVariation,
@@ -13,9 +32,8 @@ const NostoOther: React.FC = () => {
13
32
  const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("other");
14
33
 
15
34
  useEffect(() => {
16
- // @ts-ignore
17
35
  if (clientScriptLoaded && pageTypeUpdated) {
18
- window.nostojs((api: any) => {
36
+ window.nostojs((api) => {
19
37
  api
20
38
  .defaultSession()
21
39
  .setVariation(currentVariation)
@@ -23,7 +41,7 @@ const NostoOther: React.FC = () => {
23
41
  .viewOther()
24
42
  .setPlacements(api.placements.getPlacements())
25
43
  .load()
26
- .then((data: object) => {
44
+ .then((data) => {
27
45
  renderCampaigns(data, api);
28
46
  });
29
47
  });
@@ -42,6 +60,4 @@ const NostoOther: React.FC = () => {
42
60
  </div>
43
61
  </>
44
62
  );
45
- };
46
-
47
- export default NostoOther;
63
+ }
@@ -1,12 +1,22 @@
1
1
  import React from "react";
2
2
 
3
- export interface PlacementProps {
3
+ /**
4
+ * Nosto React has a special component called NostoPlacement.
5
+ * The component is a simply a hidden `<div>` placeholder into which Nosto injects recommendations or personalises the content between the tags.
6
+ *
7
+ * We recommend adding as many placements across your views as needed as these are hidden and only populated when a corresponding campaign (targeting that placement) is configured.
8
+ *
9
+ * @example
10
+ * ```
11
+ * <NostoPlacement id="frontpage-nosto-1" />
12
+ * ```
13
+ *
14
+ * @group Personalisation Components
15
+ */
16
+ export default function NostoPlacement(props: {
4
17
  id: string;
5
- pageType: string;
18
+ pageType?: string;
19
+ }): JSX.Element {
20
+ const { id, pageType } = props;
21
+ return <div className="nosto_element" id={id} key={id + (pageType || "")} />;
6
22
  }
7
-
8
- const NostoPlacement: React.FC<PlacementProps> = ({ id, pageType }) => {
9
- return <div className="nosto_element" id={id} key={id + pageType} />;
10
- };
11
-
12
- export default NostoPlacement;
@@ -1,11 +1,36 @@
1
1
  import { Product } from "../../types";
2
- import React, { useEffect } from "react";
3
2
  import { useNostoContext } from "../Provider/context.client";
3
+ import { useDeepCompareEffect } from "../../utils/hooks";
4
4
 
5
- const NostoProduct: React.FC<{ product: string; tagging: Product }> = ({
6
- product,
7
- tagging,
8
- }) => {
5
+ /**
6
+ * The NostoProduct component must be used to personalise the product page.
7
+ * The component requires that you provide it the identifier of the current product being viewed.
8
+ *
9
+ * By default, your account, when created, has three product-page placements named `productpage-nosto-1`, `productpage-nosto-2` and `productpage-nosto-3`.
10
+ * You may omit these and use any identifier you need.
11
+ * The identifiers used here are simply provided to illustrate the example.
12
+ *
13
+ * The `<NostoProduct \>` component needs to be added after the placements.
14
+ * Content and recommendations will be rendered through this component.
15
+ * Pass in the product ID via the product prop to pass this information back to Nosto.
16
+ *
17
+ * @example
18
+ * ```
19
+ * <div className="product-page">
20
+ * <NostoPlacement id="productpage-nosto-1" />
21
+ * <NostoPlacement id="productpage-nosto-2" />
22
+ * <NostoPlacement id="productpage-nosto-3" />
23
+ * <NostoProduct product={product.id} />
24
+ * </div>
25
+ * ```
26
+ *
27
+ * @group Personalisation Components
28
+ */
29
+ export default function NostoProduct(props: {
30
+ product: string;
31
+ tagging?: Product;
32
+ }): JSX.Element {
33
+ const { product, tagging } = props;
9
34
  const {
10
35
  clientScriptLoaded,
11
36
  currentVariation,
@@ -16,31 +41,27 @@ const NostoProduct: React.FC<{ product: string; tagging: Product }> = ({
16
41
 
17
42
  const { renderCampaigns, pageTypeUpdated } = useRenderCampaigns("product");
18
43
 
19
- useEffect(() => {
20
- // @ts-ignore
44
+ useDeepCompareEffect(() => {
21
45
  if (clientScriptLoaded && pageTypeUpdated) {
22
- window.nostojs(
23
- (api: any) => {
24
- api
25
- .defaultSession()
26
- .setResponseMode(responseMode)
27
- .viewProduct(product)
28
- .setPlacements(api.placements.getPlacements())
29
- .load()
30
- .then((data: object) => {
31
- renderCampaigns(data, api);
32
- });
33
- },
34
- [
35
- clientScriptLoaded,
36
- currentVariation,
37
- product,
38
- recommendationComponent,
39
- pageTypeUpdated,
40
- ]
41
- );
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
+ });
42
57
  }
43
- });
58
+ }, [
59
+ clientScriptLoaded,
60
+ currentVariation,
61
+ product,
62
+ recommendationComponent,
63
+ pageTypeUpdated,
64
+ ]);
44
65
 
45
66
  return (
46
67
  <>
@@ -162,6 +183,4 @@ const NostoProduct: React.FC<{ product: string; tagging: Product }> = ({
162
183
  </div>
163
184
  </>
164
185
  );
165
- };
166
-
167
- export default NostoProduct;
186
+ }
@@ -1,26 +1,40 @@
1
- import { createContext, useContext, ReactComponentElement } from "react";
1
+ import { createContext, useContext } from "react";
2
+ import { Recommendation } from "../../types";
2
3
 
3
- export interface NostoInterface {
4
+ /**
5
+ * @group Types
6
+ */
7
+ export interface NostoContextType {
4
8
  account: string;
5
9
  clientScriptLoaded: boolean;
6
10
  currentVariation: string;
7
11
  renderFunction?: Function;
8
12
  responseMode: string;
9
- recommendationComponent?: any;
13
+ recommendationComponent?: React.ReactElement<{
14
+ nostoRecommendation: Recommendation;
15
+ }>;
10
16
  useRenderCampaigns: Function;
11
17
  pageType: string;
12
18
  }
13
19
 
14
- /* tslint:disable:no-empty */
15
- export const NostoContext = createContext<NostoInterface>({
16
- // @ts-ignore
17
- account: undefined,
20
+ /**
21
+ * @group Essential Functions
22
+ */
23
+ export const NostoContext = createContext<NostoContextType>({
24
+ account: "",
18
25
  currentVariation: "",
19
- renderFunction: undefined,
26
+ pageType: "",
27
+ responseMode: "HTML",
28
+ clientScriptLoaded: false,
29
+ useRenderCampaigns: () => undefined,
20
30
  });
21
31
 
22
- /* tslint:enable:no-empty */
23
- export function useNostoContext() {
32
+ /**
33
+ * A hook that allows you to access the NostoContext and retrieve Nosto-related data from it in React components.
34
+ *
35
+ * @group Essential Functions
36
+ */
37
+ export function useNostoContext(): NostoContextType {
24
38
  const context = useContext(NostoContext);
25
39
 
26
40
  if (!context) {