@commercelayer/react-components 4.6.0-beta.1 → 4.7.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.
- package/README.md +302 -0
- package/lib/cjs/components/customers/MyAccountLink.d.ts +4 -0
- package/lib/cjs/components/customers/MyAccountLink.js +1 -1
- package/lib/cjs/components/customers/MyIdentityLink.d.ts +4 -0
- package/lib/cjs/components/customers/MyIdentityLink.js +1 -1
- package/lib/cjs/components/line_items/LineItemQuantity.d.ts +2 -0
- package/lib/cjs/components/line_items/LineItemQuantity.js +1 -1
- package/lib/cjs/components/line_items/LineItemRemoveLink.d.ts +2 -0
- package/lib/cjs/components/line_items/LineItemRemoveLink.js +1 -1
- package/lib/cjs/components/orders/CartLink.d.ts +4 -0
- package/lib/cjs/components/orders/CartLink.js +1 -1
- package/lib/cjs/components/orders/HostedCart.d.ts +5 -1
- package/lib/cjs/components/orders/HostedCart.js +1 -1
- package/lib/cjs/components/prices/PricesContainer.d.ts +29 -0
- package/lib/cjs/hooks/useOrderContainer.d.ts +1 -1
- package/lib/cjs/utils/getApplicationLink.d.ts +2 -1
- package/lib/cjs/utils/getApplicationLink.js +1 -1
- package/lib/esm/components/customers/MyAccountLink.d.ts +4 -0
- package/lib/esm/components/customers/MyAccountLink.js +1 -1
- package/lib/esm/components/customers/MyIdentityLink.d.ts +4 -0
- package/lib/esm/components/customers/MyIdentityLink.js +1 -1
- package/lib/esm/components/line_items/LineItemQuantity.d.ts +2 -0
- package/lib/esm/components/line_items/LineItemQuantity.js +1 -1
- package/lib/esm/components/line_items/LineItemRemoveLink.d.ts +2 -0
- package/lib/esm/components/line_items/LineItemRemoveLink.js +1 -1
- package/lib/esm/components/orders/CartLink.d.ts +4 -0
- package/lib/esm/components/orders/CartLink.js +1 -1
- package/lib/esm/components/orders/HostedCart.d.ts +5 -1
- package/lib/esm/components/orders/HostedCart.js +1 -1
- package/lib/esm/components/prices/PricesContainer.d.ts +29 -0
- package/lib/esm/hooks/useOrderContainer.d.ts +1 -1
- package/lib/esm/utils/getApplicationLink.d.ts +2 -1
- package/lib/esm/utils/getApplicationLink.js +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# Commerce Layer React Components
|
|
2
|
+
|
|
3
|
+
A collection of reusable React components that makes it super fast and simple to build your own custom commerce UI, leveraging [Commerce Layer API](https://docs.commercelayer.io/api/).
|
|
4
|
+
|
|
5
|
+
### What is Commerce Layer?
|
|
6
|
+
|
|
7
|
+
[Commerce Layer](https://commercelayer.io) is a multi-market commerce API and order management system that lets you add global shopping capabilities to any website, mobile app, chatbot, wearable, voice, or IoT device, with ease. Compose your stack with the best-of-breed tools you already mastered and love. Make any experience shoppable, anywhere, through a blazing-fast, enterprise-grade, and secure API.
|
|
8
|
+
|
|
9
|
+
# Getting started
|
|
10
|
+
|
|
11
|
+
To get started with Commerce Layer React Components you need to install them and then get the credentials that will allow you to perform the API calls they wrap.
|
|
12
|
+
|
|
13
|
+
- [Installation](#installation)
|
|
14
|
+
- [Authentication](#authentication)
|
|
15
|
+
- [Import](#import)
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Commerce Layer React Components are available as an [npm package](https://www.npmjs.com/package/@commercelayer/react-components):
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
// npm
|
|
23
|
+
npm install @commercelayer/react-components
|
|
24
|
+
|
|
25
|
+
// yarn
|
|
26
|
+
yarn add @commercelayer/react-components
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Authentication
|
|
30
|
+
|
|
31
|
+
All requests to Commerce Layer API must be authenticated with an [OAuth2](https://oauth.net/2/) bearer token. Hence, to use these components, you need to get a valid access token. Once you got it, you can pass it as prop — together with the endpoint of your Commerce Layer organization — to the `CommerceLayer` component, as follow:
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
<CommerceLayer
|
|
35
|
+
accessToken="your-access-token"
|
|
36
|
+
endpoint="https://yourdomain.commercelayer.io">
|
|
37
|
+
|
|
38
|
+
{/* ... child components */}
|
|
39
|
+
|
|
40
|
+
</CommerceLayer>
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
This token will be used to authorize the API calls of all its child components. That's why the presence of (at least) one `CommerceLayer` component is mandatory — it must wrap every other component you need to use.
|
|
44
|
+
|
|
45
|
+
> Please note that — in case you need to fetch data with different tokens (i.e. from different organizations or using apps with different roles and permissions) — nothing prevents you from putting as many `CommerceLayer` components you want in the same page.
|
|
46
|
+
|
|
47
|
+
You can check [our documentation](https://docs.commercelayer.io/api/authentication) for more information about the available authorization flows and leverage [Commerce Layer JS Auth](https://github.com/commercelayer/commercelayer-js-auth) to easily interact with our authentication API.
|
|
48
|
+
|
|
49
|
+
## Import
|
|
50
|
+
|
|
51
|
+
You can use ES6 named import with every single component you plan to use (in addition to `CommerceLayer` one), as follow:
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
import { CommerceLayer, ...otherComponents } from '@commercelayer/react-components'
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Check [this summary table](#list-of-components) for the complete (and constantly updated) list of available components.
|
|
58
|
+
|
|
59
|
+
# Usage
|
|
60
|
+
|
|
61
|
+
The code snippets below show how to put into action Commerce Layer React Components in some common use cases. Check the [`pages`](/pages) folder of this repository for more detailed examples.
|
|
62
|
+
|
|
63
|
+
- [Prices](#prices)
|
|
64
|
+
- [Add to cart](#add-to-cart)
|
|
65
|
+
- [Shopping cart](#shopping-cart)
|
|
66
|
+
- [Cart summary](#cart-summary)
|
|
67
|
+
|
|
68
|
+
Under the hood, our React components are built on top of [Commerce Layer JS SDK](https://github.com/commercelayer/commercelayer-sdk) — feel free to use it if you want to develop your custom ones.
|
|
69
|
+
|
|
70
|
+
## Prices
|
|
71
|
+
|
|
72
|
+
This example shows how to use Commerce Layer React Components to display the prices of some SKUs, identified by their SKU codes:
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
import {
|
|
76
|
+
CommerceLayer,
|
|
77
|
+
PricesContainer,
|
|
78
|
+
Price
|
|
79
|
+
} from '@commercelayer/react-components'
|
|
80
|
+
|
|
81
|
+
// your code [...]
|
|
82
|
+
|
|
83
|
+
<CommerceLayer accessToken="your-access-token" endpoint="https://yourdomain.commercelayer.io">
|
|
84
|
+
<PricesContainer>
|
|
85
|
+
<Price
|
|
86
|
+
skuCode="BABYONBU000000E63E7412MX"
|
|
87
|
+
className="your-custom-class"
|
|
88
|
+
compareClassName="your-custom-class"
|
|
89
|
+
/>
|
|
90
|
+
<Price
|
|
91
|
+
skuCode="CANVASAU000000FFFFFF1824"
|
|
92
|
+
className="your-custom-class"
|
|
93
|
+
compareClassName="your-custom-class"
|
|
94
|
+
/>
|
|
95
|
+
<Price
|
|
96
|
+
skuCode="LSLEEVMM000000E63E74LXXX"
|
|
97
|
+
className="your-custom-class"
|
|
98
|
+
compareClassName="your-custom-class"
|
|
99
|
+
/>
|
|
100
|
+
</PricesContainer>
|
|
101
|
+
</CommerceLayer>
|
|
102
|
+
|
|
103
|
+
// your code [...]
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
You can style the selling price and the full price as you like by passing the `className` and `compareClassName` props to the `Price` component. You can choose not to show the full price by passing `showCompare={false}` (default is `true`).
|
|
107
|
+
|
|
108
|
+
If you need to paginate the list of prices, pass the `perPage` prop to the `PricesContainer` component (default is `10`) — to learn how pagination works, check our [documentation](https://docs.commercelayer.io/api/pagination).
|
|
109
|
+
|
|
110
|
+
## Add to cart
|
|
111
|
+
|
|
112
|
+
This example shows how to use Commerce Layer React Components to implement the "add to cart" functionality on your page, showing the price of the chosen SKU, the possibility to select a variant and its quantity, the information about its availability, and the related button to perform the action:
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
import {
|
|
116
|
+
CommerceLayer,
|
|
117
|
+
OrderContainer,
|
|
118
|
+
OrderStorage,
|
|
119
|
+
PricesContainer,
|
|
120
|
+
Price,
|
|
121
|
+
AddToCartButton,
|
|
122
|
+
AvailabilityContainer,
|
|
123
|
+
AvailabilityTemplate
|
|
124
|
+
} from '@commercelayer/react-components'
|
|
125
|
+
|
|
126
|
+
// your code [...]
|
|
127
|
+
|
|
128
|
+
<CommerceLayer accessToken="your-access-token" endpoint="https://yourdomain.commercelayer.io">
|
|
129
|
+
<OrderStorage persistKey="your-persist-key">
|
|
130
|
+
<OrderContainer
|
|
131
|
+
attributes={{
|
|
132
|
+
cart_url: 'https://yourdomain.com/cart',
|
|
133
|
+
return_url: 'https://yourdomain.com/return',
|
|
134
|
+
privacy_url: 'https://yourdomain.com/privacy'
|
|
135
|
+
}}>
|
|
136
|
+
<PricesContainer>
|
|
137
|
+
<Price skuCode="BABYONBU000000E63E746MXX" />
|
|
138
|
+
</PricesContainer>
|
|
139
|
+
<AddToCartButton quantity={1} skuCode="BABYONBU000000E63E746MXX" />
|
|
140
|
+
<AvailabilityContainer skuCode="BABYONBU000000E63E746MXX">
|
|
141
|
+
<AvailabilityTemplate />
|
|
142
|
+
</AvailabilityContainer>
|
|
143
|
+
</ItemContainer>
|
|
144
|
+
</OrderContainer>
|
|
145
|
+
</OrderStorage>
|
|
146
|
+
</CommerceLayer>
|
|
147
|
+
|
|
148
|
+
// your code [...]
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
For each variant you can define the custom name (i.e. its translation based on location) and image that will be shown on the corresponding line item, by passing the `lineItem` prop to the `AddToCartButton` component and properly setting the `lineItem` key — all these content data are usually taken from your CMS, since Commerce Layer doesn't manage any kind of content.
|
|
152
|
+
|
|
153
|
+
When you add a product to your shopping cart:
|
|
154
|
+
|
|
155
|
+
- if there is an order stored in the Local Storage identified by a key that matches the `persistKey` property, a line item is created and it is associated with that order;
|
|
156
|
+
- if no order in the Local Storage matches the `persistKey` property, a new order is created and stored.
|
|
157
|
+
|
|
158
|
+
> A common best practice — especially for multi-country ecommerce — is to use as `persistKey` a key containing the country code, so that you have a different shopping cart for each country.
|
|
159
|
+
|
|
160
|
+
If you need to set some of the [order object](https://docs.commercelayer.io/developers/v/api-reference/orders/object) attributes at the moment of the order creation, pass to the optional prop `attributes` to the `OrderContainer` component.
|
|
161
|
+
|
|
162
|
+
## Shopping cart
|
|
163
|
+
|
|
164
|
+
This example shows how to use Commerce Layer React Components to build a shopping cart UI, containing the items that are going to be purchased with all their information (image, name, quantity, price) and the option to possibly remove some of them:
|
|
165
|
+
|
|
166
|
+
```
|
|
167
|
+
import {
|
|
168
|
+
CommerceLayer,
|
|
169
|
+
OrderContainer,
|
|
170
|
+
OrderStorage,
|
|
171
|
+
LineItemsContainer,
|
|
172
|
+
LineItemsCount,
|
|
173
|
+
LineItem,
|
|
174
|
+
LineItemImage,
|
|
175
|
+
LineItemName,
|
|
176
|
+
LineItemQuantity,
|
|
177
|
+
LineItemAmount,
|
|
178
|
+
LineItemRemoveLink,
|
|
179
|
+
Errors
|
|
180
|
+
} from '@commercelayer/react-components'
|
|
181
|
+
|
|
182
|
+
// your code [...]
|
|
183
|
+
|
|
184
|
+
<CommerceLayer accessToken="your-access-token" endpoint="https://yourdomain.commercelayer.io">
|
|
185
|
+
<OrderStorage persistKey="your-persist-key">
|
|
186
|
+
<OrderContainer>
|
|
187
|
+
<LineItemsContainer>
|
|
188
|
+
<p className="your-custom-class">
|
|
189
|
+
Your shopping cart contains <LineItemsCount /> items
|
|
190
|
+
</p>
|
|
191
|
+
<LineItem>
|
|
192
|
+
<LineItemImage width={50} />
|
|
193
|
+
<LineItemName />
|
|
194
|
+
<LineItemQuantity max={10} />
|
|
195
|
+
<Errors resource="lineItem" field="quantity" />
|
|
196
|
+
<LineItemAmount />
|
|
197
|
+
<LineItemRemoveLink />
|
|
198
|
+
</LineItem>
|
|
199
|
+
</LineItemsContainer>
|
|
200
|
+
</OrderContainer>
|
|
201
|
+
</OrderStorage>
|
|
202
|
+
</CommerceLayer>
|
|
203
|
+
|
|
204
|
+
// your code [...]
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
The `Errors` component lets you show the error (if present) returned by our API on a single attribute of a specific resource. You can customize the error message as you like by passing the `messages` prop to the component.
|
|
208
|
+
|
|
209
|
+
## Cart summary
|
|
210
|
+
|
|
211
|
+
This example shows how to use Commerce Layer React Components to show a sample order summary with all the order line items (including discounts, shipping costs, taxes, and gift cards — if present) and totals:
|
|
212
|
+
|
|
213
|
+
```
|
|
214
|
+
import {
|
|
215
|
+
CommerceLayer,
|
|
216
|
+
OrderContainer,
|
|
217
|
+
OrderStorage,
|
|
218
|
+
SubTotalAmount,
|
|
219
|
+
DiscountAmount,
|
|
220
|
+
ShippingAmount,
|
|
221
|
+
TaxesAmount,
|
|
222
|
+
GiftCardAmount,
|
|
223
|
+
TotalAmount,
|
|
224
|
+
CheckoutLink
|
|
225
|
+
} from '@commercelayer/react-components'
|
|
226
|
+
|
|
227
|
+
// your code [...]
|
|
228
|
+
|
|
229
|
+
<CommerceLayer accessToken="your-access-token" endpoint="https://yourdomain.commercelayer.io">
|
|
230
|
+
<OrderStorage persistKey="your-persist-key">
|
|
231
|
+
<OrderContainer>
|
|
232
|
+
<SubTotalAmount />
|
|
233
|
+
<DiscountAmount />
|
|
234
|
+
<ShippingAmount />
|
|
235
|
+
<TaxesAmount />
|
|
236
|
+
<GiftCardAmount />
|
|
237
|
+
<TotalAmount />
|
|
238
|
+
<CheckoutLink />
|
|
239
|
+
</OrderContainer>
|
|
240
|
+
</OrderStorage>
|
|
241
|
+
</CommerceLayer>
|
|
242
|
+
|
|
243
|
+
// your code [...]
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
You can change the amount format of each line of the summary by passing the `format` prop to the desired component (default is `formatted`).
|
|
247
|
+
|
|
248
|
+
The `CheckoutLink` component lets you proceed to checkout and links to the checkout URL configured on Commerce Layer (_Settings → Markets_).
|
|
249
|
+
|
|
250
|
+
# List of components
|
|
251
|
+
|
|
252
|
+
These are the currently available Commerce Layer React Components.
|
|
253
|
+
|
|
254
|
+
> Please note that not every Commerce Layer React component can be nested into any other one.
|
|
255
|
+
|
|
256
|
+
For each component, the table below shows its props and the list of the permitted children (if any):
|
|
257
|
+
|
|
258
|
+
>
|
|
259
|
+
|
|
260
|
+
| Name | Props | Children |
|
|
261
|
+
| -------------------------- | -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
262
|
+
| `AddToCartButton` | `disabled`<br />`label`<br />`skuCode`<br />`lineItem`<br/>`lineItemOption`<br/>`quantity` |
|
|
263
|
+
| `AvailabilityContainer` | `skuCode` | `AvailabilityTemplate` |
|
|
264
|
+
| `AvailabilityTemplate` | `showShippingMethodName`<br />`timeFormat` |
|
|
265
|
+
| `CheckoutLink` | `label` |
|
|
266
|
+
| `CommerceLayer` | `accessToken`<br />`endpoint` | `GiftCardContainer`<br />`OrderContainer`<br />`PricesContainer` |
|
|
267
|
+
| `DiscountAmount` | `className`<br />`format`<br />`id`<br />`name`<br />`style` |
|
|
268
|
+
| `Errors` | `field`<br />`messages`<br />`resource` |
|
|
269
|
+
| `ExternalFunction` | `url` | `AddToCartButton` |
|
|
270
|
+
| `GiftCard` | `onSubmit` | `Errors`<br />`GiftCardCurrencySelector`<br />`GiftCardInput`<br />`MetadataInput`<br />`SubmitButton` |
|
|
271
|
+
| `GiftCardAmount` | `className`<br />`format`<br />`id`<br />`name`<br />`style` |
|
|
272
|
+
| `GiftCardContainer` | | `GiftCard`<br/>`Errors` |
|
|
273
|
+
| `GiftCardCurrencySelector` | `placeholder`<br />`required`<br />`value` |
|
|
274
|
+
| `GiftCardInput` | `name`<br />`placeholder`<br />`type` |
|
|
275
|
+
`LineItem` | `type` | `Errors`<br />`LineItemAmount`<br />`LineItemImage`<br />`LineItemName`<br />`LineItemOptions`<br />`LineItemQuantity`<br />`LineItemRemoveLink` |
|
|
276
|
+
| `LineItemAmount` | `className`<br />`format`<br />`id`<br />`name`<br />`style`<br />`type` |
|
|
277
|
+
| `LineItemImage` | `width` |
|
|
278
|
+
| `LineItemName` | |
|
|
279
|
+
| `LineItemOption` | `keyClassName`<br />`keyId`<br />`keyStyle`<br />`name`<br />`valueClassName` |
|
|
280
|
+
| `LineItemOptions` | `title`<br /> `showName`<br />`skuOptionId` | `LineItemOption` |
|
|
281
|
+
| `LineItemQuantity` | `disabled`<br />`max` |
|
|
282
|
+
| `LineItemRemoveLink` | `label` |
|
|
283
|
+
| `LineItemsContainer` | `filters`<br />`loader` | `LineItem`<br />`LineItemsCount` |
|
|
284
|
+
| `LineItemsCount` | `className`<br />`id`<br />`name`<br />`style` |
|
|
285
|
+
| `MetadataInput` | `name`<br />`onChange`<br />`placeholder`<br />`type` |
|
|
286
|
+
| `OrderContainer` | `attributes`<br />`metadata`<br />`orderId` | `CheckoutLink`<br />`DiscountAmount`<br />`GiftCardAmount`<br />`GiftCardContainer`<br />`ItemContainer`<br />`LineItemsContainer`<br />`ShippingAmount`<br />`SubTotalAmount`<br />`TaxesAmount`<br />`TotalAmount` |
|
|
287
|
+
| `OrderStorage` | `clearWhenPlaced`<br />`persistKey` | `OrderContainer` |
|
|
288
|
+
| `Price` | `compareClassName`<br />`showCompare`<br />`skuCode` |
|
|
289
|
+
| `PricesContainer` | `filters`<br />`loader`<br />`perPage`<br />`skuCode` | `Price` |
|
|
290
|
+
`ShippingAmount` | `className`<br />`format`<br />`id`<br />`name`<br />`style` |
|
|
291
|
+
| `SubmitButton` | `label` |
|
|
292
|
+
| `SubTotalAmount` | `className`<br />`format`<br />`id`<br />`name`<br />`style` |
|
|
293
|
+
| `TaxesAmount` | `className`<br />`format`<br />`id`<br />`name`<br />`style` |
|
|
294
|
+
| `TotalAmount` | `className`<br />`format`<br />`id`<br />`name`<br />`style` |
|
|
295
|
+
|
|
296
|
+
For more detailed information on each components (i.e. prop types and default values, required props, etc.), have a look at the components folder [`packages/react-components/src/components`](/packages/react-components/src/components).
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
### License
|
|
301
|
+
|
|
302
|
+
This repository is published under the [MIT](LICENSE) license.
|
|
@@ -18,6 +18,10 @@ interface Props extends Omit<JSX.IntrinsicElements['a'], 'children'> {
|
|
|
18
18
|
* The label of the link
|
|
19
19
|
*/
|
|
20
20
|
label?: string | JSX.Element;
|
|
21
|
+
/**
|
|
22
|
+
* The domain of your forked application
|
|
23
|
+
*/
|
|
24
|
+
customDomain?: string;
|
|
21
25
|
}
|
|
22
26
|
export declare function MyAccountLink(props: Props): JSX.Element;
|
|
23
27
|
export default MyAccountLink;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MyAccountLink=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),getApplicationLink_1=require("../../utils/getApplicationLink"),getDomain_1=require("../../utils/getDomain"),jwt_1=tslib_1.__importDefault(require("../../utils/jwt"));function MyAccountLink(props){const{label="Go to my account",children}=props,p=tslib_1.__rest(props,["label","children"]),{accessToken,endpoint}=(0,react_1.useContext)(CommerceLayerContext_1.default);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyAccountLink` outside of `CommerceLayer`");const{domain,slug}=(0,getDomain_1.getDomain)(endpoint),disabled=!("owner"in(0,jwt_1.default)(accessToken)),href=(0,getApplicationLink_1.getApplicationLink)({slug,accessToken,applicationType:"my-account",domain}),parentProps=Object.assign({disabled,label,href},p);function handleClick(e){disabled&&e.preventDefault()}return children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("a",Object.assign({"aria-disabled":disabled,onClick:handleClick,href},p,{children:label}))}exports.MyAccountLink=MyAccountLink,exports.default=MyAccountLink;
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MyAccountLink=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),getApplicationLink_1=require("../../utils/getApplicationLink"),getDomain_1=require("../../utils/getDomain"),jwt_1=tslib_1.__importDefault(require("../../utils/jwt"));function MyAccountLink(props){const{label="Go to my account",children,customDomain}=props,p=tslib_1.__rest(props,["label","children","customDomain"]),{accessToken,endpoint}=(0,react_1.useContext)(CommerceLayerContext_1.default);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyAccountLink` outside of `CommerceLayer`");const{domain,slug}=(0,getDomain_1.getDomain)(endpoint),disabled=!("owner"in(0,jwt_1.default)(accessToken)),href=(0,getApplicationLink_1.getApplicationLink)({slug,accessToken,applicationType:"my-account",domain,customDomain}),parentProps=Object.assign({disabled,label,href},p);function handleClick(e){disabled&&e.preventDefault()}return children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("a",Object.assign({"aria-disabled":disabled,onClick:handleClick,href},p,{children:label}))}exports.MyAccountLink=MyAccountLink,exports.default=MyAccountLink;
|
|
@@ -30,6 +30,10 @@ interface Props extends Omit<JSX.IntrinsicElements['a'], 'children'> {
|
|
|
30
30
|
* The return url to redirect the user after the login/signup
|
|
31
31
|
*/
|
|
32
32
|
returnUrl?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The domain of your forked application
|
|
35
|
+
*/
|
|
36
|
+
customDomain?: string;
|
|
33
37
|
}
|
|
34
38
|
export declare function MyIdentityLink(props: Props): JSX.Element;
|
|
35
39
|
export default MyIdentityLink;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MyIdentityLink=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),getApplicationLink_1=require("../../utils/getApplicationLink"),getDomain_1=require("../../utils/getDomain");function MyIdentityLink(props){const{label,children,type,clientId,scope,returnUrl}=props,p=tslib_1.__rest(props,["label","children","type","clientId","scope","returnUrl"]),{accessToken,endpoint}=(0,react_1.useContext)(CommerceLayerContext_1.default);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyIdentityLink` outside of `CommerceLayer`");const{domain,slug}=(0,getDomain_1.getDomain)(endpoint),href=(0,getApplicationLink_1.getApplicationLink)({slug,accessToken,applicationType:"identity",domain,modeType:type,clientId,scope,returnUrl:returnUrl??window.location.href}),parentProps=Object.assign({label,href,clientId,scope},p);return children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("a",Object.assign({href},p,{children:label}))}exports.MyIdentityLink=MyIdentityLink,exports.default=MyIdentityLink;
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MyIdentityLink=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),getApplicationLink_1=require("../../utils/getApplicationLink"),getDomain_1=require("../../utils/getDomain");function MyIdentityLink(props){const{label,children,type,clientId,scope,returnUrl,customDomain}=props,p=tslib_1.__rest(props,["label","children","type","clientId","scope","returnUrl","customDomain"]),{accessToken,endpoint}=(0,react_1.useContext)(CommerceLayerContext_1.default);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyIdentityLink` outside of `CommerceLayer`");const{domain,slug}=(0,getDomain_1.getDomain)(endpoint),href=(0,getApplicationLink_1.getApplicationLink)({slug,accessToken,applicationType:"identity",domain,modeType:type,clientId,scope,returnUrl:returnUrl??window.location.href,customDomain}),parentProps=Object.assign({label,href,clientId,scope},p);return children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("a",Object.assign({href},p,{children:label}))}exports.MyIdentityLink=MyIdentityLink,exports.default=MyIdentityLink;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { type ChildrenFunction } from '../../typings/index';
|
|
3
|
+
import { type LineItem } from '@commercelayer/sdk';
|
|
3
4
|
interface ChildrenProps extends Omit<Props, 'children'> {
|
|
4
5
|
quantity: number;
|
|
5
6
|
handleChange: (event: React.MouseEvent<HTMLSelectElement>) => void;
|
|
7
|
+
lineItem?: LineItem;
|
|
6
8
|
}
|
|
7
9
|
type Props = {
|
|
8
10
|
children?: ChildrenFunction<ChildrenProps>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.LineItemQuantity=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),LineItemChildrenContext_1=tslib_1.__importDefault(require("../../context/LineItemChildrenContext")),LineItemContext_1=tslib_1.__importDefault(require("../../context/LineItemContext")),Parent_1=tslib_1.__importDefault(require("../utils/Parent"));function LineItemQuantity(props){var _a;const{max=50,readonly=!1}=props,p=tslib_1.__rest(props,["max","readonly"]),{lineItem}=(0,react_1.useContext)(LineItemChildrenContext_1.default),{updateLineItem}=(0,react_1.useContext)(LineItemContext_1.default),options=[];for(let i=1;i<=max;i++)options.push((0,jsx_runtime_1.jsx)("option",{value:`${i}`,children:i},i));const handleChange=e=>{const quantity2=Number(e.target.value);updateLineItem&&lineItem&&updateLineItem(lineItem.id,quantity2)},quantity=lineItem?.quantity,parentProps=Object.assign({handleChange,quantity},props);return props.children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children:props.children})):readonly?(0,jsx_runtime_1.jsx)("span",Object.assign({},p,{children:quantity})):(0,jsx_runtime_1.jsx)("select",Object.assign({"data-testid":lineItem?.sku_code,title:(_a=lineItem?.name)!==null&&_a!==void 0?_a:"",value:quantity,onChange:handleChange},p,{children:options}))}exports.LineItemQuantity=LineItemQuantity,exports.default=LineItemQuantity;
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.LineItemQuantity=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),LineItemChildrenContext_1=tslib_1.__importDefault(require("../../context/LineItemChildrenContext")),LineItemContext_1=tslib_1.__importDefault(require("../../context/LineItemContext")),Parent_1=tslib_1.__importDefault(require("../utils/Parent"));function LineItemQuantity(props){var _a;const{max=50,readonly=!1}=props,p=tslib_1.__rest(props,["max","readonly"]),{lineItem}=(0,react_1.useContext)(LineItemChildrenContext_1.default),{updateLineItem}=(0,react_1.useContext)(LineItemContext_1.default),options=[];for(let i=1;i<=max;i++)options.push((0,jsx_runtime_1.jsx)("option",{value:`${i}`,children:i},i));const handleChange=e=>{const quantity2=Number(e.target.value);updateLineItem&&lineItem&&updateLineItem(lineItem.id,quantity2)},quantity=lineItem?.quantity,parentProps=Object.assign({handleChange,quantity,lineItem},props);return props.children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children:props.children})):readonly?(0,jsx_runtime_1.jsx)("span",Object.assign({},p,{children:quantity})):(0,jsx_runtime_1.jsx)("select",Object.assign({"data-testid":lineItem?.sku_code,title:(_a=lineItem?.name)!==null&&_a!==void 0?_a:"",value:quantity,onChange:handleChange},p,{children:options}))}exports.LineItemQuantity=LineItemQuantity,exports.default=LineItemQuantity;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { type PropsWithoutRef } from 'react';
|
|
2
2
|
import { type ChildrenFunction } from '../../typings/index';
|
|
3
|
+
import { type LineItem } from '@commercelayer/sdk';
|
|
3
4
|
interface ChildrenProps extends Omit<Props, 'children'> {
|
|
4
5
|
handleRemove: (event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
5
6
|
label?: string;
|
|
7
|
+
lineItem?: LineItem;
|
|
6
8
|
}
|
|
7
9
|
interface Props extends PropsWithoutRef<Omit<JSX.IntrinsicElements['a'], 'children'>> {
|
|
8
10
|
children?: ChildrenFunction<ChildrenProps>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.LineItemRemoveLink=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),LineItemChildrenContext_1=tslib_1.__importDefault(require("../../context/LineItemChildrenContext")),LineItemContext_1=tslib_1.__importDefault(require("../../context/LineItemContext")),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),useCustomContext_1=tslib_1.__importDefault(require("../../utils/hooks/useCustomContext"));function LineItemRemoveLink(props){var _a;const{label="Remove",onClick}=props,{lineItem}=(0,useCustomContext_1.default)({context:LineItemChildrenContext_1.default,contextComponentName:"LineItem",currentComponentName:"LineItemRemoveLink",key:"lineItem"}),{deleteLineItem}=(0,react_1.useContext)(LineItemContext_1.default),handleRemove=e=>{e.preventDefault(),deleteLineItem!=null&&lineItem!=null&&deleteLineItem(lineItem.id),onClick?.(e)},parentProps=Object.assign({handleRemove},props);return props.children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children:props.children})):(0,jsx_runtime_1.jsx)("a",Object.assign({"data-testid":`line-item-remove-link-${(_a=lineItem?.sku_code)!==null&&_a!==void 0?_a:""}`},props,{href:"#",onClick:handleRemove,children:label}))}exports.LineItemRemoveLink=LineItemRemoveLink,exports.default=LineItemRemoveLink;
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.LineItemRemoveLink=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),LineItemChildrenContext_1=tslib_1.__importDefault(require("../../context/LineItemChildrenContext")),LineItemContext_1=tslib_1.__importDefault(require("../../context/LineItemContext")),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),useCustomContext_1=tslib_1.__importDefault(require("../../utils/hooks/useCustomContext"));function LineItemRemoveLink(props){var _a;const{label="Remove",onClick}=props,{lineItem}=(0,useCustomContext_1.default)({context:LineItemChildrenContext_1.default,contextComponentName:"LineItem",currentComponentName:"LineItemRemoveLink",key:"lineItem"}),{deleteLineItem}=(0,react_1.useContext)(LineItemContext_1.default),handleRemove=e=>{e.preventDefault(),deleteLineItem!=null&&lineItem!=null&&deleteLineItem(lineItem.id),onClick?.(e)},parentProps=Object.assign({handleRemove,lineItem},props);return props.children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children:props.children})):(0,jsx_runtime_1.jsx)("a",Object.assign({"data-testid":`line-item-remove-link-${(_a=lineItem?.sku_code)!==null&&_a!==void 0?_a:""}`},props,{href:"#",onClick:handleRemove,children:label}))}exports.LineItemRemoveLink=LineItemRemoveLink,exports.default=LineItemRemoveLink;
|
|
@@ -30,6 +30,10 @@ interface Props extends Omit<JSX.IntrinsicElements['a'], 'children'> {
|
|
|
30
30
|
* The type of the cart. Defaults to undefined. If set to 'mini', the cart will open in a modal.
|
|
31
31
|
*/
|
|
32
32
|
type?: 'mini';
|
|
33
|
+
/**
|
|
34
|
+
* The domain of your forked application
|
|
35
|
+
*/
|
|
36
|
+
customDomain?: string;
|
|
33
37
|
}
|
|
34
38
|
export declare function CartLink(props: Props): JSX.Element | null;
|
|
35
39
|
export default CartLink;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CartLink=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),getApplicationLink_1=require("../../utils/getApplicationLink"),getDomain_1=require("../../utils/getDomain"),events_1=require("../../utils/events");function CartLink(props){const{label,children,type}=props,p=tslib_1.__rest(props,["label","children","type"]),{order,createOrder}=(0,react_1.useContext)(OrderContext_1.default),{accessToken,endpoint}=(0,react_1.useContext)(CommerceLayerContext_1.default);if(accessToken==null)throw new Error("Cannot use `CartLink` outside of `CommerceLayer`");if(endpoint==null)throw new Error("Cannot use `CartLink` outside of `CommerceLayer`");const{domain,slug}=(0,getDomain_1.getDomain)(endpoint),href=slug&&order?.id?(0,getApplicationLink_1.getApplicationLink)({slug,orderId:order?.id,accessToken,domain,applicationType:"cart"}):"",handleClick=event=>tslib_1.__awaiter(this,void 0,void 0,function*(){if(event.preventDefault(),type!=="mini")if(order?.id)location.href=href;else{const orderId=yield createOrder({});slug&&(location.href=(0,getApplicationLink_1.getApplicationLink)({slug,orderId,accessToken,domain,applicationType:"cart"}))}else(0,events_1.publish)("open-cart")}),parentProps=Object.assign({handleClick,label,href,orderId:order?.id,accessToken},p);return accessToken?children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("a",Object.assign({href},p,{onClick:e=>{handleClick(e)},children:label})):null}exports.CartLink=CartLink,exports.default=CartLink;
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CartLink=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),getApplicationLink_1=require("../../utils/getApplicationLink"),getDomain_1=require("../../utils/getDomain"),events_1=require("../../utils/events");function CartLink(props){const{label,children,type,customDomain}=props,p=tslib_1.__rest(props,["label","children","type","customDomain"]),{order,createOrder}=(0,react_1.useContext)(OrderContext_1.default),{accessToken,endpoint}=(0,react_1.useContext)(CommerceLayerContext_1.default);if(accessToken==null)throw new Error("Cannot use `CartLink` outside of `CommerceLayer`");if(endpoint==null)throw new Error("Cannot use `CartLink` outside of `CommerceLayer`");const{domain,slug}=(0,getDomain_1.getDomain)(endpoint),href=slug&&order?.id?(0,getApplicationLink_1.getApplicationLink)({slug,orderId:order?.id,accessToken,domain,applicationType:"cart",customDomain}):"",handleClick=event=>tslib_1.__awaiter(this,void 0,void 0,function*(){if(event.preventDefault(),type!=="mini")if(order?.id)location.href=href;else{const orderId=yield createOrder({});slug&&(location.href=(0,getApplicationLink_1.getApplicationLink)({slug,orderId,accessToken,domain,applicationType:"cart",customDomain}))}else(0,events_1.publish)("open-cart")}),parentProps=Object.assign({handleClick,label,href,orderId:order?.id,accessToken},p);return accessToken?children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("a",Object.assign({href},p,{onClick:e=>{handleClick(e)},children:label})):null}exports.CartLink=CartLink,exports.default=CartLink;
|
|
@@ -27,6 +27,10 @@ interface Props extends Omit<JSX.IntrinsicElements['div'], 'children' | 'style'>
|
|
|
27
27
|
* A function that will be called when the cart is open and the background is clicked.
|
|
28
28
|
*/
|
|
29
29
|
handleOpen?: () => void;
|
|
30
|
+
/**
|
|
31
|
+
* The domain of your forked application.
|
|
32
|
+
*/
|
|
33
|
+
customDomain?: string;
|
|
30
34
|
}
|
|
31
|
-
export declare function HostedCart({ type, openAdd, style, open, handleOpen, ...props }: Props): JSX.Element | null;
|
|
35
|
+
export declare function HostedCart({ type, openAdd, style, open, handleOpen, customDomain, ...props }: Props): JSX.Element | null;
|
|
32
36
|
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.HostedCart=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),OrderStorageContext_1=tslib_1.__importDefault(require("../../context/OrderStorageContext")),getApplicationLink_1=require("../../utils/getApplicationLink"),getDomain_1=require("../../utils/getDomain"),useCustomContext_1=tslib_1.__importDefault(require("../../utils/hooks/useCustomContext")),react_1=require("react"),iframe_resizer_1=require("iframe-resizer"),events_1=require("../../utils/events"),defaultIframeStyle={width:"1px",minWidth:"100%",minHeight:"100%",border:"none"},defaultContainerStyle={position:"fixed",top:"0",right:"-25rem",height:"100%",width:"23rem",transition:"right 0.5s ease-in-out",zIndex:"0",overflow:"auto"},defaultBackgroundStyle={opacity:"0",position:"fixed",top:"0",left:"0",height:"100%",width:"100vw",transition:"opacity 0.5s ease-in-out",zIndex:"-10",backgroundColor:"black"},defaultIconStyle={width:"1.25rem",height:"1.25rem"},defaultIconContainer={textAlign:"left",paddingLeft:"20px",paddingTop:"20px",background:"#ffffff",color:"#686E6E"},defaultStyle={cart:defaultIframeStyle,container:defaultContainerStyle,background:defaultBackgroundStyle,icon:defaultIconStyle,iconContainer:defaultIconContainer};function HostedCart(_a){var _b,_c,_d,_e,{type,openAdd=!1,style,open=!1,handleOpen}=_a,props=tslib_1.__rest(_a,["type","openAdd","style","open","handleOpen"]);const[isOpen,setOpen]=(0,react_1.useState)(!1),ref=(0,react_1.useRef)(null),{accessToken,endpoint}=(0,useCustomContext_1.default)({context:CommerceLayerContext_1.default,contextComponentName:"CommerceLayer",currentComponentName:"HostedCart",key:"accessToken"}),[src,setSrc]=(0,react_1.useState)();if(accessToken==null||endpoint==null)return null;const{order,createOrder}=(0,react_1.useContext)(OrderContext_1.default),{persistKey}=(0,react_1.useContext)(OrderStorageContext_1.default),{domain,slug}=(0,getDomain_1.getDomain)(endpoint);function setOrder(openCart){var _a2;return tslib_1.__awaiter(this,void 0,void 0,function*(){const orderId=(_a2=localStorage.getItem(persistKey))!==null&&_a2!==void 0?_a2:yield createOrder({});orderId!=null&&accessToken&&(setSrc((0,getApplicationLink_1.getApplicationLink)({slug,orderId,accessToken,domain,applicationType:"cart"})),openCart&&setTimeout(()=>{handleOpen!=null?handleOpen():setOpen(!0)},300))})}function onMessage(data){var _a2;switch(data.message.type){case"update":break;case"close":type==="mini"&&(handleOpen!=null?handleOpen():setOpen(!1));break;case"blur":type==="mini"&&isOpen&&((_a2=ref.current)===null||_a2===void 0||_a2.focus());break}}(0,react_1.useEffect)(()=>{var _a2;const orderId=localStorage.getItem(persistKey);let ignore=!1;return open!=null&&open!==isOpen&&setOpen(open),openAdd&&type==="mini"&&(0,events_1.subscribe)("open-cart",()=>{window.document.body.style.overflow="hidden",src==null&&order?.id==null&&orderId==null?setOrder(!0):(src!=null&&ref.current!=null&&(ref.current.src=src),setTimeout(()=>{handleOpen!=null?handleOpen():setOpen(!0)},300))}),src==null&&order?.id==null&&orderId==null&&accessToken!=null&&!ignore&&isOpen?setOrder():src==null&&(order?.id!=null||orderId!=null)&&accessToken&&setSrc((0,getApplicationLink_1.getApplicationLink)({slug,orderId:(_a2=order?.id)!==null&&_a2!==void 0?_a2:orderId,accessToken,domain,applicationType:"cart"})),src!=null&&ref.current!=null&&(ref.current.src=src),()=>{ignore=!0,openAdd&&type==="mini"&&(0,events_1.unsubscribe)("open-cart",()=>{})}},[src,open,order?.id,accessToken]),(0,react_1.useEffect)(()=>{ref.current!=null&&(0,iframe_resizer_1.iframeResizer)({checkOrigin:!1,bodyPadding:"20px",onMessage},ref.current)},[ref.current!=null]);function onCloseCart(){window.document.body.style.removeProperty("overflow"),handleOpen!=null?handleOpen():setOpen(!1)}return src==null?null:type==="mini"?(0,jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment,{children:[(0,jsx_runtime_1.jsx)("div",{style:Object.assign(Object.assign(Object.assign({},defaultStyle.background),style?.background),{opacity:isOpen?"0.5":(_b=defaultStyle.background)===null||_b===void 0?void 0:_b.opacity,zIndex:isOpen?"1":(_c=defaultStyle.background)===null||_c===void 0?void 0:_c.zIndex}),onClick:onCloseCart}),(0,jsx_runtime_1.jsxs)("div",Object.assign({style:Object.assign(Object.assign(Object.assign({},defaultStyle.container),style?.container),{right:isOpen?"0":(_d=defaultStyle.container)===null||_d===void 0?void 0:_d.right,zIndex:isOpen?"100":(_e=defaultStyle.container)===null||_e===void 0?void 0:_e.zIndex})},props,{children:[(0,jsx_runtime_1.jsx)("div",{style:Object.assign(Object.assign({},defaultStyle.iconContainer),style?.iconContainer),children:(0,jsx_runtime_1.jsx)("svg",{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:1.5,stroke:"currentColor",style:Object.assign(Object.assign({},defaultStyle.icon),style?.icon),onClick:onCloseCart,children:(0,jsx_runtime_1.jsx)("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M6 18L18 6M6 6l12 12"})})}),(0,jsx_runtime_1.jsx)("iframe",{title:"Cart",ref,style:Object.assign(Object.assign({},defaultStyle.cart),style?.cart),src,width:"100%",height:"100%"})]}))]}):(0,jsx_runtime_1.jsx)("iframe",{title:"Cart",ref,style:Object.assign(Object.assign({},defaultStyle.cart),style?.cart),src,width:"100%",height:"100%"})}exports.HostedCart=HostedCart;
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.HostedCart=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),OrderStorageContext_1=tslib_1.__importDefault(require("../../context/OrderStorageContext")),getApplicationLink_1=require("../../utils/getApplicationLink"),getDomain_1=require("../../utils/getDomain"),useCustomContext_1=tslib_1.__importDefault(require("../../utils/hooks/useCustomContext")),react_1=require("react"),iframe_resizer_1=require("iframe-resizer"),events_1=require("../../utils/events"),defaultIframeStyle={width:"1px",minWidth:"100%",minHeight:"100%",border:"none"},defaultContainerStyle={position:"fixed",top:"0",right:"-25rem",height:"100%",width:"23rem",transition:"right 0.5s ease-in-out",zIndex:"0",overflow:"auto"},defaultBackgroundStyle={opacity:"0",position:"fixed",top:"0",left:"0",height:"100%",width:"100vw",transition:"opacity 0.5s ease-in-out",zIndex:"-10",backgroundColor:"black"},defaultIconStyle={width:"1.25rem",height:"1.25rem"},defaultIconContainer={textAlign:"left",paddingLeft:"20px",paddingTop:"20px",background:"#ffffff",color:"#686E6E"},defaultStyle={cart:defaultIframeStyle,container:defaultContainerStyle,background:defaultBackgroundStyle,icon:defaultIconStyle,iconContainer:defaultIconContainer};function HostedCart(_a){var _b,_c,_d,_e,{type,openAdd=!1,style,open=!1,handleOpen,customDomain}=_a,props=tslib_1.__rest(_a,["type","openAdd","style","open","handleOpen","customDomain"]);const[isOpen,setOpen]=(0,react_1.useState)(!1),ref=(0,react_1.useRef)(null),{accessToken,endpoint}=(0,useCustomContext_1.default)({context:CommerceLayerContext_1.default,contextComponentName:"CommerceLayer",currentComponentName:"HostedCart",key:"accessToken"}),[src,setSrc]=(0,react_1.useState)();if(accessToken==null||endpoint==null)return null;const{order,createOrder}=(0,react_1.useContext)(OrderContext_1.default),{persistKey}=(0,react_1.useContext)(OrderStorageContext_1.default),{domain,slug}=(0,getDomain_1.getDomain)(endpoint);function setOrder(openCart){var _a2;return tslib_1.__awaiter(this,void 0,void 0,function*(){const orderId=(_a2=localStorage.getItem(persistKey))!==null&&_a2!==void 0?_a2:yield createOrder({});orderId!=null&&accessToken&&(setSrc((0,getApplicationLink_1.getApplicationLink)({slug,orderId,accessToken,domain,applicationType:"cart",customDomain})),openCart&&setTimeout(()=>{handleOpen!=null?handleOpen():setOpen(!0)},300))})}function onMessage(data){var _a2;switch(data.message.type){case"update":break;case"close":type==="mini"&&(handleOpen!=null?handleOpen():setOpen(!1));break;case"blur":type==="mini"&&isOpen&&((_a2=ref.current)===null||_a2===void 0||_a2.focus());break}}(0,react_1.useEffect)(()=>{var _a2;const orderId=localStorage.getItem(persistKey);let ignore=!1;return open!=null&&open!==isOpen&&setOpen(open),openAdd&&type==="mini"&&(0,events_1.subscribe)("open-cart",()=>{window.document.body.style.overflow="hidden",src==null&&order?.id==null&&orderId==null?setOrder(!0):(src!=null&&ref.current!=null&&(ref.current.src=src),setTimeout(()=>{handleOpen!=null?handleOpen():setOpen(!0)},300))}),src==null&&order?.id==null&&orderId==null&&accessToken!=null&&!ignore&&isOpen?setOrder():src==null&&(order?.id!=null||orderId!=null)&&accessToken&&setSrc((0,getApplicationLink_1.getApplicationLink)({slug,orderId:(_a2=order?.id)!==null&&_a2!==void 0?_a2:orderId,accessToken,domain,applicationType:"cart"})),src!=null&&ref.current!=null&&(ref.current.src=src),()=>{ignore=!0,openAdd&&type==="mini"&&(0,events_1.unsubscribe)("open-cart",()=>{})}},[src,open,order?.id,accessToken]),(0,react_1.useEffect)(()=>{ref.current!=null&&(0,iframe_resizer_1.iframeResizer)({checkOrigin:!1,bodyPadding:"20px",onMessage},ref.current)},[ref.current!=null]);function onCloseCart(){window.document.body.style.removeProperty("overflow"),handleOpen!=null?handleOpen():setOpen(!1)}return src==null?null:type==="mini"?(0,jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment,{children:[(0,jsx_runtime_1.jsx)("div",{style:Object.assign(Object.assign(Object.assign({},defaultStyle.background),style?.background),{opacity:isOpen?"0.5":(_b=defaultStyle.background)===null||_b===void 0?void 0:_b.opacity,zIndex:isOpen?"1":(_c=defaultStyle.background)===null||_c===void 0?void 0:_c.zIndex}),onClick:onCloseCart}),(0,jsx_runtime_1.jsxs)("div",Object.assign({style:Object.assign(Object.assign(Object.assign({},defaultStyle.container),style?.container),{right:isOpen?"0":(_d=defaultStyle.container)===null||_d===void 0?void 0:_d.right,zIndex:isOpen?"100":(_e=defaultStyle.container)===null||_e===void 0?void 0:_e.zIndex})},props,{children:[(0,jsx_runtime_1.jsx)("div",{style:Object.assign(Object.assign({},defaultStyle.iconContainer),style?.iconContainer),children:(0,jsx_runtime_1.jsx)("svg",{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:1.5,stroke:"currentColor",style:Object.assign(Object.assign({},defaultStyle.icon),style?.icon),onClick:onCloseCart,children:(0,jsx_runtime_1.jsx)("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M6 18L18 6M6 6l12 12"})})}),(0,jsx_runtime_1.jsx)("iframe",{title:"Cart",ref,style:Object.assign(Object.assign({},defaultStyle.cart),style?.cart),src,width:"100%",height:"100%"})]}))]}):(0,jsx_runtime_1.jsx)("iframe",{title:"Cart",ref,style:Object.assign(Object.assign({},defaultStyle.cart),style?.cart),src,width:"100%",height:"100%"})}exports.HostedCart=HostedCart;
|
|
@@ -1,10 +1,39 @@
|
|
|
1
1
|
import { type LoaderType } from '../../typings/index';
|
|
2
2
|
interface Props {
|
|
3
|
+
/**
|
|
4
|
+
* Any valid JSX.Element(s).
|
|
5
|
+
* A single `<Price>` component or a list of them is expected to render the prices.
|
|
6
|
+
*/
|
|
3
7
|
children: JSX.Element | JSX.Element[];
|
|
8
|
+
/**
|
|
9
|
+
* SDK query filter to fetch the prices when multiple prices are requested.
|
|
10
|
+
*/
|
|
4
11
|
filters?: object;
|
|
12
|
+
/**
|
|
13
|
+
* Loader component or string to be rendered while the prices are being fetched.
|
|
14
|
+
* @default 'Loading...'
|
|
15
|
+
*/
|
|
5
16
|
loader?: LoaderType;
|
|
17
|
+
/**
|
|
18
|
+
* Prices per page to be fetched
|
|
19
|
+
*/
|
|
6
20
|
perPage?: number;
|
|
21
|
+
/**
|
|
22
|
+
* SKU code to fetch the prices for. If not provided, the `sku_code` will be retrieved from the `<Price>` component(s) nested as children.
|
|
23
|
+
*/
|
|
7
24
|
skuCode?: string;
|
|
8
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Main container for the Prices components. It stores the prices context.
|
|
28
|
+
*
|
|
29
|
+
* It can be used to fetch the prices for a specific `sku_code` passed as prop
|
|
30
|
+
* or for the `sku_code` retrieved from all `<Price>` components nested as children.
|
|
31
|
+
* <span title='Requirements' type='warning'>
|
|
32
|
+
* Must be a child of the `<CommerceLayer>` component.
|
|
33
|
+
* </span>
|
|
34
|
+
* <span title='Children' type='info'>
|
|
35
|
+
* `<Price>`
|
|
36
|
+
* </span>
|
|
37
|
+
*/
|
|
9
38
|
export declare function PricesContainer(props: Props): JSX.Element;
|
|
10
39
|
export default PricesContainer;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type createOrder, type OrderState, type addToCart } from '../reducers/OrderReducer';
|
|
2
2
|
type TAddToCartParams = Omit<Parameters<typeof addToCart>[number], 'buyNowMode' | 'state' | 'dispatch' | 'setLocalOrder' | 'errors' | 'checkoutUrl' | 'persistKey' | 'config'>;
|
|
3
3
|
type TCreateCartParams = Pick<Parameters<typeof createOrder>[number], 'orderAttributes' | 'orderMetadata'>;
|
|
4
|
-
interface TReturnOrder extends Omit<OrderState, 'loading' | 'include' | 'includeLoaded' | 'withoutIncludes'> {
|
|
4
|
+
interface TReturnOrder extends Omit<OrderState, 'loading' | 'include' | 'includeLoaded' | 'withoutIncludes' | 'orderId'> {
|
|
5
5
|
reloadOrder: () => Promise<OrderState['order']>;
|
|
6
6
|
addToCart: (params: TAddToCartParams) => ReturnType<typeof addToCart>;
|
|
7
7
|
createOrder: (params: TCreateCartParams) => Promise<string | undefined>;
|
|
@@ -25,7 +25,8 @@ interface TArgs {
|
|
|
25
25
|
accessToken: string;
|
|
26
26
|
slug: string;
|
|
27
27
|
domain: string;
|
|
28
|
+
customDomain?: string;
|
|
28
29
|
}
|
|
29
30
|
type Props = ApplicationTypeProps & TArgs;
|
|
30
|
-
export declare function getApplicationLink({ orderId, accessToken, slug, domain, applicationType, modeType, clientId, scope, returnUrl }: Props): string;
|
|
31
|
+
export declare function getApplicationLink({ orderId, accessToken, slug, domain, applicationType, modeType, clientId, scope, returnUrl, customDomain }: Props): string;
|
|
31
32
|
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getApplicationLink=void 0;function getApplicationLink({orderId,accessToken,slug,domain,applicationType,modeType,clientId,scope,returnUrl}){var _a;const env=domain==="commercelayer.io"?"":"stg.",t=modeType==="login"?"":"signup",c=clientId?`&clientId=${clientId}`:"",s=scope?`&scope=${scope}`:"",r=returnUrl?`&returnUrl=${returnUrl}`:"",params=applicationType==="identity"?`${c}${s}${r}`:""
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getApplicationLink=void 0;function getApplicationLink({orderId,accessToken,slug,domain,applicationType,modeType,clientId,scope,returnUrl,customDomain}){var _a;const env=domain==="commercelayer.io"?"":"stg.",t=applicationType==="identity"?modeType==="login"?"":"signup":"",c=clientId?`&clientId=${clientId}`:"",s=scope?`&scope=${scope}`:"",r=returnUrl?`&returnUrl=${returnUrl}`:"",params=applicationType==="identity"?`${c}${s}${r}`:"",domainName=customDomain??`${slug}.${env}commercelayer.app`,application=customDomain?"":`/${applicationType.toString()}`;return`https://${domainName}${application}/${(_a=orderId??t)!==null&&_a!==void 0?_a:""}?accessToken=${accessToken}${params}`}exports.getApplicationLink=getApplicationLink;
|
|
@@ -18,6 +18,10 @@ interface Props extends Omit<JSX.IntrinsicElements['a'], 'children'> {
|
|
|
18
18
|
* The label of the link
|
|
19
19
|
*/
|
|
20
20
|
label?: string | JSX.Element;
|
|
21
|
+
/**
|
|
22
|
+
* The domain of your forked application
|
|
23
|
+
*/
|
|
24
|
+
customDomain?: string;
|
|
21
25
|
}
|
|
22
26
|
export declare function MyAccountLink(props: Props): JSX.Element;
|
|
23
27
|
export default MyAccountLink;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import Parent from"../utils/Parent";import CommerceLayerContext from"../../context/CommerceLayerContext";import{getApplicationLink}from"../../utils/getApplicationLink";import{getDomain}from"../../utils/getDomain";import jwt from"../../utils/jwt";export function MyAccountLink(props){const{label="Go to my account",children,...p}=props,{accessToken,endpoint}=useContext(CommerceLayerContext);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyAccountLink` outside of `CommerceLayer`");const{domain,slug}=getDomain(endpoint),disabled=!("owner"in jwt(accessToken)),href=getApplicationLink({slug,accessToken,applicationType:"my-account",domain}),parentProps={disabled,label,href,...p};function handleClick(e){disabled&&e.preventDefault()}return children?_jsx(Parent,{...parentProps,children}):_jsx("a",{"aria-disabled":disabled,onClick:handleClick,href,...p,children:label})}export default MyAccountLink;
|
|
2
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import Parent from"../utils/Parent";import CommerceLayerContext from"../../context/CommerceLayerContext";import{getApplicationLink}from"../../utils/getApplicationLink";import{getDomain}from"../../utils/getDomain";import jwt from"../../utils/jwt";export function MyAccountLink(props){const{label="Go to my account",children,customDomain,...p}=props,{accessToken,endpoint}=useContext(CommerceLayerContext);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyAccountLink` outside of `CommerceLayer`");const{domain,slug}=getDomain(endpoint),disabled=!("owner"in jwt(accessToken)),href=getApplicationLink({slug,accessToken,applicationType:"my-account",domain,customDomain}),parentProps={disabled,label,href,...p};function handleClick(e){disabled&&e.preventDefault()}return children?_jsx(Parent,{...parentProps,children}):_jsx("a",{"aria-disabled":disabled,onClick:handleClick,href,...p,children:label})}export default MyAccountLink;
|
|
@@ -30,6 +30,10 @@ interface Props extends Omit<JSX.IntrinsicElements['a'], 'children'> {
|
|
|
30
30
|
* The return url to redirect the user after the login/signup
|
|
31
31
|
*/
|
|
32
32
|
returnUrl?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The domain of your forked application
|
|
35
|
+
*/
|
|
36
|
+
customDomain?: string;
|
|
33
37
|
}
|
|
34
38
|
export declare function MyIdentityLink(props: Props): JSX.Element;
|
|
35
39
|
export default MyIdentityLink;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import Parent from"../utils/Parent";import CommerceLayerContext from"../../context/CommerceLayerContext";import{getApplicationLink}from"../../utils/getApplicationLink";import{getDomain}from"../../utils/getDomain";export function MyIdentityLink(props){const{label,children,type,clientId,scope,returnUrl,...p}=props,{accessToken,endpoint}=useContext(CommerceLayerContext);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyIdentityLink` outside of `CommerceLayer`");const{domain,slug}=getDomain(endpoint),href=getApplicationLink({slug,accessToken,applicationType:"identity",domain,modeType:type,clientId,scope,returnUrl:returnUrl??window.location.href}),parentProps={label,href,clientId,scope,...p};return children?_jsx(Parent,{...parentProps,children}):_jsx("a",{href,...p,children:label})}export default MyIdentityLink;
|
|
2
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import Parent from"../utils/Parent";import CommerceLayerContext from"../../context/CommerceLayerContext";import{getApplicationLink}from"../../utils/getApplicationLink";import{getDomain}from"../../utils/getDomain";export function MyIdentityLink(props){const{label,children,type,clientId,scope,returnUrl,customDomain,...p}=props,{accessToken,endpoint}=useContext(CommerceLayerContext);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyIdentityLink` outside of `CommerceLayer`");const{domain,slug}=getDomain(endpoint),href=getApplicationLink({slug,accessToken,applicationType:"identity",domain,modeType:type,clientId,scope,returnUrl:returnUrl??window.location.href,customDomain}),parentProps={label,href,clientId,scope,...p};return children?_jsx(Parent,{...parentProps,children}):_jsx("a",{href,...p,children:label})}export default MyIdentityLink;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { type ChildrenFunction } from '../../typings/index';
|
|
3
|
+
import { type LineItem } from '@commercelayer/sdk';
|
|
3
4
|
interface ChildrenProps extends Omit<Props, 'children'> {
|
|
4
5
|
quantity: number;
|
|
5
6
|
handleChange: (event: React.MouseEvent<HTMLSelectElement>) => void;
|
|
7
|
+
lineItem?: LineItem;
|
|
6
8
|
}
|
|
7
9
|
type Props = {
|
|
8
10
|
children?: ChildrenFunction<ChildrenProps>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import LineItemChildrenContext from"../../context/LineItemChildrenContext";import LineItemContext from"../../context/LineItemContext";import Parent from"../utils/Parent";export function LineItemQuantity(props){const{max=50,readonly=!1,...p}=props,{lineItem}=useContext(LineItemChildrenContext),{updateLineItem}=useContext(LineItemContext),options=[];for(let i=1;i<=max;i++)options.push(_jsx("option",{value:`${i}`,children:i},i));const handleChange=e=>{const quantity2=Number(e.target.value);updateLineItem&&lineItem&&updateLineItem(lineItem.id,quantity2)},quantity=lineItem?.quantity,parentProps={handleChange,quantity,...props};return props.children?_jsx(Parent,{...parentProps,children:props.children}):readonly?_jsx("span",{...p,children:quantity}):_jsx("select",{"data-testid":lineItem?.sku_code,title:lineItem?.name??"",value:quantity,onChange:handleChange,...p,children:options})}export default LineItemQuantity;
|
|
2
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import LineItemChildrenContext from"../../context/LineItemChildrenContext";import LineItemContext from"../../context/LineItemContext";import Parent from"../utils/Parent";export function LineItemQuantity(props){const{max=50,readonly=!1,...p}=props,{lineItem}=useContext(LineItemChildrenContext),{updateLineItem}=useContext(LineItemContext),options=[];for(let i=1;i<=max;i++)options.push(_jsx("option",{value:`${i}`,children:i},i));const handleChange=e=>{const quantity2=Number(e.target.value);updateLineItem&&lineItem&&updateLineItem(lineItem.id,quantity2)},quantity=lineItem?.quantity,parentProps={handleChange,quantity,lineItem,...props};return props.children?_jsx(Parent,{...parentProps,children:props.children}):readonly?_jsx("span",{...p,children:quantity}):_jsx("select",{"data-testid":lineItem?.sku_code,title:lineItem?.name??"",value:quantity,onChange:handleChange,...p,children:options})}export default LineItemQuantity;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { type PropsWithoutRef } from 'react';
|
|
2
2
|
import { type ChildrenFunction } from '../../typings/index';
|
|
3
|
+
import { type LineItem } from '@commercelayer/sdk';
|
|
3
4
|
interface ChildrenProps extends Omit<Props, 'children'> {
|
|
4
5
|
handleRemove: (event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
5
6
|
label?: string;
|
|
7
|
+
lineItem?: LineItem;
|
|
6
8
|
}
|
|
7
9
|
interface Props extends PropsWithoutRef<Omit<JSX.IntrinsicElements['a'], 'children'>> {
|
|
8
10
|
children?: ChildrenFunction<ChildrenProps>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import LineItemChildrenContext from"../../context/LineItemChildrenContext";import LineItemContext from"../../context/LineItemContext";import Parent from"../utils/Parent";import useCustomContext from"../../utils/hooks/useCustomContext";export function LineItemRemoveLink(props){const{label="Remove",onClick}=props,{lineItem}=useCustomContext({context:LineItemChildrenContext,contextComponentName:"LineItem",currentComponentName:"LineItemRemoveLink",key:"lineItem"}),{deleteLineItem}=useContext(LineItemContext),handleRemove=e=>{e.preventDefault(),deleteLineItem!=null&&lineItem!=null&&deleteLineItem(lineItem.id),onClick?.(e)},parentProps={handleRemove,...props};return props.children?_jsx(Parent,{...parentProps,children:props.children}):_jsx("a",{"data-testid":`line-item-remove-link-${lineItem?.sku_code??""}`,...props,href:"#",onClick:handleRemove,children:label})}export default LineItemRemoveLink;
|
|
2
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import LineItemChildrenContext from"../../context/LineItemChildrenContext";import LineItemContext from"../../context/LineItemContext";import Parent from"../utils/Parent";import useCustomContext from"../../utils/hooks/useCustomContext";export function LineItemRemoveLink(props){const{label="Remove",onClick}=props,{lineItem}=useCustomContext({context:LineItemChildrenContext,contextComponentName:"LineItem",currentComponentName:"LineItemRemoveLink",key:"lineItem"}),{deleteLineItem}=useContext(LineItemContext),handleRemove=e=>{e.preventDefault(),deleteLineItem!=null&&lineItem!=null&&deleteLineItem(lineItem.id),onClick?.(e)},parentProps={handleRemove,lineItem,...props};return props.children?_jsx(Parent,{...parentProps,children:props.children}):_jsx("a",{"data-testid":`line-item-remove-link-${lineItem?.sku_code??""}`,...props,href:"#",onClick:handleRemove,children:label})}export default LineItemRemoveLink;
|
|
@@ -30,6 +30,10 @@ interface Props extends Omit<JSX.IntrinsicElements['a'], 'children'> {
|
|
|
30
30
|
* The type of the cart. Defaults to undefined. If set to 'mini', the cart will open in a modal.
|
|
31
31
|
*/
|
|
32
32
|
type?: 'mini';
|
|
33
|
+
/**
|
|
34
|
+
* The domain of your forked application
|
|
35
|
+
*/
|
|
36
|
+
customDomain?: string;
|
|
33
37
|
}
|
|
34
38
|
export declare function CartLink(props: Props): JSX.Element | null;
|
|
35
39
|
export default CartLink;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import OrderContext from"../../context/OrderContext";import Parent from"../utils/Parent";import CommerceLayerContext from"../../context/CommerceLayerContext";import{getApplicationLink}from"../../utils/getApplicationLink";import{getDomain}from"../../utils/getDomain";import{publish}from"../../utils/events";export function CartLink(props){const{label,children,type,...p}=props,{order,createOrder}=useContext(OrderContext),{accessToken,endpoint}=useContext(CommerceLayerContext);if(accessToken==null)throw new Error("Cannot use `CartLink` outside of `CommerceLayer`");if(endpoint==null)throw new Error("Cannot use `CartLink` outside of `CommerceLayer`");const{domain,slug}=getDomain(endpoint),href=slug&&order?.id?getApplicationLink({slug,orderId:order?.id,accessToken,domain,applicationType:"cart"}):"",handleClick=async event=>{if(event.preventDefault(),type!=="mini")if(order?.id)location.href=href;else{const orderId=await createOrder({});slug&&(location.href=getApplicationLink({slug,orderId,accessToken,domain,applicationType:"cart"}))}else publish("open-cart")},parentProps={handleClick,label,href,orderId:order?.id,accessToken,...p};return accessToken?children?_jsx(Parent,{...parentProps,children}):_jsx("a",{href,...p,onClick:e=>{handleClick(e)},children:label}):null}export default CartLink;
|
|
2
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import OrderContext from"../../context/OrderContext";import Parent from"../utils/Parent";import CommerceLayerContext from"../../context/CommerceLayerContext";import{getApplicationLink}from"../../utils/getApplicationLink";import{getDomain}from"../../utils/getDomain";import{publish}from"../../utils/events";export function CartLink(props){const{label,children,type,customDomain,...p}=props,{order,createOrder}=useContext(OrderContext),{accessToken,endpoint}=useContext(CommerceLayerContext);if(accessToken==null)throw new Error("Cannot use `CartLink` outside of `CommerceLayer`");if(endpoint==null)throw new Error("Cannot use `CartLink` outside of `CommerceLayer`");const{domain,slug}=getDomain(endpoint),href=slug&&order?.id?getApplicationLink({slug,orderId:order?.id,accessToken,domain,applicationType:"cart",customDomain}):"",handleClick=async event=>{if(event.preventDefault(),type!=="mini")if(order?.id)location.href=href;else{const orderId=await createOrder({});slug&&(location.href=getApplicationLink({slug,orderId,accessToken,domain,applicationType:"cart",customDomain}))}else publish("open-cart")},parentProps={handleClick,label,href,orderId:order?.id,accessToken,...p};return accessToken?children?_jsx(Parent,{...parentProps,children}):_jsx("a",{href,...p,onClick:e=>{handleClick(e)},children:label}):null}export default CartLink;
|
|
@@ -27,6 +27,10 @@ interface Props extends Omit<JSX.IntrinsicElements['div'], 'children' | 'style'>
|
|
|
27
27
|
* A function that will be called when the cart is open and the background is clicked.
|
|
28
28
|
*/
|
|
29
29
|
handleOpen?: () => void;
|
|
30
|
+
/**
|
|
31
|
+
* The domain of your forked application.
|
|
32
|
+
*/
|
|
33
|
+
customDomain?: string;
|
|
30
34
|
}
|
|
31
|
-
export declare function HostedCart({ type, openAdd, style, open, handleOpen, ...props }: Props): JSX.Element | null;
|
|
35
|
+
export declare function HostedCart({ type, openAdd, style, open, handleOpen, customDomain, ...props }: Props): JSX.Element | null;
|
|
32
36
|
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx,jsxs as _jsxs,Fragment as _Fragment}from"react/jsx-runtime";import CommerceLayerContext from"../../context/CommerceLayerContext";import OrderContext from"../../context/OrderContext";import OrderStorageContext from"../../context/OrderStorageContext";import{getApplicationLink}from"../../utils/getApplicationLink";import{getDomain}from"../../utils/getDomain";import useCustomContext from"../../utils/hooks/useCustomContext";import{useContext,useEffect,useState,useRef}from"react";import{iframeResizer}from"iframe-resizer";import{subscribe,unsubscribe}from"../../utils/events";const defaultIframeStyle={width:"1px",minWidth:"100%",minHeight:"100%",border:"none"},defaultContainerStyle={position:"fixed",top:"0",right:"-25rem",height:"100%",width:"23rem",transition:"right 0.5s ease-in-out",zIndex:"0",overflow:"auto"},defaultBackgroundStyle={opacity:"0",position:"fixed",top:"0",left:"0",height:"100%",width:"100vw",transition:"opacity 0.5s ease-in-out",zIndex:"-10",backgroundColor:"black"},defaultIconStyle={width:"1.25rem",height:"1.25rem"},defaultIconContainer={textAlign:"left",paddingLeft:"20px",paddingTop:"20px",background:"#ffffff",color:"#686E6E"},defaultStyle={cart:defaultIframeStyle,container:defaultContainerStyle,background:defaultBackgroundStyle,icon:defaultIconStyle,iconContainer:defaultIconContainer};export function HostedCart({type,openAdd=!1,style,open=!1,handleOpen,...props}){const[isOpen,setOpen]=useState(!1),ref=useRef(null),{accessToken,endpoint}=useCustomContext({context:CommerceLayerContext,contextComponentName:"CommerceLayer",currentComponentName:"HostedCart",key:"accessToken"}),[src,setSrc]=useState();if(accessToken==null||endpoint==null)return null;const{order,createOrder}=useContext(OrderContext),{persistKey}=useContext(OrderStorageContext),{domain,slug}=getDomain(endpoint);async function setOrder(openCart){const orderId=localStorage.getItem(persistKey)??await createOrder({});orderId!=null&&accessToken&&(setSrc(getApplicationLink({slug,orderId,accessToken,domain,applicationType:"cart"})),openCart&&setTimeout(()=>{handleOpen!=null?handleOpen():setOpen(!0)},300))}function onMessage(data){switch(data.message.type){case"update":break;case"close":type==="mini"&&(handleOpen!=null?handleOpen():setOpen(!1));break;case"blur":type==="mini"&&isOpen&&ref.current?.focus();break}}useEffect(()=>{const orderId=localStorage.getItem(persistKey);let ignore=!1;return open!=null&&open!==isOpen&&setOpen(open),openAdd&&type==="mini"&&subscribe("open-cart",()=>{window.document.body.style.overflow="hidden",src==null&&order?.id==null&&orderId==null?setOrder(!0):(src!=null&&ref.current!=null&&(ref.current.src=src),setTimeout(()=>{handleOpen!=null?handleOpen():setOpen(!0)},300))}),src==null&&order?.id==null&&orderId==null&&accessToken!=null&&!ignore&&isOpen?setOrder():src==null&&(order?.id!=null||orderId!=null)&&accessToken&&setSrc(getApplicationLink({slug,orderId:order?.id??orderId,accessToken,domain,applicationType:"cart"})),src!=null&&ref.current!=null&&(ref.current.src=src),()=>{ignore=!0,openAdd&&type==="mini"&&unsubscribe("open-cart",()=>{})}},[src,open,order?.id,accessToken]),useEffect(()=>{ref.current!=null&&iframeResizer({checkOrigin:!1,bodyPadding:"20px",onMessage},ref.current)},[ref.current!=null]);function onCloseCart(){window.document.body.style.removeProperty("overflow"),handleOpen!=null?handleOpen():setOpen(!1)}return src==null?null:type==="mini"?_jsxs(_Fragment,{children:[_jsx("div",{style:{...defaultStyle.background,...style?.background,opacity:isOpen?"0.5":defaultStyle.background?.opacity,zIndex:isOpen?"1":defaultStyle.background?.zIndex},onClick:onCloseCart}),_jsxs("div",{style:{...defaultStyle.container,...style?.container,right:isOpen?"0":defaultStyle.container?.right,zIndex:isOpen?"100":defaultStyle.container?.zIndex},...props,children:[_jsx("div",{style:{...defaultStyle.iconContainer,...style?.iconContainer},children:_jsx("svg",{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:1.5,stroke:"currentColor",style:{...defaultStyle.icon,...style?.icon},onClick:onCloseCart,children:_jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M6 18L18 6M6 6l12 12"})})}),_jsx("iframe",{title:"Cart",ref,style:{...defaultStyle.cart,...style?.cart},src,width:"100%",height:"100%"})]})]}):_jsx("iframe",{title:"Cart",ref,style:{...defaultStyle.cart,...style?.cart},src,width:"100%",height:"100%"})}
|
|
2
|
+
import{jsx as _jsx,jsxs as _jsxs,Fragment as _Fragment}from"react/jsx-runtime";import CommerceLayerContext from"../../context/CommerceLayerContext";import OrderContext from"../../context/OrderContext";import OrderStorageContext from"../../context/OrderStorageContext";import{getApplicationLink}from"../../utils/getApplicationLink";import{getDomain}from"../../utils/getDomain";import useCustomContext from"../../utils/hooks/useCustomContext";import{useContext,useEffect,useState,useRef}from"react";import{iframeResizer}from"iframe-resizer";import{subscribe,unsubscribe}from"../../utils/events";const defaultIframeStyle={width:"1px",minWidth:"100%",minHeight:"100%",border:"none"},defaultContainerStyle={position:"fixed",top:"0",right:"-25rem",height:"100%",width:"23rem",transition:"right 0.5s ease-in-out",zIndex:"0",overflow:"auto"},defaultBackgroundStyle={opacity:"0",position:"fixed",top:"0",left:"0",height:"100%",width:"100vw",transition:"opacity 0.5s ease-in-out",zIndex:"-10",backgroundColor:"black"},defaultIconStyle={width:"1.25rem",height:"1.25rem"},defaultIconContainer={textAlign:"left",paddingLeft:"20px",paddingTop:"20px",background:"#ffffff",color:"#686E6E"},defaultStyle={cart:defaultIframeStyle,container:defaultContainerStyle,background:defaultBackgroundStyle,icon:defaultIconStyle,iconContainer:defaultIconContainer};export function HostedCart({type,openAdd=!1,style,open=!1,handleOpen,customDomain,...props}){const[isOpen,setOpen]=useState(!1),ref=useRef(null),{accessToken,endpoint}=useCustomContext({context:CommerceLayerContext,contextComponentName:"CommerceLayer",currentComponentName:"HostedCart",key:"accessToken"}),[src,setSrc]=useState();if(accessToken==null||endpoint==null)return null;const{order,createOrder}=useContext(OrderContext),{persistKey}=useContext(OrderStorageContext),{domain,slug}=getDomain(endpoint);async function setOrder(openCart){const orderId=localStorage.getItem(persistKey)??await createOrder({});orderId!=null&&accessToken&&(setSrc(getApplicationLink({slug,orderId,accessToken,domain,applicationType:"cart",customDomain})),openCart&&setTimeout(()=>{handleOpen!=null?handleOpen():setOpen(!0)},300))}function onMessage(data){switch(data.message.type){case"update":break;case"close":type==="mini"&&(handleOpen!=null?handleOpen():setOpen(!1));break;case"blur":type==="mini"&&isOpen&&ref.current?.focus();break}}useEffect(()=>{const orderId=localStorage.getItem(persistKey);let ignore=!1;return open!=null&&open!==isOpen&&setOpen(open),openAdd&&type==="mini"&&subscribe("open-cart",()=>{window.document.body.style.overflow="hidden",src==null&&order?.id==null&&orderId==null?setOrder(!0):(src!=null&&ref.current!=null&&(ref.current.src=src),setTimeout(()=>{handleOpen!=null?handleOpen():setOpen(!0)},300))}),src==null&&order?.id==null&&orderId==null&&accessToken!=null&&!ignore&&isOpen?setOrder():src==null&&(order?.id!=null||orderId!=null)&&accessToken&&setSrc(getApplicationLink({slug,orderId:order?.id??orderId,accessToken,domain,applicationType:"cart"})),src!=null&&ref.current!=null&&(ref.current.src=src),()=>{ignore=!0,openAdd&&type==="mini"&&unsubscribe("open-cart",()=>{})}},[src,open,order?.id,accessToken]),useEffect(()=>{ref.current!=null&&iframeResizer({checkOrigin:!1,bodyPadding:"20px",onMessage},ref.current)},[ref.current!=null]);function onCloseCart(){window.document.body.style.removeProperty("overflow"),handleOpen!=null?handleOpen():setOpen(!1)}return src==null?null:type==="mini"?_jsxs(_Fragment,{children:[_jsx("div",{style:{...defaultStyle.background,...style?.background,opacity:isOpen?"0.5":defaultStyle.background?.opacity,zIndex:isOpen?"1":defaultStyle.background?.zIndex},onClick:onCloseCart}),_jsxs("div",{style:{...defaultStyle.container,...style?.container,right:isOpen?"0":defaultStyle.container?.right,zIndex:isOpen?"100":defaultStyle.container?.zIndex},...props,children:[_jsx("div",{style:{...defaultStyle.iconContainer,...style?.iconContainer},children:_jsx("svg",{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:1.5,stroke:"currentColor",style:{...defaultStyle.icon,...style?.icon},onClick:onCloseCart,children:_jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M6 18L18 6M6 6l12 12"})})}),_jsx("iframe",{title:"Cart",ref,style:{...defaultStyle.cart,...style?.cart},src,width:"100%",height:"100%"})]})]}):_jsx("iframe",{title:"Cart",ref,style:{...defaultStyle.cart,...style?.cart},src,width:"100%",height:"100%"})}
|
|
@@ -1,10 +1,39 @@
|
|
|
1
1
|
import { type LoaderType } from '../../typings/index';
|
|
2
2
|
interface Props {
|
|
3
|
+
/**
|
|
4
|
+
* Any valid JSX.Element(s).
|
|
5
|
+
* A single `<Price>` component or a list of them is expected to render the prices.
|
|
6
|
+
*/
|
|
3
7
|
children: JSX.Element | JSX.Element[];
|
|
8
|
+
/**
|
|
9
|
+
* SDK query filter to fetch the prices when multiple prices are requested.
|
|
10
|
+
*/
|
|
4
11
|
filters?: object;
|
|
12
|
+
/**
|
|
13
|
+
* Loader component or string to be rendered while the prices are being fetched.
|
|
14
|
+
* @default 'Loading...'
|
|
15
|
+
*/
|
|
5
16
|
loader?: LoaderType;
|
|
17
|
+
/**
|
|
18
|
+
* Prices per page to be fetched
|
|
19
|
+
*/
|
|
6
20
|
perPage?: number;
|
|
21
|
+
/**
|
|
22
|
+
* SKU code to fetch the prices for. If not provided, the `sku_code` will be retrieved from the `<Price>` component(s) nested as children.
|
|
23
|
+
*/
|
|
7
24
|
skuCode?: string;
|
|
8
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Main container for the Prices components. It stores the prices context.
|
|
28
|
+
*
|
|
29
|
+
* It can be used to fetch the prices for a specific `sku_code` passed as prop
|
|
30
|
+
* or for the `sku_code` retrieved from all `<Price>` components nested as children.
|
|
31
|
+
* <span title='Requirements' type='warning'>
|
|
32
|
+
* Must be a child of the `<CommerceLayer>` component.
|
|
33
|
+
* </span>
|
|
34
|
+
* <span title='Children' type='info'>
|
|
35
|
+
* `<Price>`
|
|
36
|
+
* </span>
|
|
37
|
+
*/
|
|
9
38
|
export declare function PricesContainer(props: Props): JSX.Element;
|
|
10
39
|
export default PricesContainer;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type createOrder, type OrderState, type addToCart } from '../reducers/OrderReducer';
|
|
2
2
|
type TAddToCartParams = Omit<Parameters<typeof addToCart>[number], 'buyNowMode' | 'state' | 'dispatch' | 'setLocalOrder' | 'errors' | 'checkoutUrl' | 'persistKey' | 'config'>;
|
|
3
3
|
type TCreateCartParams = Pick<Parameters<typeof createOrder>[number], 'orderAttributes' | 'orderMetadata'>;
|
|
4
|
-
interface TReturnOrder extends Omit<OrderState, 'loading' | 'include' | 'includeLoaded' | 'withoutIncludes'> {
|
|
4
|
+
interface TReturnOrder extends Omit<OrderState, 'loading' | 'include' | 'includeLoaded' | 'withoutIncludes' | 'orderId'> {
|
|
5
5
|
reloadOrder: () => Promise<OrderState['order']>;
|
|
6
6
|
addToCart: (params: TAddToCartParams) => ReturnType<typeof addToCart>;
|
|
7
7
|
createOrder: (params: TCreateCartParams) => Promise<string | undefined>;
|
|
@@ -25,7 +25,8 @@ interface TArgs {
|
|
|
25
25
|
accessToken: string;
|
|
26
26
|
slug: string;
|
|
27
27
|
domain: string;
|
|
28
|
+
customDomain?: string;
|
|
28
29
|
}
|
|
29
30
|
type Props = ApplicationTypeProps & TArgs;
|
|
30
|
-
export declare function getApplicationLink({ orderId, accessToken, slug, domain, applicationType, modeType, clientId, scope, returnUrl }: Props): string;
|
|
31
|
+
export declare function getApplicationLink({ orderId, accessToken, slug, domain, applicationType, modeType, clientId, scope, returnUrl, customDomain }: Props): string;
|
|
31
32
|
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
export function getApplicationLink({orderId,accessToken,slug,domain,applicationType,modeType,clientId,scope,returnUrl}){const env=domain==="commercelayer.io"?"":"stg.",t=modeType==="login"?"":"signup",c=clientId?`&clientId=${clientId}`:"",s=scope?`&scope=${scope}`:"",r=returnUrl?`&returnUrl=${returnUrl}`:"",params=applicationType==="identity"?`${c}${s}${r}`:""
|
|
2
|
+
export function getApplicationLink({orderId,accessToken,slug,domain,applicationType,modeType,clientId,scope,returnUrl,customDomain}){const env=domain==="commercelayer.io"?"":"stg.",t=applicationType==="identity"?modeType==="login"?"":"signup":"",c=clientId?`&clientId=${clientId}`:"",s=scope?`&scope=${scope}`:"",r=returnUrl?`&returnUrl=${returnUrl}`:"",params=applicationType==="identity"?`${c}${s}${r}`:"",domainName=customDomain??`${slug}.${env}commercelayer.app`,application=customDomain?"":`/${applicationType.toString()}`;return`https://${domainName}${application}/${orderId??t??""}?accessToken=${accessToken}${params}`}
|