@nosto/nosto-react 2.2.1 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +11 -15
- package/dist/index.es.js +178 -183
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
- package/src/components/Nosto404.tsx +2 -25
- package/src/components/NostoCategory.tsx +2 -30
- package/src/components/NostoCheckout.tsx +2 -25
- package/src/components/NostoHome.tsx +2 -26
- package/src/components/NostoOrder.tsx +2 -33
- package/src/components/NostoOther.tsx +2 -25
- package/src/components/NostoPlacement.tsx +1 -1
- package/src/components/NostoProduct.tsx +2 -32
- package/src/components/NostoProvider.tsx +3 -8
- package/src/components/NostoSearch.tsx +2 -30
- package/src/components/NostoSession.tsx +2 -42
- package/src/components/helpers.ts +2 -2
- package/src/context.ts +0 -1
- package/src/hooks/scriptLoader.ts +4 -4
- package/src/hooks/useDeepCompareEffect.ts +1 -4
- package/src/hooks/useLoadClientScript.ts +9 -4
- package/src/hooks/useNosto404.tsx +25 -0
- package/src/hooks/useNostoApi.ts +1 -5
- package/src/hooks/useNostoCategory.tsx +31 -0
- package/src/hooks/useNostoCheckout.tsx +25 -0
- package/src/hooks/useNostoContext.ts +2 -3
- package/src/hooks/useNostoHome.tsx +25 -0
- package/src/hooks/useNostoOrder.tsx +35 -0
- package/src/hooks/useNostoOther.tsx +25 -0
- package/src/hooks/useNostoProduct.tsx +37 -0
- package/src/hooks/useNostoSearch.tsx +31 -0
- package/src/hooks/useNostoSession.tsx +33 -0
- package/src/hooks/useRenderCampaigns.tsx +6 -6
- package/src/index.ts +21 -2
- package/src/types.ts +775 -733
- package/src/utils/types.ts +5 -3
- package/src/components/index.ts +0 -11
- package/src/hooks/index.ts +0 -5
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @group Components
|
|
5
|
-
*/
|
|
6
|
-
export type Nosto404Props = { placements?: string[] }
|
|
1
|
+
import { Nosto404Props, useNosto404 } from "../hooks/useNosto404"
|
|
7
2
|
|
|
8
3
|
/**
|
|
9
4
|
* You can personalise your cart and checkout pages by using the `Nosto404` component.
|
|
@@ -25,25 +20,7 @@ export type Nosto404Props = { placements?: string[] }
|
|
|
25
20
|
*
|
|
26
21
|
* @group Components
|
|
27
22
|
*/
|
|
28
|
-
export
|
|
23
|
+
export function Nosto404(props: Nosto404Props) {
|
|
29
24
|
useNosto404(props)
|
|
30
25
|
return null
|
|
31
26
|
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* You can personalise your cart and checkout pages by using the `useNosto404` hook.
|
|
35
|
-
*
|
|
36
|
-
* @group Hooks
|
|
37
|
-
*/
|
|
38
|
-
export function useNosto404(props?: Nosto404Props) {
|
|
39
|
-
const { renderCampaigns } = useRenderCampaigns()
|
|
40
|
-
|
|
41
|
-
useNostoApi(
|
|
42
|
-
async (api) => {
|
|
43
|
-
const data = await api.defaultSession()
|
|
44
|
-
.viewNotFound()
|
|
45
|
-
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
46
|
-
.load()
|
|
47
|
-
renderCampaigns(data)
|
|
48
|
-
})
|
|
49
|
-
}
|
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @group Components
|
|
5
|
-
*/
|
|
6
|
-
export type NostoCategoryProps = {
|
|
7
|
-
category: string
|
|
8
|
-
placements?: string[]
|
|
9
|
-
}
|
|
1
|
+
import { NostoCategoryProps, useNostoCategory } from "../hooks/useNostoCategory"
|
|
10
2
|
|
|
11
3
|
/**
|
|
12
4
|
* You can personalise your category and collection pages by using the NostoCategory component.
|
|
@@ -30,27 +22,7 @@ export type NostoCategoryProps = {
|
|
|
30
22
|
*
|
|
31
23
|
* @group Components
|
|
32
24
|
*/
|
|
33
|
-
export
|
|
25
|
+
export function NostoCategory(props: NostoCategoryProps) {
|
|
34
26
|
useNostoCategory(props)
|
|
35
27
|
return null
|
|
36
28
|
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* You can personalise your category and collection pages by using the useNostoCategory hook.
|
|
40
|
-
*
|
|
41
|
-
* @group Hooks
|
|
42
|
-
*/
|
|
43
|
-
export function useNostoCategory({ category, placements }: NostoCategoryProps) {
|
|
44
|
-
const { renderCampaigns } = useRenderCampaigns()
|
|
45
|
-
|
|
46
|
-
useNostoApi(
|
|
47
|
-
async (api) => {
|
|
48
|
-
const data = await api.defaultSession()
|
|
49
|
-
.viewCategory(category)
|
|
50
|
-
.setPlacements(placements || api.placements.getPlacements())
|
|
51
|
-
.load()
|
|
52
|
-
renderCampaigns(data)
|
|
53
|
-
},
|
|
54
|
-
[category]
|
|
55
|
-
)
|
|
56
|
-
}
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @group Components
|
|
5
|
-
*/
|
|
6
|
-
export type NostoCheckoutProps = { placements?: string[] }
|
|
1
|
+
import { NostoCheckoutProps, useNostoCheckout } from "../hooks/useNostoCheckout"
|
|
7
2
|
|
|
8
3
|
/**
|
|
9
4
|
* You can personalise your cart and checkout pages by using the NostoCheckout component.
|
|
@@ -24,25 +19,7 @@ export type NostoCheckoutProps = { placements?: string[] }
|
|
|
24
19
|
*
|
|
25
20
|
* @group Components
|
|
26
21
|
*/
|
|
27
|
-
export
|
|
22
|
+
export function NostoCheckout(props: NostoCheckoutProps) {
|
|
28
23
|
useNostoCheckout(props)
|
|
29
24
|
return null
|
|
30
25
|
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* You can personalise your cart and checkout pages by using the useNostoCheckout hook.
|
|
34
|
-
*
|
|
35
|
-
* @group Hooks
|
|
36
|
-
*/
|
|
37
|
-
export function useNostoCheckout(props?: NostoCheckoutProps) {
|
|
38
|
-
const { renderCampaigns } = useRenderCampaigns()
|
|
39
|
-
|
|
40
|
-
useNostoApi(
|
|
41
|
-
async (api) => {
|
|
42
|
-
const data = await api.defaultSession()
|
|
43
|
-
.viewCart()
|
|
44
|
-
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
45
|
-
.load()
|
|
46
|
-
renderCampaigns(data)
|
|
47
|
-
})
|
|
48
|
-
}
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @group Components
|
|
5
|
-
*/
|
|
6
|
-
export type NostoHomeProps = { placements?: string[] }
|
|
1
|
+
import { NostoHomeProps, useNostoHome } from "../hooks/useNostoHome"
|
|
7
2
|
|
|
8
3
|
/**
|
|
9
4
|
* The `NostoHome` component must be used to personalise the home page. The component does not require any props.
|
|
@@ -28,26 +23,7 @@ export type NostoHomeProps = { placements?: string[] }
|
|
|
28
23
|
*
|
|
29
24
|
* @group Components
|
|
30
25
|
*/
|
|
31
|
-
export
|
|
26
|
+
export function NostoHome(props: NostoHomeProps) {
|
|
32
27
|
useNostoHome(props)
|
|
33
28
|
return null
|
|
34
29
|
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* You can personalise your home page by using the useNostoHome hook.
|
|
38
|
-
*
|
|
39
|
-
* @group Hooks
|
|
40
|
-
*/
|
|
41
|
-
export function useNostoHome(props?: NostoHomeProps) {
|
|
42
|
-
const { renderCampaigns } = useRenderCampaigns()
|
|
43
|
-
|
|
44
|
-
useNostoApi(
|
|
45
|
-
async (api) => {
|
|
46
|
-
const data = await api.defaultSession()
|
|
47
|
-
.viewFrontPage()
|
|
48
|
-
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
49
|
-
.load()
|
|
50
|
-
renderCampaigns(data)
|
|
51
|
-
}
|
|
52
|
-
)
|
|
53
|
-
}
|
|
@@ -1,14 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useRenderCampaigns, useNostoApi } from "../hooks"
|
|
3
|
-
import { snakeize } from "../utils/snakeize"
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @group Components
|
|
7
|
-
*/
|
|
8
|
-
export type NostoOrderProps = {
|
|
9
|
-
order: Order
|
|
10
|
-
placements?: string[]
|
|
11
|
-
}
|
|
1
|
+
import { NostoOrderProps, useNostoOrder } from "../hooks/useNostoOrder"
|
|
12
2
|
|
|
13
3
|
/**
|
|
14
4
|
* You can personalise your order-confirmation/thank-you page by using the `NostoOrder` component.
|
|
@@ -29,28 +19,7 @@ export type NostoOrderProps = {
|
|
|
29
19
|
*
|
|
30
20
|
* @group Components
|
|
31
21
|
*/
|
|
32
|
-
export
|
|
22
|
+
export function NostoOrder(props: NostoOrderProps) {
|
|
33
23
|
useNostoOrder(props)
|
|
34
24
|
return null
|
|
35
25
|
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* You can personalise your order-confirmation/thank-you page by using the `useNostoOrder` hook.
|
|
39
|
-
*
|
|
40
|
-
* @group Hooks
|
|
41
|
-
*/
|
|
42
|
-
export function useNostoOrder({ order, placements }: NostoOrderProps) {
|
|
43
|
-
const { renderCampaigns } = useRenderCampaigns()
|
|
44
|
-
|
|
45
|
-
useNostoApi(
|
|
46
|
-
async (api) => {
|
|
47
|
-
const data = await api.defaultSession()
|
|
48
|
-
.addOrder(snakeize(order))
|
|
49
|
-
.setPlacements(placements || api.placements.getPlacements())
|
|
50
|
-
.load()
|
|
51
|
-
renderCampaigns(data)
|
|
52
|
-
},
|
|
53
|
-
[order],
|
|
54
|
-
{ deep: true }
|
|
55
|
-
)
|
|
56
|
-
}
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @group Components
|
|
5
|
-
*/
|
|
6
|
-
export type NostoOtherProps = { placements?: string[] }
|
|
1
|
+
import { NostoOtherProps, useNostoOther } from "../hooks/useNostoOther"
|
|
7
2
|
|
|
8
3
|
/**
|
|
9
4
|
* You can personalise your miscellaneous pages by using the NostoOther component.
|
|
@@ -24,25 +19,7 @@ export type NostoOtherProps = { placements?: string[] }
|
|
|
24
19
|
*
|
|
25
20
|
* @group Components
|
|
26
21
|
*/
|
|
27
|
-
export
|
|
22
|
+
export function NostoOther(props: NostoOtherProps) {
|
|
28
23
|
useNostoOther(props)
|
|
29
24
|
return null
|
|
30
25
|
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* You can personalise your miscellaneous pages by using the useNostoOther hook.
|
|
34
|
-
*
|
|
35
|
-
* @group Hooks
|
|
36
|
-
*/
|
|
37
|
-
export function useNostoOther(props?: NostoOtherProps) {
|
|
38
|
-
const { renderCampaigns } = useRenderCampaigns()
|
|
39
|
-
|
|
40
|
-
useNostoApi(
|
|
41
|
-
async (api) => {
|
|
42
|
-
const data = await api.defaultSession()
|
|
43
|
-
.viewOther()
|
|
44
|
-
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
45
|
-
.load()
|
|
46
|
-
renderCampaigns(data)
|
|
47
|
-
})
|
|
48
|
-
}
|
|
@@ -16,6 +16,6 @@ export type NostoPlacementProps = { id: string; pageType?: string }
|
|
|
16
16
|
*
|
|
17
17
|
* @group Components
|
|
18
18
|
*/
|
|
19
|
-
export
|
|
19
|
+
export function NostoPlacement({ id, pageType }: NostoPlacementProps) {
|
|
20
20
|
return <div className="nosto_element" id={id} key={id + (pageType || "")} />
|
|
21
21
|
}
|
|
@@ -1,14 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Product } from "../types"
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @group Components
|
|
6
|
-
*/
|
|
7
|
-
export type NostoProductProps = {
|
|
8
|
-
product: string
|
|
9
|
-
tagging?: Product
|
|
10
|
-
placements?: string[]
|
|
11
|
-
}
|
|
1
|
+
import { NostoProductProps, useNostoProduct } from "../hooks/useNostoProduct"
|
|
12
2
|
|
|
13
3
|
/**
|
|
14
4
|
* The NostoProduct component must be used to personalise the product page.
|
|
@@ -34,27 +24,7 @@ export type NostoProductProps = {
|
|
|
34
24
|
*
|
|
35
25
|
* @group Components
|
|
36
26
|
*/
|
|
37
|
-
export
|
|
27
|
+
export function NostoProduct(props: NostoProductProps) {
|
|
38
28
|
useNostoProduct(props)
|
|
39
29
|
return null
|
|
40
30
|
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* You can personalise your product pages by using the useNostoProduct hook.
|
|
44
|
-
*
|
|
45
|
-
* @group Hooks
|
|
46
|
-
*/
|
|
47
|
-
export function useNostoProduct({ product, tagging, placements }: NostoProductProps) {
|
|
48
|
-
const { renderCampaigns } = useRenderCampaigns()
|
|
49
|
-
|
|
50
|
-
useNostoApi(
|
|
51
|
-
async (api) => {
|
|
52
|
-
const data = await api.defaultSession()
|
|
53
|
-
.viewProduct(tagging ?? product)
|
|
54
|
-
.setPlacements(placements || api.placements.getPlacements())
|
|
55
|
-
.load()
|
|
56
|
-
renderCampaigns(data)
|
|
57
|
-
},
|
|
58
|
-
[product, tagging?.selected_sku_id]
|
|
59
|
-
)
|
|
60
|
-
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { isValidElement } from "react"
|
|
2
2
|
import { NostoContext, RecommendationComponent } from "../context"
|
|
3
|
-
import { useLoadClientScript } from "../hooks"
|
|
4
3
|
import type { ReactNode } from "react"
|
|
5
4
|
import { ScriptLoadOptions } from "../hooks/scriptLoader"
|
|
5
|
+
import { useLoadClientScript } from "../hooks/useLoadClientScript"
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @group Components
|
|
@@ -70,13 +70,8 @@ export interface NostoProviderProps {
|
|
|
70
70
|
*
|
|
71
71
|
* @group Components
|
|
72
72
|
*/
|
|
73
|
-
export
|
|
74
|
-
const {
|
|
75
|
-
account,
|
|
76
|
-
multiCurrency = false,
|
|
77
|
-
children,
|
|
78
|
-
recommendationComponent,
|
|
79
|
-
} = props
|
|
73
|
+
export function NostoProvider(props: NostoProviderProps) {
|
|
74
|
+
const { account, multiCurrency = false, children, recommendationComponent } = props
|
|
80
75
|
|
|
81
76
|
// Pass currentVariation as empty string if multiCurrency is disabled
|
|
82
77
|
const currentVariation = multiCurrency ? props.currentVariation : ""
|
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @group Components
|
|
5
|
-
*/
|
|
6
|
-
export type NostoSearchProps = {
|
|
7
|
-
query: string
|
|
8
|
-
placements?: string[]
|
|
9
|
-
}
|
|
1
|
+
import { NostoSearchProps, useNostoSearch } from "../hooks/useNostoSearch"
|
|
10
2
|
|
|
11
3
|
/**
|
|
12
4
|
* You can personalise your search pages by using the NostoSearch component.
|
|
@@ -31,27 +23,7 @@ export type NostoSearchProps = {
|
|
|
31
23
|
*
|
|
32
24
|
* @group Components
|
|
33
25
|
*/
|
|
34
|
-
export
|
|
26
|
+
export function NostoSearch(props: NostoSearchProps) {
|
|
35
27
|
useNostoSearch(props)
|
|
36
28
|
return null
|
|
37
29
|
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* You can personalise your search pages by using the useNostoSearch hook.
|
|
41
|
-
*
|
|
42
|
-
* @group Hooks
|
|
43
|
-
*/
|
|
44
|
-
export function useNostoSearch({ query, placements }: NostoSearchProps) {
|
|
45
|
-
const { renderCampaigns } = useRenderCampaigns()
|
|
46
|
-
|
|
47
|
-
useNostoApi(
|
|
48
|
-
async (api) => {
|
|
49
|
-
const data = await api.defaultSession()
|
|
50
|
-
.viewSearch(query)
|
|
51
|
-
.setPlacements(placements || api.placements.getPlacements())
|
|
52
|
-
.load()
|
|
53
|
-
renderCampaigns(data)
|
|
54
|
-
},
|
|
55
|
-
[query]
|
|
56
|
-
)
|
|
57
|
-
}
|
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Cart as CartSnakeCase, Customer as CustomerSnakeCase } from "../types"
|
|
3
|
-
import { snakeize } from "../utils/snakeize"
|
|
4
|
-
import { ToCamelCase } from "../utils/types"
|
|
5
|
-
|
|
6
|
-
type Cart = CartSnakeCase | ToCamelCase<CartSnakeCase>
|
|
7
|
-
type Customer = CustomerSnakeCase | ToCamelCase<CustomerSnakeCase>
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @group Components
|
|
11
|
-
*/
|
|
12
|
-
export type NostoSessionProps = {
|
|
13
|
-
cart?: Cart
|
|
14
|
-
customer?: Customer
|
|
15
|
-
}
|
|
1
|
+
import { NostoSessionProps, useNostoSession } from "../hooks/useNostoSession"
|
|
16
2
|
|
|
17
3
|
/**
|
|
18
4
|
* Nosto React requires that you pass it the details of current cart contents and the details of the currently logged-in customer, if any, on every route change.
|
|
@@ -24,33 +10,7 @@ export type NostoSessionProps = {
|
|
|
24
10
|
*
|
|
25
11
|
* @group Components
|
|
26
12
|
*/
|
|
27
|
-
export
|
|
13
|
+
export function NostoSession(props?: NostoSessionProps) {
|
|
28
14
|
useNostoSession(props)
|
|
29
15
|
return null
|
|
30
16
|
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Nosto React requires that you pass it the details of current cart contents and the details of the currently logged-in customer, if any, on every route change.
|
|
34
|
-
*
|
|
35
|
-
* @group Hooks
|
|
36
|
-
*/
|
|
37
|
-
export function useNostoSession({ cart, customer }: NostoSessionProps = {}) {
|
|
38
|
-
const { clientScriptLoaded } = useNostoContext()
|
|
39
|
-
|
|
40
|
-
useDeepCompareEffect(() => {
|
|
41
|
-
const currentCart = cart ? snakeize(cart) : undefined
|
|
42
|
-
const currentCustomer = customer ? snakeize(customer) : undefined
|
|
43
|
-
|
|
44
|
-
if (clientScriptLoaded) {
|
|
45
|
-
window.nostojs(api => {
|
|
46
|
-
api
|
|
47
|
-
.defaultSession()
|
|
48
|
-
.setCart(currentCart)
|
|
49
|
-
.setCustomer(currentCustomer)
|
|
50
|
-
.viewOther()
|
|
51
|
-
.load({ skipPageViews: true })
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
}, [clientScriptLoaded, cart, customer])
|
|
55
|
-
|
|
56
|
-
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export function isNostoLoaded() {
|
|
2
|
-
return typeof window.nosto !== "undefined"
|
|
3
|
-
}
|
|
2
|
+
return typeof window.nosto !== "undefined"
|
|
3
|
+
}
|
package/src/context.ts
CHANGED
|
@@ -8,9 +8,9 @@ export default function scriptLoader(scriptSrc: string, options?: ScriptLoadOpti
|
|
|
8
8
|
script.onerror = () => reject()
|
|
9
9
|
Object.entries(options?.attributes ?? {}).forEach(([k, v]) => script.setAttribute(k, v))
|
|
10
10
|
if (options?.position === "head") {
|
|
11
|
-
document.head.appendChild(script)
|
|
11
|
+
document.head.appendChild(script)
|
|
12
12
|
} else {
|
|
13
|
-
document.body.appendChild(script)
|
|
13
|
+
document.body.appendChild(script)
|
|
14
14
|
}
|
|
15
15
|
})
|
|
16
16
|
}
|
|
@@ -26,5 +26,5 @@ export type ScriptLoadOptions = {
|
|
|
26
26
|
/**
|
|
27
27
|
* Indicates the attributes of the script element
|
|
28
28
|
*/
|
|
29
|
-
attributes?: Record<string, string>
|
|
30
|
-
}
|
|
29
|
+
attributes?: Record<string, string>
|
|
30
|
+
}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { useEffect, useRef, useMemo, type EffectCallback, type DependencyList } from "react"
|
|
2
2
|
import { deepCompare } from "../utils/compare"
|
|
3
3
|
|
|
4
|
-
export function useDeepCompareEffect(
|
|
5
|
-
callback: EffectCallback,
|
|
6
|
-
dependencies: DependencyList
|
|
7
|
-
) {
|
|
4
|
+
export function useDeepCompareEffect(callback: EffectCallback, dependencies: DependencyList) {
|
|
8
5
|
return useEffect(callback, useDeepCompareMemoize(dependencies))
|
|
9
6
|
}
|
|
10
7
|
|
|
@@ -7,7 +7,13 @@ import scriptLoaderFn from "./scriptLoader"
|
|
|
7
7
|
type NostoScriptProps = Pick<NostoProviderProps, "account" | "host" | "shopifyMarkets" | "loadScript" | "scriptLoader">
|
|
8
8
|
|
|
9
9
|
export function useLoadClientScript(props: NostoScriptProps) {
|
|
10
|
-
const {
|
|
10
|
+
const {
|
|
11
|
+
host = "connect.nosto.com",
|
|
12
|
+
scriptLoader = scriptLoaderFn,
|
|
13
|
+
account,
|
|
14
|
+
shopifyMarkets,
|
|
15
|
+
loadScript = true
|
|
16
|
+
} = props
|
|
11
17
|
const [clientScriptLoaded, setClientScriptLoaded] = useState(false)
|
|
12
18
|
|
|
13
19
|
useEffect(() => {
|
|
@@ -49,8 +55,7 @@ export function useLoadClientScript(props: NostoScriptProps) {
|
|
|
49
55
|
existingScript?.parentNode?.removeChild(existingScript)
|
|
50
56
|
nostoSandbox?.parentNode?.removeChild(nostoSandbox)
|
|
51
57
|
|
|
52
|
-
const urlPartial =
|
|
53
|
-
`/script/shopify/market/nosto.js?merchant=${account}&market=${marketId}&locale=${language.toLowerCase()}`
|
|
58
|
+
const urlPartial = `/script/shopify/market/nosto.js?merchant=${account}&market=${marketId}&locale=${language.toLowerCase()}`
|
|
54
59
|
injectScriptElement(urlPartial, { "nosto-language": language, "nosto-market-id": marketId })
|
|
55
60
|
}
|
|
56
61
|
}
|
|
@@ -81,4 +86,4 @@ export function useLoadClientScript(props: NostoScriptProps) {
|
|
|
81
86
|
}, [shopifyMarkets?.marketId, shopifyMarkets?.language])
|
|
82
87
|
|
|
83
88
|
return { clientScriptLoaded }
|
|
84
|
-
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useNostoApi } from "./useNostoApi"
|
|
2
|
+
import { useRenderCampaigns } from "./useRenderCampaigns"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @group Hooks
|
|
6
|
+
*/
|
|
7
|
+
export type Nosto404Props = { placements?: string[] }
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* You can personalise your cart and checkout pages by using the `useNosto404` hook.
|
|
11
|
+
*
|
|
12
|
+
* @group Hooks
|
|
13
|
+
*/
|
|
14
|
+
export function useNosto404(props?: Nosto404Props) {
|
|
15
|
+
const { renderCampaigns } = useRenderCampaigns()
|
|
16
|
+
|
|
17
|
+
useNostoApi(async api => {
|
|
18
|
+
const data = await api
|
|
19
|
+
.defaultSession()
|
|
20
|
+
.viewNotFound()
|
|
21
|
+
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
22
|
+
.load()
|
|
23
|
+
renderCampaigns(data)
|
|
24
|
+
})
|
|
25
|
+
}
|
package/src/hooks/useNostoApi.ts
CHANGED
|
@@ -3,11 +3,7 @@ import { useNostoContext } from "./useNostoContext"
|
|
|
3
3
|
import { NostoClient } from "../types"
|
|
4
4
|
import { useDeepCompareEffect } from "./useDeepCompareEffect"
|
|
5
5
|
|
|
6
|
-
export function useNostoApi(
|
|
7
|
-
cb: (api: NostoClient) => void,
|
|
8
|
-
deps?: DependencyList,
|
|
9
|
-
flags?: { deep?: boolean }
|
|
10
|
-
): void {
|
|
6
|
+
export function useNostoApi(cb: (api: NostoClient) => void, deps?: DependencyList, flags?: { deep?: boolean }): void {
|
|
11
7
|
const { clientScriptLoaded } = useNostoContext()
|
|
12
8
|
const useEffectFn = flags?.deep ? useDeepCompareEffect : useEffect
|
|
13
9
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useNostoApi } from "./useNostoApi"
|
|
2
|
+
import { useRenderCampaigns } from "./useRenderCampaigns"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @group Hooks
|
|
6
|
+
*/
|
|
7
|
+
export type NostoCategoryProps = {
|
|
8
|
+
category: string
|
|
9
|
+
placements?: string[]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* You can personalise your category and collection pages by using the useNostoCategory hook.
|
|
14
|
+
*
|
|
15
|
+
* @group Hooks
|
|
16
|
+
*/
|
|
17
|
+
export function useNostoCategory({ category, placements }: NostoCategoryProps) {
|
|
18
|
+
const { renderCampaigns } = useRenderCampaigns()
|
|
19
|
+
|
|
20
|
+
useNostoApi(
|
|
21
|
+
async api => {
|
|
22
|
+
const data = await api
|
|
23
|
+
.defaultSession()
|
|
24
|
+
.viewCategory(category)
|
|
25
|
+
.setPlacements(placements || api.placements.getPlacements())
|
|
26
|
+
.load()
|
|
27
|
+
renderCampaigns(data)
|
|
28
|
+
},
|
|
29
|
+
[category]
|
|
30
|
+
)
|
|
31
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useNostoApi } from "./useNostoApi"
|
|
2
|
+
import { useRenderCampaigns } from "./useRenderCampaigns"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @group Hooks
|
|
6
|
+
*/
|
|
7
|
+
export type NostoCheckoutProps = { placements?: string[] }
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* You can personalise your cart and checkout pages by using the useNostoCheckout hook.
|
|
11
|
+
*
|
|
12
|
+
* @group Hooks
|
|
13
|
+
*/
|
|
14
|
+
export function useNostoCheckout(props?: NostoCheckoutProps) {
|
|
15
|
+
const { renderCampaigns } = useRenderCampaigns()
|
|
16
|
+
|
|
17
|
+
useNostoApi(async api => {
|
|
18
|
+
const data = await api
|
|
19
|
+
.defaultSession()
|
|
20
|
+
.viewCart()
|
|
21
|
+
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
22
|
+
.load()
|
|
23
|
+
renderCampaigns(data)
|
|
24
|
+
})
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useNostoApi } from "./useNostoApi"
|
|
2
|
+
import { useRenderCampaigns } from "./useRenderCampaigns"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @group Hooks
|
|
6
|
+
*/
|
|
7
|
+
export type NostoHomeProps = { placements?: string[] }
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* You can personalise your home page by using the useNostoHome hook.
|
|
11
|
+
*
|
|
12
|
+
* @group Hooks
|
|
13
|
+
*/
|
|
14
|
+
export function useNostoHome(props?: NostoHomeProps) {
|
|
15
|
+
const { renderCampaigns } = useRenderCampaigns()
|
|
16
|
+
|
|
17
|
+
useNostoApi(async api => {
|
|
18
|
+
const data = await api
|
|
19
|
+
.defaultSession()
|
|
20
|
+
.viewFrontPage()
|
|
21
|
+
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
22
|
+
.load()
|
|
23
|
+
renderCampaigns(data)
|
|
24
|
+
})
|
|
25
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { snakeize } from "../utils/snakeize"
|
|
2
|
+
import { Order } from "../types"
|
|
3
|
+
import { useRenderCampaigns } from "./useRenderCampaigns"
|
|
4
|
+
import { useNostoApi } from "./useNostoApi"
|
|
5
|
+
import { ToCamelCase } from "../utils/types"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @group Hooks
|
|
9
|
+
*/
|
|
10
|
+
export type NostoOrderProps = {
|
|
11
|
+
order: Order | ToCamelCase<Order>
|
|
12
|
+
placements?: string[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* You can personalise your order-confirmation/thank-you page by using the `useNostoOrder` hook.
|
|
17
|
+
*
|
|
18
|
+
* @group Hooks
|
|
19
|
+
*/
|
|
20
|
+
export function useNostoOrder({ order, placements }: NostoOrderProps) {
|
|
21
|
+
const { renderCampaigns } = useRenderCampaigns()
|
|
22
|
+
|
|
23
|
+
useNostoApi(
|
|
24
|
+
async api => {
|
|
25
|
+
const data = await api
|
|
26
|
+
.defaultSession()
|
|
27
|
+
.addOrder(snakeize(order))
|
|
28
|
+
.setPlacements(placements || api.placements.getPlacements())
|
|
29
|
+
.load()
|
|
30
|
+
renderCampaigns(data)
|
|
31
|
+
},
|
|
32
|
+
[order],
|
|
33
|
+
{ deep: true }
|
|
34
|
+
)
|
|
35
|
+
}
|