@commercelayer/react-components 4.2.1-beta.0 → 4.2.1-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/components/addresses/SaveAddressesButton.d.ts +7 -2
- package/lib/cjs/components/addresses/SaveAddressesButton.js +1 -1
- package/lib/cjs/components/gift_cards/GiftCardOrCouponForm.d.ts +6 -4
- package/lib/cjs/components/gift_cards/GiftCardOrCouponForm.js +1 -1
- package/lib/cjs/components/gift_cards/GiftCardOrCouponRemoveButton.d.ts +2 -0
- package/lib/cjs/components/gift_cards/GiftCardOrCouponRemoveButton.js +1 -1
- package/lib/cjs/components/orders/OrderContainer.js +1 -1
- package/lib/cjs/components/orders/PlaceOrderButton.d.ts +4 -0
- package/lib/cjs/components/payment_methods/PaymentMethod.d.ts +8 -4
- package/lib/cjs/components/payment_methods/PaymentMethod.js +1 -1
- package/lib/cjs/components/payment_methods/PaymentMethodRadioButton.d.ts +6 -2
- package/lib/cjs/components/payment_methods/PaymentMethodRadioButton.js +1 -1
- package/lib/cjs/components/shipments/Shipment.js +1 -1
- package/lib/cjs/components/shipping_methods/ShippingMethodRadioButton.d.ts +9 -5
- package/lib/cjs/components/shipping_methods/ShippingMethodRadioButton.js +1 -1
- package/lib/cjs/components/utils/PaymentCardsTemplate.d.ts +2 -3
- package/lib/cjs/context/AddressContext.d.ts +2 -3
- package/lib/cjs/context/AddressContext.js +1 -1
- package/lib/cjs/context/OrderContext.d.ts +5 -5
- package/lib/cjs/context/PaymentMethodContext.d.ts +2 -2
- package/lib/cjs/context/PlaceOrderContext.d.ts +4 -10
- package/lib/cjs/context/ShipmentContext.d.ts +2 -3
- package/lib/cjs/context/ShipmentContext.js +1 -1
- package/lib/cjs/index.d.ts +0 -2
- package/lib/cjs/reducers/AddressReducer.d.ts +11 -7
- package/lib/cjs/reducers/AddressReducer.js +1 -1
- package/lib/cjs/reducers/OrderReducer.d.ts +9 -6
- package/lib/cjs/reducers/OrderReducer.js +1 -1
- package/lib/cjs/reducers/PaymentMethodReducer.d.ts +6 -3
- package/lib/cjs/reducers/PaymentMethodReducer.js +1 -1
- package/lib/cjs/reducers/PlaceOrderReducer.d.ts +16 -18
- package/lib/cjs/reducers/PlaceOrderReducer.js +1 -1
- package/lib/cjs/reducers/ShipmentReducer.d.ts +6 -3
- package/lib/cjs/reducers/ShipmentReducer.js +1 -1
- package/lib/esm/components/addresses/SaveAddressesButton.d.ts +7 -2
- package/lib/esm/components/addresses/SaveAddressesButton.js +1 -1
- package/lib/esm/components/gift_cards/GiftCardOrCouponForm.d.ts +6 -4
- package/lib/esm/components/gift_cards/GiftCardOrCouponForm.js +1 -1
- package/lib/esm/components/gift_cards/GiftCardOrCouponRemoveButton.d.ts +2 -0
- package/lib/esm/components/gift_cards/GiftCardOrCouponRemoveButton.js +1 -1
- package/lib/esm/components/orders/OrderContainer.js +1 -1
- package/lib/esm/components/orders/PlaceOrderButton.d.ts +4 -0
- package/lib/esm/components/payment_methods/PaymentMethod.d.ts +8 -4
- package/lib/esm/components/payment_methods/PaymentMethod.js +1 -1
- package/lib/esm/components/payment_methods/PaymentMethodRadioButton.d.ts +6 -2
- package/lib/esm/components/payment_methods/PaymentMethodRadioButton.js +1 -1
- package/lib/esm/components/shipments/Shipment.js +1 -1
- package/lib/esm/components/shipping_methods/ShippingMethodRadioButton.d.ts +9 -5
- package/lib/esm/components/shipping_methods/ShippingMethodRadioButton.js +1 -1
- package/lib/esm/components/utils/PaymentCardsTemplate.d.ts +2 -3
- package/lib/esm/context/AddressContext.d.ts +2 -3
- package/lib/esm/context/AddressContext.js +1 -1
- package/lib/esm/context/OrderContext.d.ts +5 -5
- package/lib/esm/context/PaymentMethodContext.d.ts +2 -2
- package/lib/esm/context/PlaceOrderContext.d.ts +4 -10
- package/lib/esm/context/ShipmentContext.d.ts +2 -3
- package/lib/esm/context/ShipmentContext.js +1 -1
- package/lib/esm/index.d.ts +0 -2
- package/lib/esm/reducers/AddressReducer.d.ts +11 -7
- package/lib/esm/reducers/AddressReducer.js +1 -1
- package/lib/esm/reducers/OrderReducer.d.ts +9 -6
- package/lib/esm/reducers/OrderReducer.js +1 -1
- package/lib/esm/reducers/PaymentMethodReducer.d.ts +6 -3
- package/lib/esm/reducers/PaymentMethodReducer.js +1 -1
- package/lib/esm/reducers/PlaceOrderReducer.d.ts +16 -18
- package/lib/esm/reducers/PlaceOrderReducer.js +1 -1
- package/lib/esm/reducers/ShipmentReducer.d.ts +6 -3
- package/lib/esm/reducers/ShipmentReducer.js +1 -1
- package/lib/tsconfig.prod.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.prod.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
2
|
import { ChildrenFunction } from '../../typings/index';
|
|
3
|
+
import type { Order } from '@commercelayer/sdk';
|
|
4
|
+
interface TOnClick {
|
|
5
|
+
success: boolean;
|
|
6
|
+
order?: Order;
|
|
7
|
+
}
|
|
3
8
|
interface ChildrenProps extends Omit<Props, 'children'> {
|
|
4
9
|
}
|
|
5
|
-
interface Props extends Omit<JSX.IntrinsicElements['button'], 'children'> {
|
|
10
|
+
interface Props extends Omit<JSX.IntrinsicElements['button'], 'children' | 'onClick'> {
|
|
6
11
|
children?: ChildrenFunction<ChildrenProps>;
|
|
7
12
|
label?: string | ReactNode;
|
|
8
|
-
onClick?: () => void;
|
|
13
|
+
onClick?: (params: TOnClick) => void;
|
|
9
14
|
addressId?: string;
|
|
10
15
|
}
|
|
11
16
|
export declare function SaveAddressesButton(props: Props): JSX.Element;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.SaveAddressesButton=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")),AddressContext_1=tslib_1.__importDefault(require("../../context/AddressContext")),addressesManager_1=require("../../utils/addressesManager"),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),CustomerContext_1=tslib_1.__importDefault(require("../../context/CustomerContext")),isFunction_1=tslib_1.__importDefault(require("lodash/isFunction"));function SaveAddressesButton(props){const{children,label="Continue to delivery",resource,disabled=!1,addressId,onClick,...p}=props,{errors,billing_address:billingAddress,shipToDifferentAddress,shipping_address:shippingAddress,saveAddresses,billingAddressId,shippingAddressId}=(0,react_1.useContext)(AddressContext_1.default),{order}=(0,react_1.useContext)(OrderContext_1.default),{addresses,isGuest,createCustomerAddress}=(0,react_1.useContext)(CustomerContext_1.default),[forceDisable,setForceDisable]=(0,react_1.useState)(disabled),customerEmail=(isGuest===!0||typeof isGuest>"u")&&!order?.customer_email,billingDisable=(0,addressesManager_1.billingAddressController)({billing_address:billingAddress,errors,billingAddressId,requiresBillingInfo:order?.requires_billing_info}),shippingDisable=(0,addressesManager_1.shippingAddressController)({billingDisable,errors,shipToDifferentAddress,shipping_address:shippingAddress,shippingAddressId}),countryLockDisable=(0,addressesManager_1.countryLockController)({countryCodeLock:order?.shipping_country_code_lock,addresses,shipToDifferentAddress,billingAddressId,billing_address:billingAddress,shipping_address:shippingAddress,shippingAddressId}),disable=disabled||customerEmail||billingDisable||shippingDisable||countryLockDisable,handleClick=()=>{if(errors&&Object.keys(errors).length===0&&!disable){if(setForceDisable(!0),order)saveAddresses();else if(createCustomerAddress&&billingAddress){const address={...billingAddress};addressId&&(address.id=addressId),createCustomerAddress(address)}setForceDisable(!1),onClick&&onClick()}},parentProps={...p,label,resource,handleClick,disabled:disable};return children?(0,jsx_runtime_1.jsx)(Parent_1.default,{...parentProps,children}):(0,jsx_runtime_1.jsx)("button",{type:"button",disabled:disable||forceDisable,onClick:handleClick,...p,children:(0,isFunction_1.default)(label)?label():label})}exports.SaveAddressesButton=SaveAddressesButton,exports.default=SaveAddressesButton;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.SaveAddressesButton=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")),AddressContext_1=tslib_1.__importDefault(require("../../context/AddressContext")),addressesManager_1=require("../../utils/addressesManager"),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),CustomerContext_1=tslib_1.__importDefault(require("../../context/CustomerContext")),isFunction_1=tslib_1.__importDefault(require("lodash/isFunction"));function SaveAddressesButton(props){const{children,label="Continue to delivery",resource,disabled=!1,addressId,onClick,...p}=props,{errors,billing_address:billingAddress,shipToDifferentAddress,shipping_address:shippingAddress,saveAddresses,billingAddressId,shippingAddressId}=(0,react_1.useContext)(AddressContext_1.default),{order}=(0,react_1.useContext)(OrderContext_1.default),{addresses,isGuest,createCustomerAddress}=(0,react_1.useContext)(CustomerContext_1.default),[forceDisable,setForceDisable]=(0,react_1.useState)(disabled),customerEmail=(isGuest===!0||typeof isGuest>"u")&&!order?.customer_email,billingDisable=(0,addressesManager_1.billingAddressController)({billing_address:billingAddress,errors,billingAddressId,requiresBillingInfo:order?.requires_billing_info}),shippingDisable=(0,addressesManager_1.shippingAddressController)({billingDisable,errors,shipToDifferentAddress,shipping_address:shippingAddress,shippingAddressId}),countryLockDisable=(0,addressesManager_1.countryLockController)({countryCodeLock:order?.shipping_country_code_lock,addresses,shipToDifferentAddress,billingAddressId,billing_address:billingAddress,shipping_address:shippingAddress,shippingAddressId}),disable=disabled||customerEmail||billingDisable||shippingDisable||countryLockDisable,handleClick=async()=>{if(errors&&Object.keys(errors).length===0&&!disable){let orderUpdated={success:!1};if(setForceDisable(!0),order&&saveAddresses!=null)orderUpdated=await saveAddresses();else if(createCustomerAddress&&billingAddress){const address={...billingAddress};addressId&&(address.id=addressId),createCustomerAddress(address)}setForceDisable(!1),onClick&&orderUpdated.success&&onClick(orderUpdated)}},parentProps={...p,label,resource,handleClick,disabled:disable};return children?(0,jsx_runtime_1.jsx)(Parent_1.default,{...parentProps,children}):(0,jsx_runtime_1.jsx)("button",{type:"button",disabled:disable||forceDisable,onClick:()=>{handleClick()},...p,children:(0,isFunction_1.default)(label)?label():label})}exports.SaveAddressesButton=SaveAddressesButton,exports.default=SaveAddressesButton;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type
|
|
3
|
-
|
|
1
|
+
import type { Order } from '@commercelayer/sdk';
|
|
2
|
+
import type { DefaultChildrenType } from '../../typings/globals';
|
|
3
|
+
interface Props extends Omit<JSX.IntrinsicElements['form'], 'onSubmit'> {
|
|
4
|
+
children: DefaultChildrenType;
|
|
4
5
|
onSubmit?: (response: {
|
|
5
6
|
success: boolean;
|
|
6
7
|
value: string;
|
|
8
|
+
order?: Order;
|
|
7
9
|
}) => void;
|
|
8
|
-
}
|
|
10
|
+
}
|
|
9
11
|
export declare function GiftCardOrCouponForm(props: Props): JSX.Element | null;
|
|
10
12
|
export default GiftCardOrCouponForm;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.GiftCardOrCouponForm=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),rapid_form_1=require("rapid-form"),react_1=require("react"),CouponAndGiftCardFormContext_1=tslib_1.__importDefault(require("../../context/CouponAndGiftCardFormContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),camelCase_1=tslib_1.__importDefault(require("lodash/camelCase")),dropWhile_1=tslib_1.__importDefault(require("lodash/dropWhile")),has_1=tslib_1.__importDefault(require("lodash/has")),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.GiftCardOrCouponForm=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),rapid_form_1=require("rapid-form"),react_1=require("react"),CouponAndGiftCardFormContext_1=tslib_1.__importDefault(require("../../context/CouponAndGiftCardFormContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),camelCase_1=tslib_1.__importDefault(require("lodash/camelCase")),dropWhile_1=tslib_1.__importDefault(require("lodash/dropWhile")),has_1=tslib_1.__importDefault(require("lodash/has")),findIndex_1=tslib_1.__importDefault(require("lodash/findIndex"));function GiftCardOrCouponForm(props){const{children,autoComplete="on",onSubmit,...p}=props,{validation,values,reset}=(0,rapid_form_1.useRapidForm)(),[codeType,setCodeType]=(0,react_1.useState)("gift_card_or_coupon_code"),{setGiftCardOrCouponCode,order,errors,setOrderErrors}=(0,react_1.useContext)(OrderContext_1.default),ref=(0,react_1.useRef)(null),inputName="gift_card_or_coupon_code";(0,react_1.useEffect)(()=>{if(values[inputName]?.value===""&&(0,findIndex_1.default)(errors,{field:(0,camelCase_1.default)(inputName)})!==-1){const err=(0,dropWhile_1.default)(errors,i=>i.field===(0,camelCase_1.default)(inputName));setOrderErrors(err),onSubmit&&onSubmit({value:values[inputName]?.value,success:!1})}values[inputName]?.value===""&&(setOrderErrors([]),onSubmit&&onSubmit({value:values[inputName]?.value,success:!1}))},[values]),(0,react_1.useEffect)(()=>{order?.gift_card_code&&!order?.coupon_code&&setCodeType("coupon_code"),!order?.gift_card_code&&order?.coupon_code&&setCodeType("gift_card_code"),!order?.gift_card_code&&!order?.coupon_code&&setCodeType("gift_card_or_coupon_code")},[order]);const handleSubmit=async e=>{e.preventDefault();const code=(0,has_1.default)(values,inputName)?values[inputName].value:void 0;if(code!=null&&setGiftCardOrCouponCode!=null){const{success,order:order2}=await setGiftCardOrCouponCode({code,codeType}),value=values[inputName]?.value;onSubmit&&onSubmit({success,value,order:order2}),success&&reset(e)}};return order?.gift_card_code&&order?.coupon_code||(0,isEmpty_1.default)(order)?null:(0,jsx_runtime_1.jsx)(CouponAndGiftCardFormContext_1.default.Provider,{value:{validation,codeType},children:(0,jsx_runtime_1.jsx)("form",{ref,autoComplete,onSubmit:e=>{handleSubmit(e)},...p,children})})}exports.GiftCardOrCouponForm=GiftCardOrCouponForm,exports.default=GiftCardOrCouponForm;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
2
|
import { ChildrenFunction } from '../../typings/index';
|
|
3
3
|
import { CodeType, OrderCodeType } from '../../reducers/OrderReducer';
|
|
4
|
+
import type { Order } from '@commercelayer/sdk';
|
|
4
5
|
interface ChildrenProps extends Omit<Props, 'children' | 'onClick'> {
|
|
5
6
|
codeType?: OrderCodeType;
|
|
6
7
|
hide?: boolean;
|
|
@@ -12,6 +13,7 @@ type Props = {
|
|
|
12
13
|
label?: string | ReactNode;
|
|
13
14
|
onClick?: (response: {
|
|
14
15
|
success: boolean;
|
|
16
|
+
order?: Order;
|
|
15
17
|
}) => void;
|
|
16
18
|
} & Omit<JSX.IntrinsicElements['button'], 'type' | 'onClick'>;
|
|
17
19
|
export declare function GiftCardOrCouponRemoveButton(props: Props): JSX.Element | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.GiftCardOrCouponRemoveButton=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")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext"));function GiftCardOrCouponRemoveButton(props){const{children,label="Remove",onClick,type,...p}=props,{order,removeGiftCardOrCouponCode}=(0,react_1.useContext)(OrderContext_1.default);let codeType=type?`${type}_code`:void 0;!type&&order&&"coupon_code"in order&&order.coupon_code!=null?codeType="coupon_code":type||(codeType="gift_card_code");const code=order&&codeType?order[codeType]:"",hide=!(order&&code),handleClick=async()=>{const response=
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.GiftCardOrCouponRemoveButton=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")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext"));function GiftCardOrCouponRemoveButton(props){const{children,label="Remove",onClick,type,...p}=props,{order,removeGiftCardOrCouponCode}=(0,react_1.useContext)(OrderContext_1.default);let codeType=type?`${type}_code`:void 0;!type&&order&&"coupon_code"in order&&order.coupon_code!=null?codeType="coupon_code":type||(codeType="gift_card_code");const code=order&&codeType?order[codeType]:"",hide=!(order&&code),handleClick=async()=>{if(codeType!=null&&removeGiftCardOrCouponCode!=null){const response=await removeGiftCardOrCouponCode({codeType});onClick!=null&&response!=null&&onClick(response)}},parentProps={...p,label,handleClick,codeType};return children?(0,jsx_runtime_1.jsx)(Parent_1.default,{...parentProps,children}):hide?null:(0,jsx_runtime_1.jsx)("button",{type:"button",onClick:()=>{handleClick()},...p,children:label})}exports.GiftCardOrCouponRemoveButton=GiftCardOrCouponRemoveButton,exports.default=GiftCardOrCouponRemoveButton;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.OrderContainer=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),OrderReducer_1=tslib_1.__importStar(require("../../reducers/OrderReducer")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),OrderContext_1=tslib_1.__importStar(require("../../context/OrderContext")),OrderStorageContext_1=tslib_1.__importDefault(require("../../context/OrderStorageContext")),compareObjAttribute_1=tslib_1.__importDefault(require("../../utils/compareObjAttribute")),useCustomContext_1=tslib_1.__importDefault(require("../../utils/hooks/useCustomContext"));function OrderContainer(props){const{orderId,children,metadata,attributes,fetchOrder}=props,[state,dispatch]=(0,react_1.useReducer)(OrderReducer_1.default,OrderReducer_1.orderInitialState),[lock,setLock]=(0,react_1.useState)(!1),[lockOrder,setLockOrder]=(0,react_1.useState)(!0),config=(0,useCustomContext_1.default)({context:CommerceLayerContext_1.default,contextComponentName:"CommerceLayer",currentComponentName:"OrderContainer",key:"accessToken"}),{persistKey,clearWhenPlaced,getLocalOrder,setLocalOrder,deleteLocalOrder}=(0,react_1.useContext)(OrderStorageContext_1.default);(0,react_1.useEffect)(()=>{state.withoutIncludes||dispatch({type:"setLoading",payload:{loading:!0}})},[state.withoutIncludes]),(0,react_1.useEffect)(()=>{if(attributes&&state?.order&&!lock){const updateAttributes=(0,compareObjAttribute_1.default)({attributes,object:state.order});Object.keys(updateAttributes).length>0&&((0,OrderReducer_1.updateOrder)({id:state.order.id,attributes:updateAttributes,dispatch,config,include:state.include,state}),setLock(!0))}return()=>{if(attributes&&state?.order){const updateAttributes=(0,compareObjAttribute_1.default)({attributes,object:state.order});state.order&&Object.keys(updateAttributes).length===0&&setLock(!1)}}},[attributes,state?.order,lock]),(0,react_1.useEffect)(()=>{const localOrder=persistKey?getLocalOrder(persistKey):orderId,startRequest=Object.keys(state?.includeLoaded||{}).filter(key=>state?.includeLoaded?.[key]===!0),getOrder=async()=>{const removeOrderPlaced=!!(persistKey&&clearWhenPlaced);localOrder&&await(0,OrderReducer_1.getApiOrder)({id:localOrder,dispatch,config,persistKey,clearWhenPlaced:removeOrderPlaced,deleteLocalOrder,state})};return config.accessToken&&state.loading===!1&&state?.order==null?localOrder&&!state.order&&state.include?.length===startRequest.length&&!state.withoutIncludes&&!lockOrder
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.OrderContainer=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),OrderReducer_1=tslib_1.__importStar(require("../../reducers/OrderReducer")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),OrderContext_1=tslib_1.__importStar(require("../../context/OrderContext")),OrderStorageContext_1=tslib_1.__importDefault(require("../../context/OrderStorageContext")),compareObjAttribute_1=tslib_1.__importDefault(require("../../utils/compareObjAttribute")),useCustomContext_1=tslib_1.__importDefault(require("../../utils/hooks/useCustomContext"));function OrderContainer(props){const{orderId,children,metadata,attributes,fetchOrder}=props,[state,dispatch]=(0,react_1.useReducer)(OrderReducer_1.default,OrderReducer_1.orderInitialState),[lock,setLock]=(0,react_1.useState)(!1),[lockOrder,setLockOrder]=(0,react_1.useState)(!0),config=(0,useCustomContext_1.default)({context:CommerceLayerContext_1.default,contextComponentName:"CommerceLayer",currentComponentName:"OrderContainer",key:"accessToken"}),{persistKey,clearWhenPlaced,getLocalOrder,setLocalOrder,deleteLocalOrder}=(0,react_1.useContext)(OrderStorageContext_1.default);(0,react_1.useEffect)(()=>{state.withoutIncludes||dispatch({type:"setLoading",payload:{loading:!0}})},[state.withoutIncludes]),(0,react_1.useEffect)(()=>{if(attributes&&state?.order&&!lock){const updateAttributes=(0,compareObjAttribute_1.default)({attributes,object:state.order});Object.keys(updateAttributes).length>0&&((0,OrderReducer_1.updateOrder)({id:state.order.id,attributes:updateAttributes,dispatch,config,include:state.include,state}),setLock(!0))}return()=>{if(attributes&&state?.order){const updateAttributes=(0,compareObjAttribute_1.default)({attributes,object:state.order});state.order&&Object.keys(updateAttributes).length===0&&setLock(!1)}}},[attributes,state?.order,lock]),(0,react_1.useEffect)(()=>{const localOrder=persistKey?getLocalOrder(persistKey):orderId,startRequest=Object.keys(state?.includeLoaded||{}).filter(key=>state?.includeLoaded?.[key]===!0),getOrder=async()=>{const removeOrderPlaced=!!(persistKey&&clearWhenPlaced);localOrder&&await(0,OrderReducer_1.getApiOrder)({id:localOrder,dispatch,config,persistKey,clearWhenPlaced:removeOrderPlaced,deleteLocalOrder,state})};return config.accessToken&&state.loading===!1&&state?.order==null?(localOrder&&!state.order&&state.include?.length===startRequest.length&&!state.withoutIncludes&&!lockOrder||state.withoutIncludes&&!state.include?.length&&startRequest.length===0)&&getOrder():[config.accessToken,state.order!=null,state.loading,state.withoutIncludes].every(Boolean)?dispatch({type:"setLoading",payload:{loading:!1}}):[config.accessToken,state.order!=null,state.loading,state.withoutIncludes===!1].every(Boolean)&&dispatch({type:"setLoading",payload:{loading:!1}}),()=>{state.order==null&&state.loading&&state.withoutIncludes===!1&&(state.include?.length===0&&startRequest.length>0?dispatch({type:"setLoading",payload:{loading:!1}}):state.include&&state.include?.length>0&&(dispatch({type:"setIncludesResource",payload:{include:[]}}),setLockOrder(!1)))}},[config.accessToken,Object.keys(state.includeLoaded??{}).length,state.include?.length,orderId,Object.keys(state?.order??{}).length,state.loading,state.withoutIncludes,lockOrder]);const orderValue=(0,react_1.useMemo)(()=>{if(fetchOrder!=null&&state?.order!=null){fetchOrder(state.order);debugger}return{...state,setOrder:order=>(0,OrderReducer_1.setOrder)(order,dispatch),getOrder:async id=>await(0,OrderReducer_1.getApiOrder)({id,dispatch,config,state}),setOrderErrors:errors=>(0,OrderReducer_1.setOrderErrors)({dispatch,errors}),createOrder:async()=>await(0,OrderReducer_1.createOrder)({persistKey,dispatch,config,state,orderMetadata:metadata,orderAttributes:attributes,setLocalOrder}),addToCart:async params=>await(0,OrderReducer_1.addToCart)({...params,persistKey,dispatch,state,config,errors:state.errors,orderMetadata:metadata||{},orderAttributes:attributes,setLocalOrder}),saveAddressToCustomerAddressBook:args=>OrderContext_1.defaultOrderContext.saveAddressToCustomerAddressBook({...args,dispatch}),setGiftCardOrCouponCode:async({code,codeType})=>await OrderContext_1.defaultOrderContext.setGiftCardOrCouponCode({code,codeType,dispatch,order:state.order,config,include:state.include,state}),removeGiftCardOrCouponCode:async({codeType})=>await OrderContext_1.defaultOrderContext.removeGiftCardOrCouponCode({codeType,dispatch,order:state.order,config,include:state.include,state}),addResourceToInclude:args=>OrderContext_1.defaultOrderContext.addResourceToInclude({...args,dispatch,resourcesIncluded:state.include,resourceIncludedLoaded:state.includeLoaded}),updateOrder:async args=>await OrderContext_1.defaultOrderContext.updateOrder({...args,dispatch,config,include:state.include,state})}},[state,config.accessToken]);return(0,jsx_runtime_1.jsx)(OrderContext_1.default.Provider,{value:orderValue,children})}exports.OrderContainer=OrderContainer,exports.default=OrderContainer;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
2
|
import { ChildrenFunction } from '../../typings/index';
|
|
3
|
+
import type { BaseError } from '../../typings/errors';
|
|
4
|
+
import type { Order } from '@commercelayer/sdk';
|
|
3
5
|
interface ChildrenProps extends Omit<Props, 'children'> {
|
|
4
6
|
handleClick: () => Promise<void>;
|
|
5
7
|
}
|
|
@@ -8,6 +10,8 @@ interface Props extends Omit<JSX.IntrinsicElements['button'], 'children' | 'onCl
|
|
|
8
10
|
label?: string | ReactNode;
|
|
9
11
|
onClick?: (response: {
|
|
10
12
|
placed: boolean;
|
|
13
|
+
order?: Order;
|
|
14
|
+
errors?: BaseError[];
|
|
11
15
|
}) => void;
|
|
12
16
|
}
|
|
13
17
|
export declare function PlaceOrderButton(props: Props): JSX.Element;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { LoaderType } from '../../typings/index';
|
|
2
|
-
import { PaymentMethod as PaymentMethodType } from '@commercelayer/sdk';
|
|
3
|
-
import { PaymentResource } from '../../reducers/PaymentMethodReducer';
|
|
1
|
+
import type { LoaderType } from '../../typings/index';
|
|
2
|
+
import type { Order, PaymentMethod as PaymentMethodType } from '@commercelayer/sdk';
|
|
3
|
+
import type { PaymentResource } from '../../reducers/PaymentMethodReducer';
|
|
4
4
|
import type { DefaultChildrenType } from '../../typings/globals';
|
|
5
|
+
interface TOnClickParams {
|
|
6
|
+
payment?: PaymentMethodType | Record<string, any>;
|
|
7
|
+
order?: Order;
|
|
8
|
+
}
|
|
5
9
|
type Props = {
|
|
6
10
|
/**
|
|
7
11
|
* Hide payment methods by an array of strings or a function that returns a boolean
|
|
@@ -13,7 +17,7 @@ type Props = {
|
|
|
13
17
|
autoSelectSinglePaymentMethod?: boolean | (() => void);
|
|
14
18
|
} & Omit<JSX.IntrinsicElements['div'], 'onClick' | 'children'> & ({
|
|
15
19
|
clickableContainer: true;
|
|
16
|
-
onClick?: (
|
|
20
|
+
onClick?: (params?: TOnClickParams) => void;
|
|
17
21
|
} | {
|
|
18
22
|
clickableContainer?: never;
|
|
19
23
|
onClick?: never;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PaymentMethod=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),PaymentMethodContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodContext")),PaymentMethodChildrenContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodChildrenContext")),getLoaderComponent_1=tslib_1.__importDefault(require("../../utils/getLoaderComponent")),useCustomContext_1=tslib_1.__importDefault(require("../../utils/hooks/useCustomContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),CustomerContext_1=tslib_1.__importDefault(require("../../context/CustomerContext")),getPaymentAttributes_1=require("../../utils/getPaymentAttributes");function PaymentMethod({children,className,activeClass,loader="Loading...",clickableContainer,autoSelectSinglePaymentMethod,hide,onClick,...p}){const[loading,setLoading]=(0,react_1.useState)(!0),[paymentSelected,setPaymentSelected]=(0,react_1.useState)(""),{paymentMethods,currentPaymentMethodId,setPaymentMethod,setLoading:setLoadingPlaceOrder,paymentSource,setPaymentSource,config}=(0,useCustomContext_1.default)({context:PaymentMethodContext_1.default,contextComponentName:"PaymentMethodsContainer",currentComponentName:"PaymentMethod",key:"paymentMethods"}),{order}=(0,react_1.useContext)(OrderContext_1.default),{getCustomerPaymentSources}=(0,react_1.useContext)(CustomerContext_1.default);(0,react_1.useEffect)(()=>{paymentMethods!=null&&autoSelectSinglePaymentMethod!=null&&(async()=>{if(paymentMethods.length===1){const[paymentMethod]=paymentMethods??[];if(paymentMethod&&!paymentSource){setLoadingPlaceOrder({loading:!0}),setPaymentSelected(paymentMethod.id);const paymentMethodId=paymentMethod?.id,paymentResource=paymentMethod?.payment_source_type;await setPaymentMethod({paymentResource,paymentMethodId});let attributes={};config!=null&&paymentResource==="paypal_payments"&&(attributes=(0,getPaymentAttributes_1.getPaypalAttributes)(paymentResource,config)),config!=null&&paymentResource==="external_payments"&&(attributes=(0,getPaymentAttributes_1.getExternalPaymentAttributes)(paymentResource,config))
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PaymentMethod=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),PaymentMethodContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodContext")),PaymentMethodChildrenContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodChildrenContext")),getLoaderComponent_1=tslib_1.__importDefault(require("../../utils/getLoaderComponent")),useCustomContext_1=tslib_1.__importDefault(require("../../utils/hooks/useCustomContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),CustomerContext_1=tslib_1.__importDefault(require("../../context/CustomerContext")),getPaymentAttributes_1=require("../../utils/getPaymentAttributes");function PaymentMethod({children,className,activeClass,loader="Loading...",clickableContainer,autoSelectSinglePaymentMethod,hide,onClick,...p}){const[loading,setLoading]=(0,react_1.useState)(!0),[paymentSelected,setPaymentSelected]=(0,react_1.useState)(""),{paymentMethods,currentPaymentMethodId,setPaymentMethod,setLoading:setLoadingPlaceOrder,paymentSource,setPaymentSource,config}=(0,useCustomContext_1.default)({context:PaymentMethodContext_1.default,contextComponentName:"PaymentMethodsContainer",currentComponentName:"PaymentMethod",key:"paymentMethods"}),{order}=(0,react_1.useContext)(OrderContext_1.default),{getCustomerPaymentSources}=(0,react_1.useContext)(CustomerContext_1.default);(0,react_1.useEffect)(()=>{paymentMethods!=null&&autoSelectSinglePaymentMethod!=null&&(async()=>{if(paymentMethods.length===1){const[paymentMethod]=paymentMethods??[];if(paymentMethod&&!paymentSource){setLoadingPlaceOrder({loading:!0}),setPaymentSelected(paymentMethod.id);const paymentMethodId=paymentMethod?.id,paymentResource=paymentMethod?.payment_source_type;await setPaymentMethod({paymentResource,paymentMethodId});let attributes={};config!=null&&paymentResource==="paypal_payments"&&(attributes=(0,getPaymentAttributes_1.getPaypalAttributes)(paymentResource,config)),config!=null&&paymentResource==="external_payments"&&(attributes=(0,getPaymentAttributes_1.getExternalPaymentAttributes)(paymentResource,config)),await setPaymentSource({paymentResource,order,attributes})&&paymentMethod&&onClick!=null&&(onClick({payment:paymentMethod,order}),setTimeout(()=>{setLoading(!1)},200)),getCustomerPaymentSources&&await getCustomerPaymentSources(),setLoadingPlaceOrder({loading:!1})}typeof autoSelectSinglePaymentMethod=="function"&&autoSelectSinglePaymentMethod()}else setTimeout(()=>{setLoading(!1)},200)})()},[paymentMethods]),(0,react_1.useEffect)(()=>(paymentMethods&&(paymentMethods.length===1&&autoSelectSinglePaymentMethod?paymentSource&&setTimeout(()=>{setLoading(!1)},200):setLoading(!1)),currentPaymentMethodId&&setPaymentSelected(currentPaymentMethodId),()=>{setLoading(!0)}),[paymentMethods,currentPaymentMethodId]);const components=paymentMethods?.filter(payment=>{if(Array.isArray(hide)){const source=payment?.payment_source_type;return!hide?.includes(source)}else if(typeof hide=="function")return hide(payment);return!0}).map((payment,k)=>{const isActive=currentPaymentMethodId===payment?.id,paymentMethodProps={payment,clickableContainer,paymentSelected,setPaymentSelected},paymentResource=payment?.payment_source_type,onClickable=clickableContainer?async e=>{e.stopPropagation(),setLoadingPlaceOrder({loading:!0}),setPaymentSelected(payment.id);const paymentMethodId=payment?.id,{order:order2}=await setPaymentMethod({paymentResource,paymentMethodId});onClick&&onClick({payment,order:order2}),setLoadingPlaceOrder({loading:!1})}:void 0;return(0,jsx_runtime_1.jsx)("div",{"data-test-id":paymentResource,className:`${className??""} ${isActive&&activeClass!=null?activeClass:""}`,onClick:onClickable,...p,children:(0,jsx_runtime_1.jsx)(PaymentMethodChildrenContext_1.default.Provider,{value:paymentMethodProps,children})},k)});return loading?(0,getLoaderComponent_1.default)(loader):(0,jsx_runtime_1.jsx)(jsx_runtime_1.Fragment,{children:components})}exports.PaymentMethod=PaymentMethod,exports.default=PaymentMethod;
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { ChangeEvent } from 'react';
|
|
2
|
-
import { PaymentMethod } from '@commercelayer/sdk';
|
|
2
|
+
import type { Order, PaymentMethod } from '@commercelayer/sdk';
|
|
3
3
|
import { ChildrenFunction } from '../../typings/index';
|
|
4
4
|
interface ChildrenProps extends Omit<Props, 'children'> {
|
|
5
5
|
checked: boolean;
|
|
6
6
|
handleOnChange: (event: ChangeEvent<HTMLInputElement>) => Promise<void>;
|
|
7
7
|
}
|
|
8
|
+
interface TOnChangeParams {
|
|
9
|
+
payment?: PaymentMethod | Record<string, any>;
|
|
10
|
+
order?: Order;
|
|
11
|
+
}
|
|
8
12
|
type Props = {
|
|
9
13
|
children?: ChildrenFunction<ChildrenProps>;
|
|
10
|
-
onChange?: (
|
|
14
|
+
onChange?: (params: TOnChangeParams) => void;
|
|
11
15
|
} & JSX.IntrinsicElements['input'];
|
|
12
16
|
export declare function PaymentMethodRadioButton(props: Props): JSX.Element;
|
|
13
17
|
export default PaymentMethodRadioButton;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PaymentMethodRadioButton=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),PaymentMethodChildrenContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodChildrenContext")),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),PaymentMethodContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),useCustomContext_1=tslib_1.__importDefault(require("../../utils/hooks/useCustomContext"));function PaymentMethodRadioButton(props){const{onChange,...p}=props,{payment,paymentSelected,setPaymentSelected,clickableContainer}=(0,useCustomContext_1.default)({context:PaymentMethodChildrenContext_1.default,contextComponentName:"PaymentMethod",currentComponentName:"PaymentMethodRadioButton",key:"payment"}),{order}=(0,react_1.useContext)(OrderContext_1.default),{setPaymentMethod,setLoading}=(0,react_1.useContext)(PaymentMethodContext_1.default),orderId=order?.id||"",paymentResource=payment?.payment_source_type,paymentMethodId=payment?.id,name=`payment-${orderId}`,checked=paymentSelected===payment?.id,handleOnChange=async e=>{e.stopPropagation(),setPaymentSelected&&setPaymentSelected(paymentMethodId),setLoading({loading:!0}),!clickableContainer
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PaymentMethodRadioButton=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),PaymentMethodChildrenContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodChildrenContext")),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),PaymentMethodContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),useCustomContext_1=tslib_1.__importDefault(require("../../utils/hooks/useCustomContext"));function PaymentMethodRadioButton(props){const{onChange,...p}=props,{payment,paymentSelected,setPaymentSelected,clickableContainer}=(0,useCustomContext_1.default)({context:PaymentMethodChildrenContext_1.default,contextComponentName:"PaymentMethod",currentComponentName:"PaymentMethodRadioButton",key:"payment"}),{order}=(0,react_1.useContext)(OrderContext_1.default),{setPaymentMethod,setLoading}=(0,react_1.useContext)(PaymentMethodContext_1.default),orderId=order?.id||"",paymentResource=payment?.payment_source_type,paymentMethodId=payment?.id,name=`payment-${orderId}`,checked=paymentSelected===payment?.id,handleOnChange=async e=>{if(e.stopPropagation(),setPaymentSelected&&setPaymentSelected(paymentMethodId),setLoading({loading:!0}),!clickableContainer){const{order:order2}=await setPaymentMethod({paymentResource,paymentMethodId});onChange&&onChange({payment,order:order2})}setLoading({loading:!1})},id=payment?.payment_source_type,parentProps={handleOnChange,checked,id,name,...props};return props.children?(0,jsx_runtime_1.jsx)(Parent_1.default,{...parentProps,children:props.children}):(0,jsx_runtime_1.jsx)("input",{title:name,type:"radio",id,onChange:e=>{handleOnChange(e)},checked,...p})}exports.PaymentMethodRadioButton=PaymentMethodRadioButton,exports.default=PaymentMethodRadioButton;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Shipment=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),ShipmentContext_1=tslib_1.__importDefault(require("../../context/ShipmentContext")),ShipmentChildrenContext_1=tslib_1.__importDefault(require("../../context/ShipmentChildrenContext")),getLoaderComponent_1=tslib_1.__importDefault(require("../../utils/getLoaderComponent"));function Shipment({children,loader="Loading...",autoSelectSingleShippingMethod=!1}){const[loading,setLoading]=(0,react_1.useState)(!0),{shipments,deliveryLeadTimes,setShippingMethod}=(0,react_1.useContext)(ShipmentContext_1.default);(0,react_1.useEffect)(()=>{shipments&&(autoSelectSingleShippingMethod?(async()=>shipments.forEach(shipment=>{const isSingle=shipment?.available_shipping_methods?.length===1;if(!shipment?.shipping_method&&isSingle){const[shippingMethod]=shipment?.available_shipping_methods||[];shippingMethod&&setShippingMethod(shipment.id,shippingMethod.id),typeof autoSelectSingleShippingMethod=="function"&&autoSelectSingleShippingMethod()}else setTimeout(()=>{setLoading(!1)},200)}))():setLoading(!1))},[shipments]);const components=shipments?.map((shipment,k)=>{const lineItems=shipment.shipment_line_items?.map(shipmentLineItem=>{const l=shipmentLineItem.line_item;return l&&(l.quantity=shipmentLineItem.quantity),l}),shippingMethods=shipment.available_shipping_methods,currentShippingMethodId=autoSelectSingleShippingMethod&&shippingMethods&&shippingMethods.length===1?shippingMethods[0]?.id:shipment.shipping_method?.id,stockTransfers=shipment.stock_transfers,parcels=shipment.parcels,times=deliveryLeadTimes?.filter(time=>time.stock_location?.id===shipment.stock_location?.id),shipmentProps={parcels,lineItems,shippingMethods,currentShippingMethodId,stockTransfers,deliveryLeadTimes:times,shipment,keyNumber:k+1};return(0,jsx_runtime_1.jsx)(ShipmentChildrenContext_1.default.Provider,{value:shipmentProps,children},k)});return loading?(0,getLoaderComponent_1.default)(loader):(0,jsx_runtime_1.jsx)(jsx_runtime_1.Fragment,{children:components})}exports.Shipment=Shipment,exports.default=Shipment;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Shipment=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),ShipmentContext_1=tslib_1.__importDefault(require("../../context/ShipmentContext")),ShipmentChildrenContext_1=tslib_1.__importDefault(require("../../context/ShipmentChildrenContext")),getLoaderComponent_1=tslib_1.__importDefault(require("../../utils/getLoaderComponent"));function Shipment({children,loader="Loading...",autoSelectSingleShippingMethod=!1}){const[loading,setLoading]=(0,react_1.useState)(!0),{shipments,deliveryLeadTimes,setShippingMethod}=(0,react_1.useContext)(ShipmentContext_1.default);(0,react_1.useEffect)(()=>{shipments&&(autoSelectSingleShippingMethod?(async()=>shipments.forEach(shipment=>{const isSingle=shipment?.available_shipping_methods?.length===1;if(!shipment?.shipping_method&&isSingle){const[shippingMethod]=shipment?.available_shipping_methods||[];shippingMethod&&setShippingMethod!=null&&setShippingMethod(shipment.id,shippingMethod.id),typeof autoSelectSingleShippingMethod=="function"&&autoSelectSingleShippingMethod()}else setTimeout(()=>{setLoading(!1)},200)}))():setLoading(!1))},[shipments]);const components=shipments?.map((shipment,k)=>{const lineItems=shipment.shipment_line_items?.map(shipmentLineItem=>{const l=shipmentLineItem.line_item;return l&&(l.quantity=shipmentLineItem.quantity),l}),shippingMethods=shipment.available_shipping_methods,currentShippingMethodId=autoSelectSingleShippingMethod&&shippingMethods&&shippingMethods.length===1?shippingMethods[0]?.id:shipment.shipping_method?.id,stockTransfers=shipment.stock_transfers,parcels=shipment.parcels,times=deliveryLeadTimes?.filter(time=>time.stock_location?.id===shipment.stock_location?.id),shipmentProps={parcels,lineItems,shippingMethods,currentShippingMethodId,stockTransfers,deliveryLeadTimes:times,shipment,keyNumber:k+1};return(0,jsx_runtime_1.jsx)(ShipmentChildrenContext_1.default.Provider,{value:shipmentProps,children},k)});return loading?(0,getLoaderComponent_1.default)(loader):(0,jsx_runtime_1.jsx)(jsx_runtime_1.Fragment,{children:components})}exports.Shipment=Shipment,exports.default=Shipment;
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import type { ShippingMethod } from '@commercelayer/sdk';
|
|
2
|
-
|
|
1
|
+
import type { Order, ShippingMethod } from '@commercelayer/sdk';
|
|
2
|
+
interface ShippingMethodRadioButtonType extends Omit<Props, 'children'> {
|
|
3
3
|
shippingMethod: ShippingMethod;
|
|
4
4
|
shipmentId: string;
|
|
5
|
-
}
|
|
6
|
-
|
|
5
|
+
}
|
|
6
|
+
interface TOnChange {
|
|
7
|
+
shippingMethod: ShippingMethod;
|
|
8
|
+
shipmentId: string;
|
|
9
|
+
order?: Order;
|
|
10
|
+
}
|
|
7
11
|
type Props = {
|
|
8
12
|
children?: (props: ShippingMethodRadioButtonType) => JSX.Element;
|
|
9
|
-
onChange?:
|
|
13
|
+
onChange?: (params: TOnChange) => void;
|
|
10
14
|
} & Omit<JSX.IntrinsicElements['input'], 'onChange'>;
|
|
11
15
|
export declare function ShippingMethodRadioButton(props: Props): JSX.Element;
|
|
12
16
|
export default ShippingMethodRadioButton;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ShippingMethodRadioButton=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),ShippingMethodChildrenContext_1=tslib_1.__importDefault(require("../../context/ShippingMethodChildrenContext")),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),ShipmentContext_1=tslib_1.__importDefault(require("../../context/ShipmentContext"));function ShippingMethodRadioButton(props){const{onChange,...p}=props,[checked,setChecked]=(0,react_1.useState)(!1),{shippingMethod,currentShippingMethodId,shipmentId}=(0,react_1.useContext)(ShippingMethodChildrenContext_1.default),{setShippingMethod}=(0,react_1.useContext)(ShipmentContext_1.default),shippingMethodId=shippingMethod?.id,name=`shipment-${shipmentId??""}`,id=`${name}-${shippingMethodId??""}`;(0,react_1.useEffect)(()=>(setChecked(shippingMethodId===currentShippingMethodId),()=>{setChecked(!1)}),[currentShippingMethodId]);const handleOnChange=async()=>{shipmentId&&shippingMethodId&&
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ShippingMethodRadioButton=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),ShippingMethodChildrenContext_1=tslib_1.__importDefault(require("../../context/ShippingMethodChildrenContext")),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),ShipmentContext_1=tslib_1.__importDefault(require("../../context/ShipmentContext"));function ShippingMethodRadioButton(props){const{onChange,...p}=props,[checked,setChecked]=(0,react_1.useState)(!1),{shippingMethod,currentShippingMethodId,shipmentId}=(0,react_1.useContext)(ShippingMethodChildrenContext_1.default),{setShippingMethod}=(0,react_1.useContext)(ShipmentContext_1.default),shippingMethodId=shippingMethod?.id,name=`shipment-${shipmentId??""}`,id=`${name}-${shippingMethodId??""}`;(0,react_1.useEffect)(()=>(setChecked(shippingMethodId===currentShippingMethodId),()=>{setChecked(!1)}),[currentShippingMethodId]);const handleOnChange=async()=>{if(shipmentId&&shippingMethodId&&setShippingMethod!=null){setChecked(!0);const{order}=await setShippingMethod(shipmentId,shippingMethodId);shippingMethod&&onChange!=null&&onChange({shippingMethod,shipmentId,order})}},parentProps={shippingMethod,shipmentId,handleOnChange,name,id,...props};return props.children?(0,jsx_runtime_1.jsx)(Parent_1.default,{...parentProps,children:props.children}):(0,jsx_runtime_1.jsx)("input",{type:"radio",name,id,onChange:()=>{handleOnChange()},checked,...p})}exports.ShippingMethodRadioButton=ShippingMethodRadioButton,exports.default=ShippingMethodRadioButton;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { PaymentResource, PaymentSourceType } from '../../reducers/PaymentMethodReducer';
|
|
2
2
|
import PaymentSourceContext, { IconBrand } from '../../context/PaymentSourceContext';
|
|
3
3
|
import { ChildrenFunction } from '../../typings/index';
|
|
4
|
-
|
|
4
|
+
interface ChildrenProps extends Pick<Props, 'customerPayments'> {
|
|
5
5
|
PaymentSourceProvider: typeof PaymentSourceContext.Provider;
|
|
6
|
-
}
|
|
6
|
+
}
|
|
7
7
|
type CustomerPayment = PaymentSourceType & {
|
|
8
8
|
card?: {
|
|
9
9
|
brand?: IconBrand;
|
|
@@ -12,7 +12,6 @@ type CustomerPayment = PaymentSourceType & {
|
|
|
12
12
|
handleClick?: () => void;
|
|
13
13
|
};
|
|
14
14
|
export type CustomerCardsTemplateChildren = ChildrenFunction<ChildrenProps>;
|
|
15
|
-
export type CustomerCardsType = ChildrenProps;
|
|
16
15
|
interface Props {
|
|
17
16
|
customerPayments: CustomerPayment[];
|
|
18
17
|
children: CustomerCardsTemplateChildren;
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { AddressState, SetAddress, AddressResource } from '../reducers/AddressReducer';
|
|
2
|
+
import { AddressState, SetAddress, AddressResource, saveAddresses } from '../reducers/AddressReducer';
|
|
3
3
|
import { BaseError } from '../typings/errors';
|
|
4
4
|
type DefaultContext = {
|
|
5
|
-
saveAddresses
|
|
5
|
+
saveAddresses?: () => ReturnType<typeof saveAddresses>;
|
|
6
6
|
setCloneAddress: (id: string, resource: AddressResource) => void;
|
|
7
7
|
setAddress: SetAddress;
|
|
8
8
|
setAddressErrors: (errors: BaseError[], resource: AddressResource) => void;
|
|
9
9
|
} & AddressState;
|
|
10
10
|
export declare const defaultAddressContext: {
|
|
11
|
-
saveAddresses: () => void;
|
|
12
11
|
setCloneAddress: () => void;
|
|
13
12
|
setAddress: SetAddress;
|
|
14
13
|
setAddressErrors: () => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.defaultAddressContext=void 0;const react_1=require("react"),AddressReducer_1=require("../reducers/AddressReducer");exports.defaultAddressContext={
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.defaultAddressContext=void 0;const react_1=require("react"),AddressReducer_1=require("../reducers/AddressReducer");exports.defaultAddressContext={setCloneAddress:()=>{},setAddress:AddressReducer_1.setAddress,setAddressErrors:()=>{}};const AddressesContext=(0,react_1.createContext)(exports.defaultAddressContext);exports.default=AddressesContext;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { Order } from '@commercelayer/sdk';
|
|
3
|
-
import { OrderState, CreateOrder,
|
|
3
|
+
import { OrderState, CreateOrder, SaveAddressToCustomerAddressBook, setGiftCardOrCouponCode, removeGiftCardOrCouponCode, addResourceToInclude, updateOrder, getOrderContext, addToCart } from '../reducers/OrderReducer';
|
|
4
4
|
import { BaseError } from '../typings/errors';
|
|
5
5
|
interface DefaultContext extends OrderState {
|
|
6
6
|
createOrder: CreateOrder;
|
|
7
7
|
addToCart?: typeof addToCart;
|
|
8
8
|
setOrderErrors: (errors: BaseError[]) => void;
|
|
9
|
-
setGiftCardOrCouponCode
|
|
10
|
-
removeGiftCardOrCouponCode
|
|
9
|
+
setGiftCardOrCouponCode?: typeof setGiftCardOrCouponCode;
|
|
10
|
+
removeGiftCardOrCouponCode?: typeof removeGiftCardOrCouponCode;
|
|
11
11
|
saveAddressToCustomerAddressBook: SaveAddressToCustomerAddressBook;
|
|
12
12
|
addResourceToInclude: typeof addResourceToInclude;
|
|
13
13
|
getOrder: getOrderContext;
|
|
@@ -18,8 +18,8 @@ export declare const defaultOrderContext: {
|
|
|
18
18
|
createOrder: CreateOrder;
|
|
19
19
|
setOrderErrors: () => void;
|
|
20
20
|
setOrder: () => void;
|
|
21
|
-
setGiftCardOrCouponCode:
|
|
22
|
-
removeGiftCardOrCouponCode:
|
|
21
|
+
setGiftCardOrCouponCode: typeof setGiftCardOrCouponCode;
|
|
22
|
+
removeGiftCardOrCouponCode: typeof removeGiftCardOrCouponCode;
|
|
23
23
|
saveAddressToCustomerAddressBook: SaveAddressToCustomerAddressBook;
|
|
24
24
|
addResourceToInclude: typeof addResourceToInclude;
|
|
25
25
|
getOrder: () => Promise<undefined>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { UpdatePaymentSource, PaymentMethodState, SetPaymentMethodErrors, SetPaymentSource,
|
|
2
|
+
import { UpdatePaymentSource, PaymentMethodState, SetPaymentMethodErrors, SetPaymentSource, setPaymentMethod, DestroyPaymentSource, SetPaymentRef, setLoading } from '../reducers/PaymentMethodReducer';
|
|
3
3
|
type DefaultContext = {
|
|
4
4
|
setPaymentMethodErrors: SetPaymentMethodErrors;
|
|
5
|
-
setPaymentMethod:
|
|
5
|
+
setPaymentMethod: typeof setPaymentMethod;
|
|
6
6
|
setPaymentSource: SetPaymentSource;
|
|
7
7
|
setPaymentRef: SetPaymentRef;
|
|
8
8
|
destroyPaymentSource: DestroyPaymentSource;
|
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
import { RefObject } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import { PaymentSourceType } from '../reducers/PaymentMethodReducer';
|
|
2
|
+
import { PlaceOrderState, setPlaceOrder, setPlaceOrderErrors } from '../reducers/PlaceOrderReducer';
|
|
4
3
|
type DefaultContext = {
|
|
5
|
-
setPlaceOrderErrors?:
|
|
6
|
-
setPlaceOrder?:
|
|
7
|
-
paymentSource?: PaymentSourceType;
|
|
8
|
-
options?: PlaceOrderOptions;
|
|
9
|
-
}) => Promise<{
|
|
10
|
-
placed: boolean;
|
|
11
|
-
}>;
|
|
4
|
+
setPlaceOrderErrors?: typeof setPlaceOrderErrors;
|
|
5
|
+
setPlaceOrder?: typeof setPlaceOrder;
|
|
12
6
|
placeOrderPermitted?: () => void;
|
|
13
7
|
setButtonRef?: (ref: RefObject<HTMLButtonElement>) => void;
|
|
14
8
|
} & PlaceOrderState;
|
|
15
9
|
export declare const defaultPlaceOrderContext: {
|
|
16
|
-
setPlaceOrderErrors:
|
|
10
|
+
setPlaceOrderErrors: typeof setPlaceOrderErrors;
|
|
17
11
|
};
|
|
18
12
|
declare const PlaceOrderContext: import("react").Context<DefaultContext>;
|
|
19
13
|
export default PlaceOrderContext;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { ShipmentState, SetShipmentErrors } from '../reducers/ShipmentReducer';
|
|
2
|
+
import { ShipmentState, SetShipmentErrors, setShippingMethod } from '../reducers/ShipmentReducer';
|
|
3
3
|
type DefaultContext = {
|
|
4
4
|
setShipmentErrors: SetShipmentErrors;
|
|
5
|
-
setShippingMethod
|
|
5
|
+
setShippingMethod?: (shipmentId: string, shippingMethodId: string) => ReturnType<typeof setShippingMethod>;
|
|
6
6
|
} & ShipmentState;
|
|
7
7
|
export declare const defaultShipmentContext: {
|
|
8
8
|
setShipmentErrors: SetShipmentErrors;
|
|
9
|
-
setShippingMethod: () => Promise<void>;
|
|
10
9
|
};
|
|
11
10
|
declare const ShipmentContext: import("react").Context<DefaultContext>;
|
|
12
11
|
export default ShipmentContext;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.defaultShipmentContext=void 0;const react_1=require("react"),ShipmentReducer_1=require("../reducers/ShipmentReducer");exports.defaultShipmentContext={setShipmentErrors:ShipmentReducer_1.setShipmentErrors
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.defaultShipmentContext=void 0;const react_1=require("react"),ShipmentReducer_1=require("../reducers/ShipmentReducer");exports.defaultShipmentContext={setShipmentErrors:ShipmentReducer_1.setShipmentErrors};const ShipmentContext=(0,react_1.createContext)(exports.defaultShipmentContext);exports.default=ShipmentContext;
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -98,6 +98,4 @@ export * from './components/skus/Skus';
|
|
|
98
98
|
export * from './components/skus/SkusContainer';
|
|
99
99
|
export * from './components/stock_transfers/StockTransfer';
|
|
100
100
|
export * from './components/stock_transfers/StockTransferField';
|
|
101
|
-
export type { CustomerCardsType } from './components/utils/PaymentCardsTemplate';
|
|
102
|
-
export type { ShippingMethodRadioButtonType, ShippingMethodRadioButtonOnChangeType } from './components/shipping_methods/ShippingMethodRadioButton';
|
|
103
101
|
export * from './typings/errors';
|
|
@@ -38,7 +38,11 @@ export interface SetAddressParams<V extends AddressSchema> {
|
|
|
38
38
|
dispatch?: Dispatch<AddressAction>;
|
|
39
39
|
}
|
|
40
40
|
export type SetAddress = <V extends AddressSchema>(params: SetAddressParams<V>) => void;
|
|
41
|
-
export
|
|
41
|
+
export declare const setAddressErrors: SetAddressErrors;
|
|
42
|
+
export declare const setAddress: SetAddress;
|
|
43
|
+
type SetCloneAddress = (id: string, resource: AddressResource, dispatch: Dispatch<AddressAction>) => void;
|
|
44
|
+
export declare const setCloneAddress: SetCloneAddress;
|
|
45
|
+
interface TSaveAddressesParams {
|
|
42
46
|
orderId?: string;
|
|
43
47
|
order?: Order | null;
|
|
44
48
|
updateOrder?: typeof updateOrder;
|
|
@@ -46,11 +50,11 @@ export type SaveAddresses = (params: {
|
|
|
46
50
|
state: AddressState;
|
|
47
51
|
dispatch: Dispatch<AddressAction>;
|
|
48
52
|
getCustomerAddresses?: () => Promise<void>;
|
|
49
|
-
}
|
|
50
|
-
export declare
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
}
|
|
54
|
+
export declare function saveAddresses({ config, updateOrder, order, state }: TSaveAddressesParams): Promise<{
|
|
55
|
+
success: boolean;
|
|
56
|
+
order?: Order;
|
|
57
|
+
error?: unknown;
|
|
58
|
+
}>;
|
|
55
59
|
declare const addressReducer: (state: AddressState, reducer: AddressAction) => AddressState;
|
|
56
60
|
export default addressReducer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.saveAddresses=exports.setCloneAddress=exports.setAddress=exports.setAddressErrors=exports.addressInitialState=exports.addressFields=void 0;const tslib_1=require("tslib"),baseReducer_1=tslib_1.__importDefault(require("../utils/baseReducer")),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),getSdk_1=tslib_1.__importDefault(require("../utils/getSdk")),camelCase_1=tslib_1.__importDefault(require("lodash/camelCase"));exports.addressFields=["city","company","country_code","first_name","last_name","line_1","line_2","phone","state_code","zip_code"],exports.addressInitialState={errors:[]};const setAddressErrors=({errors,dispatch,currentErrors=[],resource})=>{const billingErrors=resource==="billing_address"?errors.filter(e=>e.resource===resource):currentErrors.filter(e=>e.resource==="billing_address"),shippingErrors=resource==="shipping_address"?errors.filter(e=>e.resource===resource):currentErrors.filter(e=>e.resource==="shipping_address"),finalErrors=[...billingErrors,...shippingErrors];dispatch&&dispatch({type:"setErrors",payload:{errors:finalErrors}})};exports.setAddressErrors=setAddressErrors;const setAddress=({values,resource,dispatch})=>{dispatch&&dispatch({type:"setAddress",payload:{[`${resource}`]:values}})};exports.setAddress=setAddress;const setCloneAddress=(id,resource,dispatch)=>{dispatch({type:"setCloneAddress",payload:{[`${(0,camelCase_1.default)(resource)}Id`]:id}})};exports.setCloneAddress=setCloneAddress;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.saveAddresses=exports.setCloneAddress=exports.setAddress=exports.setAddressErrors=exports.addressInitialState=exports.addressFields=void 0;const tslib_1=require("tslib"),baseReducer_1=tslib_1.__importDefault(require("../utils/baseReducer")),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),getSdk_1=tslib_1.__importDefault(require("../utils/getSdk")),camelCase_1=tslib_1.__importDefault(require("lodash/camelCase"));exports.addressFields=["city","company","country_code","first_name","last_name","line_1","line_2","phone","state_code","zip_code"],exports.addressInitialState={errors:[]};const setAddressErrors=({errors,dispatch,currentErrors=[],resource})=>{const billingErrors=resource==="billing_address"?errors.filter(e=>e.resource===resource):currentErrors.filter(e=>e.resource==="billing_address"),shippingErrors=resource==="shipping_address"?errors.filter(e=>e.resource===resource):currentErrors.filter(e=>e.resource==="shipping_address"),finalErrors=[...billingErrors,...shippingErrors];dispatch&&dispatch({type:"setErrors",payload:{errors:finalErrors}})};exports.setAddressErrors=setAddressErrors;const setAddress=({values,resource,dispatch})=>{dispatch&&dispatch({type:"setAddress",payload:{[`${resource}`]:values}})};exports.setAddress=setAddress;const setCloneAddress=(id,resource,dispatch)=>{dispatch({type:"setCloneAddress",payload:{[`${(0,camelCase_1.default)(resource)}Id`]:id}})};exports.setCloneAddress=setCloneAddress;async function saveAddresses({config,updateOrder,order,state}){const{shipToDifferentAddress,billing_address:billingAddress,shipping_address:shippingAddress,billingAddressId,shippingAddressId}=state;try{const sdk=(0,getSdk_1.default)(config);if(order){const currentBillingAddressRef=order?.billing_address?.reference,orderAttributes={id:order?.id,_billing_address_clone_id:billingAddressId,_shipping_address_clone_id:billingAddressId};if(currentBillingAddressRef===billingAddressId&&(orderAttributes._billing_address_clone_id=order?.billing_address?.id,orderAttributes._shipping_address_clone_id=order?.shipping_address?.id),!(0,isEmpty_1.default)(billingAddress)&&billingAddress){delete orderAttributes._billing_address_clone_id,delete orderAttributes._shipping_address_clone_id,orderAttributes._shipping_address_same_as_billing=!0;const address=await sdk.addresses.create(billingAddress);orderAttributes.billing_address=sdk.addresses.relationship(address.id)}if(shipToDifferentAddress&&(delete orderAttributes._shipping_address_same_as_billing,shippingAddressId&&(orderAttributes._shipping_address_clone_id=shippingAddressId),!(0,isEmpty_1.default)(shippingAddress)&&shippingAddress)){delete orderAttributes._shipping_address_clone_id;const address=await sdk.addresses.create(shippingAddress);orderAttributes.shipping_address=sdk.addresses.relationship(address.id)}if(!(0,isEmpty_1.default)(orderAttributes)&&updateOrder)return{success:!0,order:(await updateOrder({id:order.id,attributes:orderAttributes}))?.order}}return{success:!1}}catch(error){return console.error(error),{success:!1,error}}}exports.saveAddresses=saveAddresses;const type=["setErrors","setAddress","setShipToDifferentAddress","setCloneAddress","cleanup"],addressReducer=(state,reducer)=>(0,baseReducer_1.default)(state,reducer,type);exports.default=addressReducer;
|
|
@@ -58,6 +58,7 @@ export interface UpdateOrderArgs {
|
|
|
58
58
|
export declare function updateOrder({ id, attributes, dispatch, config, include, state }: UpdateOrderArgs): Promise<{
|
|
59
59
|
success: boolean;
|
|
60
60
|
error?: unknown;
|
|
61
|
+
order?: Order;
|
|
61
62
|
}>;
|
|
62
63
|
export declare const setOrder: (order: Order, dispatch?: Dispatch<OrderActions>) => void;
|
|
63
64
|
export interface AddResourceToInclude {
|
|
@@ -118,7 +119,7 @@ export type SaveAddressToCustomerAddressBook = (params: {
|
|
|
118
119
|
value: boolean;
|
|
119
120
|
}) => void;
|
|
120
121
|
export declare const saveAddressToCustomerAddressBook: SaveAddressToCustomerAddressBook;
|
|
121
|
-
|
|
122
|
+
interface TSetGiftCardOrCouponCodeParams {
|
|
122
123
|
code: string;
|
|
123
124
|
codeType: OrderCodeType;
|
|
124
125
|
dispatch?: Dispatch<OrderActions>;
|
|
@@ -126,23 +127,25 @@ export type SetGiftCardOrCouponCode = (args: {
|
|
|
126
127
|
order?: Order;
|
|
127
128
|
include?: string[];
|
|
128
129
|
state?: OrderState;
|
|
129
|
-
}
|
|
130
|
+
}
|
|
131
|
+
export declare function setGiftCardOrCouponCode({ code, codeType, dispatch, config, order, include, state }: TSetGiftCardOrCouponCodeParams): Promise<{
|
|
130
132
|
success: boolean;
|
|
133
|
+
order?: Order;
|
|
131
134
|
}>;
|
|
132
|
-
export declare const setGiftCardOrCouponCode: SetGiftCardOrCouponCode;
|
|
133
135
|
export type CodeType = 'coupon' | 'gift_card' | 'gift_card_or_coupon';
|
|
134
136
|
export type OrderCodeType = `${CodeType}_code`;
|
|
135
|
-
|
|
137
|
+
interface TRemoveGiftCardOrCouponCodeParams {
|
|
136
138
|
codeType: OrderCodeType;
|
|
137
139
|
dispatch?: Dispatch<OrderActions>;
|
|
138
140
|
config?: CommerceLayerConfig;
|
|
139
141
|
order?: Order;
|
|
140
142
|
include?: string[];
|
|
141
143
|
state?: OrderState;
|
|
142
|
-
}
|
|
144
|
+
}
|
|
145
|
+
export declare function removeGiftCardOrCouponCode({ codeType, dispatch, config, order, include, state }: TRemoveGiftCardOrCouponCodeParams): Promise<{
|
|
143
146
|
success: boolean;
|
|
147
|
+
order?: Order;
|
|
144
148
|
}>;
|
|
145
|
-
export declare const removeGiftCardOrCouponCode: RemoveGiftCardOrCouponCode;
|
|
146
149
|
export declare const orderInitialState: Partial<OrderState>;
|
|
147
150
|
declare const orderReducer: (state: OrderState, reducer: OrderActions) => OrderState;
|
|
148
151
|
export default orderReducer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.orderInitialState=exports.removeGiftCardOrCouponCode=exports.setGiftCardOrCouponCode=exports.saveAddressToCustomerAddressBook=exports.setOrderErrors=exports.unsetOrderState=exports.addToCart=exports.addResourceToInclude=exports.setOrder=exports.updateOrder=exports.getApiOrder=exports.createOrder=void 0;const tslib_1=require("tslib"),localStorage_1=require("../utils/localStorage"),baseReducer_1=tslib_1.__importDefault(require("../utils/baseReducer")),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),getSdk_1=tslib_1.__importDefault(require("../utils/getSdk")),getErrors_1=tslib_1.__importStar(require("../utils/getErrors")),organization_1=tslib_1.__importDefault(require("../utils/organization")),actionType=["setLoading","setOrderId","setOrder","setSingleQuantity","setCurrentSkuCodes","setCurrentSkuPrices","setErrors","setCurrentItem","setSaveAddressToCustomerAddressBook","setIncludesResource"],createOrder=async params=>{if(params){const{persistKey,state,dispatch,config,orderMetadata:metadata,orderAttributes={},setLocalOrder}=params;if(state?.orderId)return state.orderId;const sdk=(0,getSdk_1.default)(config);try{const o=await sdk?.orders.create({metadata,...orderAttributes});return dispatch&&dispatch({type:"setOrderId",payload:{orderId:o?.id}}),persistKey&&setLocalOrder&&setLocalOrder(persistKey,o.id),o.id}catch(error){const errors=(0,getErrors_1.default)(error,"orders");console.error("Create order",errors),dispatch&&(0,getErrors_1.setErrors)({currentErrors:state?.errors,newErrors:errors,dispatch})}}return""};exports.createOrder=createOrder;const getApiOrder=async params=>{const{id,dispatch,config,clearWhenPlaced,persistKey,deleteLocalOrder,state}=params,sdk=(0,getSdk_1.default)(config);try{const options={};state?.include&&state.include.length>0&&(options.include=state.include);const order=await sdk.orders.retrieve(id,options);return clearWhenPlaced&&order.editable===!1?(persistKey&&deleteLocalOrder&&deleteLocalOrder(persistKey),dispatch&&dispatch({type:"setOrder",payload:{order:void 0,orderId:""}})):dispatch&&dispatch({type:"setOrder",payload:{order,orderId:order.id}}),order}catch(error){const errors=(0,getErrors_1.default)(error,"orders");console.error("Retrieve order",errors),dispatch&&(0,getErrors_1.setErrors)({currentErrors:state?.errors,newErrors:errors,dispatch});return}};exports.getApiOrder=getApiOrder;async function updateOrder({id,attributes,dispatch,config,include,state}){const sdk=(0,getSdk_1.default)(config);try{const resource={...attributes,id};await sdk.orders.update(resource,{include});const order=await(0,exports.getApiOrder)({id,config,dispatch,state});return dispatch&&order&&dispatch({type:"setOrder",payload:{order}}),{success:!0}}catch(error){const errors=(0,getErrors_1.default)(error,"orders");return dispatch&&(setOrderErrors({errors,dispatch}),dispatch({type:"setErrors",payload:{errors}})),{success:!1,error}}}exports.updateOrder=updateOrder;const setOrder=(order,dispatch)=>{dispatch&&dispatch({type:"setOrder",payload:{order}})};exports.setOrder=setOrder;function addResourceToInclude({resourcesIncluded=[],dispatch,newResource,newResourceLoaded,resourceIncludedLoaded}){const payload={include:void 0,includeLoaded:void 0};if(newResource){const resources=typeof newResource=="string"?[newResource]:newResource;payload.include=[...new Set([...resourcesIncluded,...resources])],resources.forEach(resource=>{const includeLoaded={...payload.includeLoaded,[resource]:!0};payload.includeLoaded=includeLoaded})}else delete payload.include;const payloadIncludeLoaded={...resourceIncludedLoaded,...newResourceLoaded,...payload.includeLoaded&&payload.includeLoaded};payload.includeLoaded=payloadIncludeLoaded,dispatch&&dispatch({type:"setIncludesResource",payload:{...payload,withoutIncludes:!1}})}exports.addResourceToInclude=addResourceToInclude;async function addToCart(params){const{skuCode,bundleCode,quantity,config,dispatch,lineItem,state,errors=[],buyNowMode,checkoutUrl,lineItemOption}=params;try{if(config){const sdk=(0,getSdk_1.default)(config),id=await(0,exports.createOrder)(params);if(id){const order=sdk.orders.relationship(id),name=lineItem?.name,imageUrl=lineItem?.imageUrl;if(buyNowMode)if(state?.order?.line_items)await Promise.all(state?.order?.line_items.map(async lineItem2=>{await sdk.line_items.delete(lineItem2.id)}));else{const{line_items:lineItems}=await sdk.orders.retrieve(id,{fields:["line_items"],include:["line_items"]});lineItems&&lineItems?.length>0&&await Promise.all(lineItems.map(async lineItem2=>{await sdk.line_items.delete(lineItem2.id)}))}const attrs={order,sku_code:skuCode,name,image_url:imageUrl,quantity:quantity??1,_update_quantity:!0,bundle_code:bundleCode},newLineItem=await sdk.line_items.create(attrs);if(lineItemOption!=null){const{skuOptionId,options,quantity:quantity2}=lineItemOption,skuOption=sdk.sku_options.relationship(skuOptionId),lineItemRel=sdk.line_items.relationship(newLineItem.id),lineItemOptionsAttributes={quantity:quantity2??1,options,sku_option:skuOption,line_item:lineItemRel};await sdk.line_item_options.create(lineItemOptionsAttributes),await(0,exports.getApiOrder)({id,...params})}else await(0,exports.getApiOrder)({id,...params,state});if(!(0,isEmpty_1.default)(errors)&&dispatch&&dispatch({type:"setErrors",payload:{errors:[]}}),buyNowMode){const{organization}=(0,organization_1.default)(config.endpoint??""),params2=`${id}?accessToken=${config.accessToken??""}`,redirectUrl=checkoutUrl?`${checkoutUrl}/${params2}`:`https://${organization}.checkout.commercelayer.app/${params2}`;location.href=redirectUrl}return{success:!0,orderId:id}}}return{success:!1}}catch(error){const errors2=(0,getErrors_1.default)(error,"orders");return console.error("Add to cart",errors2),dispatch&&(0,getErrors_1.setErrors)({currentErrors:state?.errors,newErrors:errors2,dispatch}),{success:!1}}}exports.addToCart=addToCart;const unsetOrderState=dispatch=>{dispatch({type:"setOrderId",payload:{orderId:""}}),dispatch({type:"setOrder",payload:{order:void 0}})};exports.unsetOrderState=unsetOrderState;function setOrderErrors({dispatch,errors=[]}){return dispatch&&dispatch({type:"setErrors",payload:{errors}}),{success:!1}}exports.setOrderErrors=setOrderErrors;const saveAddressToCustomerAddressBook=({type,value,dispatch})=>{const k=`_save_${type}_to_customer_address_book`,v=`${value.toString()}`;(0,localStorage_1.setCustomerOrderParam)(k,v),dispatch&&dispatch({type:"setSaveAddressToCustomerAddressBook",payload:{[k]:v}})};exports.saveAddressToCustomerAddressBook=saveAddressToCustomerAddressBook;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.orderInitialState=exports.removeGiftCardOrCouponCode=exports.setGiftCardOrCouponCode=exports.saveAddressToCustomerAddressBook=exports.setOrderErrors=exports.unsetOrderState=exports.addToCart=exports.addResourceToInclude=exports.setOrder=exports.updateOrder=exports.getApiOrder=exports.createOrder=void 0;const tslib_1=require("tslib"),localStorage_1=require("../utils/localStorage"),baseReducer_1=tslib_1.__importDefault(require("../utils/baseReducer")),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),getSdk_1=tslib_1.__importDefault(require("../utils/getSdk")),getErrors_1=tslib_1.__importStar(require("../utils/getErrors")),organization_1=tslib_1.__importDefault(require("../utils/organization")),actionType=["setLoading","setOrderId","setOrder","setSingleQuantity","setCurrentSkuCodes","setCurrentSkuPrices","setErrors","setCurrentItem","setSaveAddressToCustomerAddressBook","setIncludesResource"],createOrder=async params=>{if(params){const{persistKey,state,dispatch,config,orderMetadata:metadata,orderAttributes={},setLocalOrder}=params;if(state?.orderId)return state.orderId;const sdk=(0,getSdk_1.default)(config);try{const o=await sdk?.orders.create({metadata,...orderAttributes});return dispatch&&dispatch({type:"setOrderId",payload:{orderId:o?.id}}),persistKey&&setLocalOrder&&setLocalOrder(persistKey,o.id),o.id}catch(error){const errors=(0,getErrors_1.default)(error,"orders");console.error("Create order",errors),dispatch&&(0,getErrors_1.setErrors)({currentErrors:state?.errors,newErrors:errors,dispatch})}}return""};exports.createOrder=createOrder;const getApiOrder=async params=>{const{id,dispatch,config,clearWhenPlaced,persistKey,deleteLocalOrder,state}=params,sdk=(0,getSdk_1.default)(config);try{const options={};state?.include&&state.include.length>0&&(options.include=state.include);const order=await sdk.orders.retrieve(id,options);return clearWhenPlaced&&order.editable===!1?(persistKey&&deleteLocalOrder&&deleteLocalOrder(persistKey),dispatch&&dispatch({type:"setOrder",payload:{order:void 0,orderId:""}})):dispatch&&dispatch({type:"setOrder",payload:{order,orderId:order.id}}),order}catch(error){const errors=(0,getErrors_1.default)(error,"orders");console.error("Retrieve order",errors),dispatch&&(0,getErrors_1.setErrors)({currentErrors:state?.errors,newErrors:errors,dispatch});return}};exports.getApiOrder=getApiOrder;async function updateOrder({id,attributes,dispatch,config,include,state}){const sdk=(0,getSdk_1.default)(config);try{const resource={...attributes,id};await sdk.orders.update(resource,{include});const order=await(0,exports.getApiOrder)({id,config,dispatch,state});return dispatch&&order&&dispatch({type:"setOrder",payload:{order}}),{success:!0,order}}catch(error){const errors=(0,getErrors_1.default)(error,"orders");return dispatch&&(setOrderErrors({errors,dispatch}),dispatch({type:"setErrors",payload:{errors}})),{success:!1,error}}}exports.updateOrder=updateOrder;const setOrder=(order,dispatch)=>{dispatch&&dispatch({type:"setOrder",payload:{order}})};exports.setOrder=setOrder;function addResourceToInclude({resourcesIncluded=[],dispatch,newResource,newResourceLoaded,resourceIncludedLoaded}){const payload={include:void 0,includeLoaded:void 0};if(newResource){const resources=typeof newResource=="string"?[newResource]:newResource;payload.include=[...new Set([...resourcesIncluded,...resources])],resources.forEach(resource=>{const includeLoaded={...payload.includeLoaded,[resource]:!0};payload.includeLoaded=includeLoaded})}else delete payload.include;const payloadIncludeLoaded={...resourceIncludedLoaded,...newResourceLoaded,...payload.includeLoaded&&payload.includeLoaded};payload.includeLoaded=payloadIncludeLoaded,dispatch&&dispatch({type:"setIncludesResource",payload:{...payload,withoutIncludes:!1}})}exports.addResourceToInclude=addResourceToInclude;async function addToCart(params){const{skuCode,bundleCode,quantity,config,dispatch,lineItem,state,errors=[],buyNowMode,checkoutUrl,lineItemOption}=params;try{if(config){const sdk=(0,getSdk_1.default)(config),id=await(0,exports.createOrder)(params);if(id){const order=sdk.orders.relationship(id),name=lineItem?.name,imageUrl=lineItem?.imageUrl;if(buyNowMode)if(state?.order?.line_items)await Promise.all(state?.order?.line_items.map(async lineItem2=>{await sdk.line_items.delete(lineItem2.id)}));else{const{line_items:lineItems}=await sdk.orders.retrieve(id,{fields:["line_items"],include:["line_items"]});lineItems&&lineItems?.length>0&&await Promise.all(lineItems.map(async lineItem2=>{await sdk.line_items.delete(lineItem2.id)}))}const attrs={order,sku_code:skuCode,name,image_url:imageUrl,quantity:quantity??1,_update_quantity:!0,bundle_code:bundleCode},newLineItem=await sdk.line_items.create(attrs);if(lineItemOption!=null){const{skuOptionId,options,quantity:quantity2}=lineItemOption,skuOption=sdk.sku_options.relationship(skuOptionId),lineItemRel=sdk.line_items.relationship(newLineItem.id),lineItemOptionsAttributes={quantity:quantity2??1,options,sku_option:skuOption,line_item:lineItemRel};await sdk.line_item_options.create(lineItemOptionsAttributes),await(0,exports.getApiOrder)({id,...params})}else await(0,exports.getApiOrder)({id,...params,state});if(!(0,isEmpty_1.default)(errors)&&dispatch&&dispatch({type:"setErrors",payload:{errors:[]}}),buyNowMode){const{organization}=(0,organization_1.default)(config.endpoint??""),params2=`${id}?accessToken=${config.accessToken??""}`,redirectUrl=checkoutUrl?`${checkoutUrl}/${params2}`:`https://${organization}.checkout.commercelayer.app/${params2}`;location.href=redirectUrl}return{success:!0,orderId:id}}}return{success:!1}}catch(error){const errors2=(0,getErrors_1.default)(error,"orders");return console.error("Add to cart",errors2),dispatch&&(0,getErrors_1.setErrors)({currentErrors:state?.errors,newErrors:errors2,dispatch}),{success:!1}}}exports.addToCart=addToCart;const unsetOrderState=dispatch=>{dispatch({type:"setOrderId",payload:{orderId:""}}),dispatch({type:"setOrder",payload:{order:void 0}})};exports.unsetOrderState=unsetOrderState;function setOrderErrors({dispatch,errors=[]}){return dispatch&&dispatch({type:"setErrors",payload:{errors}}),{success:!1}}exports.setOrderErrors=setOrderErrors;const saveAddressToCustomerAddressBook=({type,value,dispatch})=>{const k=`_save_${type}_to_customer_address_book`,v=`${value.toString()}`;(0,localStorage_1.setCustomerOrderParam)(k,v),dispatch&&dispatch({type:"setSaveAddressToCustomerAddressBook",payload:{[k]:v}})};exports.saveAddressToCustomerAddressBook=saveAddressToCustomerAddressBook;async function setGiftCardOrCouponCode({code,codeType,dispatch,config,order,include,state}){try{if(config&&order&&code&&dispatch){const attributes={[codeType]:code},{success,order:currentOrder,error}=await updateOrder({id:order.id,attributes,config,include,dispatch,state});if(!success)throw error;return dispatch({type:"setErrors",payload:{errors:[]}}),{success,order:currentOrder}}return{success:!1}}catch(error){const errors=(0,getErrors_1.default)(error,"orders",codeType);return dispatch&&setOrderErrors({errors,dispatch}),{success:!1}}}exports.setGiftCardOrCouponCode=setGiftCardOrCouponCode;async function removeGiftCardOrCouponCode({codeType,dispatch,config,order,include,state}){try{if(config&&order&&dispatch){const attributes={[codeType]:""},orderUpdated=await updateOrder({id:order.id,attributes,config,include,dispatch,state});return dispatch({type:"setErrors",payload:{errors:[]}}),{success:!0,order:orderUpdated?.order}}return{success:!1}}catch(error){const errors=(0,getErrors_1.default)(error,"orders",codeType);return console.error("Remove gift card o coupon code",errors),dispatch&&setOrderErrors({errors,dispatch}),{success:!1}}}exports.removeGiftCardOrCouponCode=removeGiftCardOrCouponCode,exports.orderInitialState={loading:!0,orderId:"",order:void 0,errors:[],include:void 0,withoutIncludes:!0};const orderReducer=(state,reducer)=>(0,baseReducer_1.default)(state,reducer,actionType);exports.default=orderReducer;
|
|
@@ -91,7 +91,7 @@ export declare const getPaymentMethods: GetPaymentMethods;
|
|
|
91
91
|
export type PaymentResource = keyof PaymentSourceObject;
|
|
92
92
|
export type PaymentResourceKey = 'braintreePayment' | 'stripePayment' | 'klarnaPayment' | 'wireTransfer' | 'paypalPayment' | 'adyenPayment' | 'checkoutComPayment';
|
|
93
93
|
export type SDKPaymentResource = 'AdyenPayment' | 'BraintreePayment' | 'ExternalPayment' | 'PaypalPayment' | 'StripePayment' | 'WireTransfer' | 'CheckoutComPayment';
|
|
94
|
-
|
|
94
|
+
interface TSetPaymentMethodParams {
|
|
95
95
|
config?: CommerceLayerConfig;
|
|
96
96
|
dispatch?: Dispatch<PaymentMethodAction>;
|
|
97
97
|
updateOrder?: typeof updateOrder;
|
|
@@ -101,8 +101,11 @@ export type SetPaymentMethod = (args: {
|
|
|
101
101
|
order?: Order;
|
|
102
102
|
paymentMethodId: string;
|
|
103
103
|
paymentResource?: PaymentResource;
|
|
104
|
-
}
|
|
105
|
-
export declare
|
|
104
|
+
}
|
|
105
|
+
export declare function setPaymentMethod({ config, dispatch, order, paymentMethodId, updateOrder, setOrderErrors, paymentResource }: TSetPaymentMethodParams): Promise<{
|
|
106
|
+
success: boolean;
|
|
107
|
+
order?: Order;
|
|
108
|
+
}>;
|
|
106
109
|
type PaymentSourceTypes = (StripePayment & WireTransfer) | (StripePayment | WireTransfer);
|
|
107
110
|
export type SetPaymentSourceResponse = {
|
|
108
111
|
order: Order;
|