@commercelayer/react-components 4.7.0 → 4.8.0-beta.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/lib/cjs/components/addresses/AddressInput.d.ts +7 -0
- package/lib/cjs/components/addresses/AddressesContainer.d.ts +10 -0
- package/lib/cjs/components/addresses/AddressesContainer.js +1 -1
- package/lib/cjs/components/addresses/SaveAddressesButton.js +1 -1
- package/lib/cjs/context/BillingAddressFormContext.d.ts +1 -1
- package/lib/cjs/reducers/AddressReducer.d.ts +2 -1
- package/lib/cjs/reducers/AddressReducer.js +1 -1
- package/lib/cjs/typings/index.d.ts +1 -1
- package/lib/cjs/utils/addressesManager.d.ts +37 -7
- package/lib/cjs/utils/addressesManager.js +1 -1
- package/lib/esm/components/addresses/AddressInput.d.ts +7 -0
- package/lib/esm/components/addresses/AddressesContainer.d.ts +10 -0
- package/lib/esm/components/addresses/AddressesContainer.js +1 -1
- package/lib/esm/components/addresses/SaveAddressesButton.js +1 -1
- package/lib/esm/context/BillingAddressFormContext.d.ts +1 -1
- package/lib/esm/reducers/AddressReducer.d.ts +2 -1
- package/lib/esm/reducers/AddressReducer.js +1 -1
- package/lib/esm/typings/index.d.ts +1 -1
- package/lib/esm/utils/addressesManager.d.ts +37 -7
- package/lib/esm/utils/addressesManager.js +1 -1
- package/package.json +1 -1
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { type BaseInputComponentProps, type AddressInputName } from '../../typings/index';
|
|
2
2
|
import { type AddressValuesKeys } from '../../context/BillingAddressFormContext';
|
|
3
3
|
type Props = {
|
|
4
|
+
/**
|
|
5
|
+
* The name of the input.
|
|
6
|
+
*/
|
|
4
7
|
name: Extract<AddressValuesKeys, AddressInputName>;
|
|
8
|
+
/**
|
|
9
|
+
* Used to add a custom validation rule.
|
|
10
|
+
*/
|
|
11
|
+
pattern?: RegExp | string;
|
|
5
12
|
} & Omit<BaseInputComponentProps, 'name'> & JSX.IntrinsicElements['input'] & Omit<JSX.IntrinsicElements['textarea'], 'children'>;
|
|
6
13
|
export declare function AddressInput(props: Props): JSX.Element | null;
|
|
7
14
|
export default AddressInput;
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import { type ReactNode } from 'react';
|
|
2
2
|
interface Props {
|
|
3
3
|
children: ReactNode;
|
|
4
|
+
/**
|
|
5
|
+
* If true, the shipping address will be considered. Default is false.
|
|
6
|
+
*/
|
|
4
7
|
shipToDifferentAddress?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* If true, the address will be considered a business address.
|
|
10
|
+
*/
|
|
5
11
|
isBusiness?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* If true, the shipping address will be considered as primary address. Default is false.
|
|
14
|
+
*/
|
|
15
|
+
invertAddresses?: boolean;
|
|
6
16
|
}
|
|
7
17
|
export declare function AddressesContainer(props: Props): JSX.Element;
|
|
8
18
|
export default AddressesContainer;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.AddressesContainer=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),AddressContext_1=tslib_1.__importStar(require("../../context/AddressContext")),react_1=require("react"),AddressReducer_1=tslib_1.__importStar(require("../../reducers/AddressReducer")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext"));function AddressesContainer(props){const{children,shipToDifferentAddress=!1,isBusiness}=props,[state,dispatch]=(0,react_1.useReducer)(AddressReducer_1.default,AddressReducer_1.addressInitialState),{order,orderId,updateOrder}=(0,react_1.useContext)(OrderContext_1.default),config=(0,react_1.useContext)(CommerceLayerContext_1.default);(0,react_1.useEffect)(()=>(dispatch({type:"setShipToDifferentAddress",payload:{shipToDifferentAddress,isBusiness}}),()=>{dispatch({type:"cleanup",payload:{}})}),[shipToDifferentAddress,isBusiness]);const contextValue=Object.assign(Object.assign({},state),{setAddressErrors:(errors,resource)=>{(0,AddressReducer_1.setAddressErrors)({errors,resource,dispatch,currentErrors:state.errors})},setAddress:params=>{AddressContext_1.defaultAddressContext.setAddress(Object.assign(Object.assign({},params),{dispatch}))},saveAddresses:customerEmail=>tslib_1.__awaiter(this,void 0,void 0,function*(){return yield(0,AddressReducer_1.saveAddresses)({config,dispatch,updateOrder,order,orderId,state,customerEmail})}),setCloneAddress:(id,resource)=>{(0,AddressReducer_1.setCloneAddress)(id,resource,dispatch)}});return(0,jsx_runtime_1.jsx)(AddressContext_1.default.Provider,{value:contextValue,children})}exports.AddressesContainer=AddressesContainer,exports.default=AddressesContainer;
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.AddressesContainer=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),AddressContext_1=tslib_1.__importStar(require("../../context/AddressContext")),react_1=require("react"),AddressReducer_1=tslib_1.__importStar(require("../../reducers/AddressReducer")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext"));function AddressesContainer(props){const{children,shipToDifferentAddress=!1,isBusiness,invertAddresses=!1}=props,[state,dispatch]=(0,react_1.useReducer)(AddressReducer_1.default,AddressReducer_1.addressInitialState),{order,orderId,updateOrder}=(0,react_1.useContext)(OrderContext_1.default),config=(0,react_1.useContext)(CommerceLayerContext_1.default);(0,react_1.useEffect)(()=>(dispatch({type:"setShipToDifferentAddress",payload:{shipToDifferentAddress,isBusiness,invertAddresses}}),()=>{dispatch({type:"cleanup",payload:{}})}),[shipToDifferentAddress,isBusiness,invertAddresses]);const contextValue=Object.assign(Object.assign({},state),{setAddressErrors:(errors,resource)=>{(0,AddressReducer_1.setAddressErrors)({errors,resource,dispatch,currentErrors:state.errors})},setAddress:params=>{AddressContext_1.defaultAddressContext.setAddress(Object.assign(Object.assign({},params),{dispatch}))},saveAddresses:customerEmail=>tslib_1.__awaiter(this,void 0,void 0,function*(){return yield(0,AddressReducer_1.saveAddresses)({config,dispatch,updateOrder,order,orderId,state,customerEmail})}),setCloneAddress:(id,resource)=>{(0,AddressReducer_1.setCloneAddress)(id,resource,dispatch)}});return(0,jsx_runtime_1.jsx)(AddressContext_1.default.Provider,{value:contextValue,children})}exports.AddressesContainer=AddressesContainer,exports.default=AddressesContainer;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"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}=props,p=tslib_1.__rest(props,["children","label","resource","disabled","addressId","onClick"]),{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),{customerEmail:email,addresses,isGuest,createCustomerAddress}=(0,react_1.useContext)(CustomerContext_1.default),[forceDisable,setForceDisable]=(0,react_1.useState)(disabled);let customerEmail=(isGuest===!0||typeof isGuest>"u")&&!order?.customer_email;email!=null&&email!==""&&(customerEmail=!1);const
|
|
2
|
+
"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}=props,p=tslib_1.__rest(props,["children","label","resource","disabled","addressId","onClick"]),{errors,billing_address:billingAddress,shipToDifferentAddress,shipping_address:shippingAddress,saveAddresses,billingAddressId,shippingAddressId,invertAddresses}=(0,react_1.useContext)(AddressContext_1.default);console.log("invertAddresses",invertAddresses);const{order}=(0,react_1.useContext)(OrderContext_1.default),{customerEmail:email,addresses,isGuest,createCustomerAddress}=(0,react_1.useContext)(CustomerContext_1.default),[forceDisable,setForceDisable]=(0,react_1.useState)(disabled);let customerEmail=(isGuest===!0||typeof isGuest>"u")&&!order?.customer_email;email!=null&&email!==""&&(customerEmail=!1);const{billingDisable,shippingDisable}=(0,addressesManager_1.addressesController)({invertAddresses,billing_address:billingAddress,shipping_address:shippingAddress,shippingAddressId,billingAddressId,errors}),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=()=>tslib_1.__awaiter(this,void 0,void 0,function*(){if(errors&&Object.keys(errors).length===0&&!disable){let response={success:!1};if(setForceDisable(!0),order&&saveAddresses!=null)response=yield saveAddresses(email);else if(createCustomerAddress&&billingAddress){const address=Object.assign({},billingAddress);addressId&&(address.id=addressId),createCustomerAddress(address),response={success:!0}}setForceDisable(!1),onClick&&response.success&&onClick(response)}}),parentProps=Object.assign(Object.assign({},p),{label,resource,handleClick,disabled:disable});return children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("button",Object.assign({type:"button",disabled:disable||forceDisable,onClick:()=>{handleClick()}},p,{children:(0,isFunction_1.default)(label)?label():label}))}exports.SaveAddressesButton=SaveAddressesButton,exports.default=SaveAddressesButton;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import type { Address } from '@commercelayer/sdk';
|
|
3
|
-
export type AddressValuesKeys = `${keyof Address}` | `billing_address_${keyof Address}` | `shipping_address_${keyof Address}` | `billing_address_${`metadata_${string}`}`;
|
|
3
|
+
export type AddressValuesKeys = `${keyof Address}` | `billing_address_${keyof Address}` | `shipping_address_${keyof Address}` | `billing_address_${`metadata_${string}`}` | `shipping_address_${`metadata_${string}`}`;
|
|
4
4
|
export interface DefaultContextAddress {
|
|
5
5
|
validation?: void;
|
|
6
6
|
setValue?: (name: AddressValuesKeys, value: string | number | readonly string[]) => void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Dispatch } from 'react';
|
|
2
2
|
import { type BaseError } from '../typings/errors';
|
|
3
3
|
import { type CommerceLayerConfig } from '../context/CommerceLayerContext';
|
|
4
|
-
import type
|
|
4
|
+
import { type Address, type AddressCreate, type Order } from '@commercelayer/sdk';
|
|
5
5
|
import { type updateOrder } from './OrderReducer';
|
|
6
6
|
import { type TCustomerAddress } from './CustomerReducer';
|
|
7
7
|
import { type TResourceError } from '../components/errors/Errors';
|
|
@@ -19,6 +19,7 @@ export interface AddressActionPayload {
|
|
|
19
19
|
billingAddressId: string;
|
|
20
20
|
shippingAddressId: string;
|
|
21
21
|
isBusiness: boolean;
|
|
22
|
+
invertAddresses: boolean;
|
|
22
23
|
}
|
|
23
24
|
export type AddressState = Partial<AddressActionPayload>;
|
|
24
25
|
export interface AddressAction {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"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")),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;function saveAddresses({config,updateOrder,order,state,customerEmail}){var _a,_b,_c;return tslib_1.__awaiter(this,void 0,void 0,function*(){const{shipToDifferentAddress,billing_address:billingAddress,shipping_address:shippingAddress,billingAddressId,shippingAddressId}=state;try{const sdk=(0,getSdk_1.default)(config);if(order){const currentBillingAddressRef=(_a=order?.billing_address)===null||_a===void 0?void 0:_a.reference
|
|
2
|
+
"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")),getSdk_1=tslib_1.__importDefault(require("../utils/getSdk")),camelCase_1=tslib_1.__importDefault(require("lodash/camelCase")),addressesManager_1=require("../utils/addressesManager");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;function saveAddresses({config,updateOrder,order,state,customerEmail}){var _a,_b,_c;return tslib_1.__awaiter(this,void 0,void 0,function*(){const{shipToDifferentAddress,invertAddresses,billing_address:billingAddress,shipping_address:shippingAddress,billingAddressId,shippingAddressId}=state;try{const sdk=(0,getSdk_1.default)(config);if(order){let orderAttributes=null;if(invertAddresses)orderAttributes=yield(0,addressesManager_1.invertedAddressesHandler)({billingAddress,billingAddressId,customerEmail,order,shipToDifferentAddress,shippingAddress,shippingAddressId,sdk});else{const currentBillingAddressRef=(_a=order?.billing_address)===null||_a===void 0?void 0:_a.reference;if(orderAttributes={id:order?.id,_billing_address_clone_id:billingAddressId,_shipping_address_clone_id:billingAddressId,customer_email:customerEmail},currentBillingAddressRef===billingAddressId&&(orderAttributes._billing_address_clone_id=(_b=order?.billing_address)===null||_b===void 0?void 0:_b.id,orderAttributes._shipping_address_clone_id=(_c=order?.shipping_address)===null||_c===void 0?void 0:_c.id),billingAddress!=null&&Object.keys(billingAddress).length>0){delete orderAttributes._billing_address_clone_id,delete orderAttributes._shipping_address_clone_id,orderAttributes._shipping_address_same_as_billing=!0;const hasMetadata=Object.keys(billingAddress).filter(key=>!!key.startsWith("metadata_"));hasMetadata?.length>0&&hasMetadata.forEach(key=>{const metadataKey=key.replace("metadata_","");billingAddress.metadata=Object.assign(Object.assign({},billingAddress.metadata||{}),{[metadataKey]:billingAddress[key]}),delete billingAddress[key]});const address=yield 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),shippingAddress!=null&&Object.keys(shippingAddress).length>0)){delete orderAttributes._shipping_address_clone_id;const hasMetadata=Object.keys(shippingAddress).filter(key=>!!key.startsWith("metadata_"));hasMetadata?.length>0&&hasMetadata.forEach(key=>{const metadataKey=key.replace("metadata_","");shippingAddress.metadata=Object.assign(Object.assign({},shippingAddress.metadata||{}),{[metadataKey]:shippingAddress[key]}),delete shippingAddress[key]});const address=yield sdk.addresses.create(shippingAddress);orderAttributes.shipping_address=sdk.addresses.relationship(address.id)}}if(orderAttributes!=null&&updateOrder){const orderUpdated=yield updateOrder({id:order.id,attributes:orderAttributes});return{success:!0,order:orderUpdated?.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;
|
|
@@ -51,7 +51,7 @@ export interface BaseInputComponentProps {
|
|
|
51
51
|
placeholder?: string;
|
|
52
52
|
}
|
|
53
53
|
export type GiftCardInputName = 'balanceCents' | 'balanceMaxCents' | 'singleUse' | 'rechargeable' | 'imageUrl' | 'expiresAt' | 'referenceOrigin' | 'email' | 'firstName' | 'lastName' | 'reference';
|
|
54
|
-
export type AddressInputName = 'billing_address_city' | 'billing_address_company' | 'billing_address_first_name' | 'billing_address_email' | 'billing_address_last_name' | 'billing_address_line_1' | 'billing_address_line_2' | 'billing_address_phone' | 'billing_address_state_code' | 'billing_address_zip_code' | 'billing_address_billing_info' | 'billing_address_save_to_customer_book' | `billing_address_${`metadata_${string}`}` | 'shipping_address_city' | 'shipping_address_company' | 'shipping_address_email' | 'shipping_address_first_name' | 'shipping_address_last_name' | 'shipping_address_line_1' | 'shipping_address_line_2' | 'shipping_address_phone' | 'shipping_address_state_code' | 'shipping_address_zip_code' | 'shipping_address_save_to_customer_book'
|
|
54
|
+
export type AddressInputName = 'billing_address_city' | 'billing_address_company' | 'billing_address_first_name' | 'billing_address_email' | 'billing_address_last_name' | 'billing_address_line_1' | 'billing_address_line_2' | 'billing_address_phone' | 'billing_address_state_code' | 'billing_address_zip_code' | 'billing_address_billing_info' | 'billing_address_save_to_customer_book' | `billing_address_${`metadata_${string}`}` | 'shipping_address_city' | 'shipping_address_company' | 'shipping_address_email' | 'shipping_address_first_name' | 'shipping_address_last_name' | 'shipping_address_line_1' | 'shipping_address_line_2' | 'shipping_address_phone' | 'shipping_address_state_code' | 'shipping_address_zip_code' | 'shipping_address_save_to_customer_book' | 'shipping_address_billing_info' | `shipping_address_${`metadata_${string}`}`;
|
|
55
55
|
export type AddressCountrySelectName = 'billing_address_country_code' | 'shipping_address_country_code';
|
|
56
56
|
export type AddressStateSelectName = 'billing_address_state_code' | 'shipping_address_state_code';
|
|
57
57
|
export type LoaderType = string | JSX.Element;
|
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
import { type BaseError } from '../typings/errors';
|
|
2
|
-
import { type Address, type AddressCreate } from '@commercelayer/sdk';
|
|
2
|
+
import { type OrderUpdate, type Address, type AddressCreate, type Order, type CommerceLayerClient } from '@commercelayer/sdk';
|
|
3
3
|
import { type TCustomerAddress } from '../reducers/CustomerReducer';
|
|
4
|
-
|
|
4
|
+
interface BillingAddressControllerProps {
|
|
5
5
|
billing_address?: AddressCreate;
|
|
6
6
|
billingAddressId?: string;
|
|
7
7
|
errors?: BaseError[];
|
|
8
8
|
requiresBillingInfo?: boolean | null;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
invertAddresses?: boolean;
|
|
10
|
+
shippingDisable?: boolean;
|
|
11
|
+
shipToDifferentAddress?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare function billingAddressController({ billing_address, billingAddressId, errors, requiresBillingInfo, invertAddresses, shipToDifferentAddress, shippingDisable }: BillingAddressControllerProps): boolean;
|
|
14
|
+
interface ShippingAddressControllerProps {
|
|
12
15
|
billingDisable?: boolean;
|
|
13
16
|
errors?: BaseError[];
|
|
17
|
+
requiresBillingInfo?: boolean | null;
|
|
14
18
|
shipToDifferentAddress?: boolean;
|
|
15
19
|
shipping_address?: AddressCreate;
|
|
16
20
|
shippingAddressId?: string;
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
invertAddresses?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare function shippingAddressController({ billingDisable, errors, shipToDifferentAddress, shipping_address, shippingAddressId, invertAddresses, requiresBillingInfo }: ShippingAddressControllerProps): boolean;
|
|
19
24
|
type CountryLockController = (params: {
|
|
20
25
|
addresses?: Address[] | null;
|
|
21
26
|
billing_address?: TCustomerAddress;
|
|
@@ -26,4 +31,29 @@ type CountryLockController = (params: {
|
|
|
26
31
|
shippingAddressId?: string;
|
|
27
32
|
}) => boolean;
|
|
28
33
|
export declare const countryLockController: CountryLockController;
|
|
34
|
+
interface InvertedAddressesHandlerParams {
|
|
35
|
+
billingAddress?: AddressCreate;
|
|
36
|
+
billingAddressId?: string;
|
|
37
|
+
customerEmail?: string;
|
|
38
|
+
order: Order;
|
|
39
|
+
shipToDifferentAddress?: boolean;
|
|
40
|
+
shippingAddress?: AddressCreate;
|
|
41
|
+
shippingAddressId?: string;
|
|
42
|
+
sdk: CommerceLayerClient;
|
|
43
|
+
}
|
|
44
|
+
export declare function invertedAddressesHandler({ order, billingAddress, billingAddressId, customerEmail, shipToDifferentAddress, shippingAddress, shippingAddressId, sdk }: InvertedAddressesHandlerParams): Promise<OrderUpdate | null>;
|
|
45
|
+
interface AddressControllerProps {
|
|
46
|
+
billing_address?: AddressCreate;
|
|
47
|
+
billingAddressId?: string;
|
|
48
|
+
shipToDifferentAddress?: boolean;
|
|
49
|
+
shipping_address?: AddressCreate;
|
|
50
|
+
shippingAddressId?: string;
|
|
51
|
+
errors?: BaseError[];
|
|
52
|
+
requiresBillingInfo?: boolean | null;
|
|
53
|
+
invertAddresses?: boolean;
|
|
54
|
+
}
|
|
55
|
+
export declare function addressesController({ billing_address, billingAddressId, shipToDifferentAddress, shipping_address, shippingAddressId, errors, requiresBillingInfo, invertAddresses }: AddressControllerProps): {
|
|
56
|
+
billingDisable: boolean;
|
|
57
|
+
shippingDisable: boolean;
|
|
58
|
+
};
|
|
29
59
|
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.countryLockController=exports.shippingAddressController=exports.billingAddressController=void 0;const tslib_1=require("tslib"),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),validateFormFields_1=require("./validateFormFields"),AddressReducer_1=require("../reducers/AddressReducer")
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.addressesController=exports.invertedAddressesHandler=exports.countryLockController=exports.shippingAddressController=exports.billingAddressController=void 0;const tslib_1=require("tslib"),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),validateFormFields_1=require("./validateFormFields"),AddressReducer_1=require("../reducers/AddressReducer");function billingAddressController({billing_address,billingAddressId,errors,requiresBillingInfo=!1,invertAddresses=!1,shipToDifferentAddress,shippingDisable}){let billingDisable=invertAddresses?!!(!shippingDisable&&shipToDifferentAddress):!(0,isEmpty_1.default)(errors)||(0,isEmpty_1.default)(billing_address);if((0,isEmpty_1.default)(errors)&&!(0,isEmpty_1.default)(billing_address))if(invertAddresses)billingDisable=!!(billing_address&&(0,validateFormFields_1.fieldsExist)(billing_address));else{let billingInfo=[...AddressReducer_1.addressFields];requiresBillingInfo&&(billingInfo=[...billingInfo,"billing_info"]),billingDisable=!!(billing_address&&(0,validateFormFields_1.fieldsExist)(billing_address,billingInfo))}return billingDisable&&!(0,isEmpty_1.default)(billingAddressId)&&(0,isEmpty_1.default)(billing_address)&&(billingDisable=!1),billingDisable}exports.billingAddressController=billingAddressController;function shippingAddressController({billingDisable,errors,shipToDifferentAddress,shipping_address,shippingAddressId,invertAddresses=!1,requiresBillingInfo=!1}){let shippingDisable=invertAddresses?!(0,isEmpty_1.default)(errors)||(0,isEmpty_1.default)(shipping_address):!!(!billingDisable&&shipToDifferentAddress);if((0,isEmpty_1.default)(errors)&&!(0,isEmpty_1.default)(shipping_address))if(invertAddresses){let billingInfo=[...AddressReducer_1.addressFields];requiresBillingInfo&&(billingInfo=[...billingInfo,"billing_info"]),shippingDisable=!!(shipping_address&&(0,validateFormFields_1.fieldsExist)(shipping_address,billingInfo))}else shippingDisable=!!(shipping_address&&(0,validateFormFields_1.fieldsExist)(shipping_address));return shippingDisable&&!(0,isEmpty_1.default)(shippingAddressId)&&(0,isEmpty_1.default)(shipping_address)&&(shippingDisable=!1),shippingDisable}exports.shippingAddressController=shippingAddressController;const countryLockController=({addresses,billing_address,billingAddressId,countryCodeLock,shipToDifferentAddress,shipping_address,shippingAddressId})=>{if(countryCodeLock&&!(0,isEmpty_1.default)(addresses)&&billingAddressId&&!shipToDifferentAddress){const addressLocked=addresses?.find(a=>(a?.id===billingAddressId||a?.reference===billingAddressId)&&a?.country_code!==countryCodeLock);if(!(0,isEmpty_1.default)(addressLocked))return!0}if(countryCodeLock&&!(0,isEmpty_1.default)(billing_address)&&!shipToDifferentAddress)return billing_address?.country_code!==countryCodeLock;if(countryCodeLock&&!(0,isEmpty_1.default)(shipping_address)&&shipToDifferentAddress)return shipping_address?.country_code!==countryCodeLock;if(countryCodeLock&&!(0,isEmpty_1.default)(addresses)&&shippingAddressId&&shipToDifferentAddress){const addressLocked=addresses?.find(a=>(a?.id===shippingAddressId||a?.reference===shippingAddressId)&&a?.country_code!==countryCodeLock);if(!(0,isEmpty_1.default)(addressLocked))return!0}return!1};exports.countryLockController=countryLockController;function invertedAddressesHandler({order,billingAddress,billingAddressId,customerEmail,shipToDifferentAddress,shippingAddress,shippingAddressId,sdk}){var _a,_b,_c;return tslib_1.__awaiter(this,void 0,void 0,function*(){const currentShippingAddressRef=(_a=order?.shipping_address)===null||_a===void 0?void 0:_a.reference,orderAttributes={id:order?.id,_billing_address_clone_id:shippingAddressId,_shipping_address_clone_id:shippingAddressId,customer_email:customerEmail};if(currentShippingAddressRef===billingAddressId&&(orderAttributes._billing_address_clone_id=(_b=order?.billing_address)===null||_b===void 0?void 0:_b.id,orderAttributes._shipping_address_clone_id=(_c=order?.shipping_address)===null||_c===void 0?void 0:_c.id),shippingAddress!=null&&Object.keys(shippingAddress).length>0){delete orderAttributes._billing_address_clone_id,delete orderAttributes._shipping_address_clone_id,orderAttributes._billing_address_same_as_shipping=!0;const hasMetadata=Object.keys(shippingAddress).filter(key=>!!key.startsWith("metadata_"));hasMetadata?.length>0&&hasMetadata.forEach(key=>{const metadataKey=key.replace("metadata_","");shippingAddress.metadata=Object.assign(Object.assign({},shippingAddress.metadata||{}),{[metadataKey]:shippingAddress[key]}),delete shippingAddress[key]});const address=yield sdk.addresses.create(shippingAddress);orderAttributes.shipping_address=sdk.addresses.relationship(address.id)}if(shipToDifferentAddress&&(delete orderAttributes._billing_address_same_as_shipping,billingAddressId&&(orderAttributes._billing_address_clone_id=billingAddressId),billingAddress!=null&&Object.keys(billingAddress).length>0)){delete orderAttributes._billing_address_clone_id;const hasMetadata=Object.keys(billingAddress).filter(key=>!!key.startsWith("metadata_"));hasMetadata?.length>0&&hasMetadata.forEach(key=>{const metadataKey=key.replace("metadata_","");billingAddress.metadata=Object.assign(Object.assign({},billingAddress.metadata||{}),{[metadataKey]:billingAddress[key]}),delete billingAddress[key]});const address=yield sdk.addresses.create(billingAddress);orderAttributes.billing_address=sdk.addresses.relationship(address.id)}return orderAttributes})}exports.invertedAddressesHandler=invertedAddressesHandler;function addressesController({billing_address,billingAddressId,shipToDifferentAddress,shipping_address,shippingAddressId,errors,requiresBillingInfo,invertAddresses}){if(invertAddresses){const shippingDisable2=shippingAddressController({errors,shipToDifferentAddress,shipping_address,shippingAddressId,invertAddresses}),billingDisable2=billingAddressController({shippingDisable:shippingDisable2,billing_address,billingAddressId,errors,requiresBillingInfo,invertAddresses});return{shippingDisable:shippingDisable2,billingDisable:billingDisable2}}const billingDisable=billingAddressController({billing_address,billingAddressId,errors,requiresBillingInfo}),shippingDisable=shippingAddressController({billingDisable,errors,shipToDifferentAddress,shipping_address,shippingAddressId});return{billingDisable,shippingDisable}}exports.addressesController=addressesController;
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { type BaseInputComponentProps, type AddressInputName } from '../../typings/index';
|
|
2
2
|
import { type AddressValuesKeys } from '../../context/BillingAddressFormContext';
|
|
3
3
|
type Props = {
|
|
4
|
+
/**
|
|
5
|
+
* The name of the input.
|
|
6
|
+
*/
|
|
4
7
|
name: Extract<AddressValuesKeys, AddressInputName>;
|
|
8
|
+
/**
|
|
9
|
+
* Used to add a custom validation rule.
|
|
10
|
+
*/
|
|
11
|
+
pattern?: RegExp | string;
|
|
5
12
|
} & Omit<BaseInputComponentProps, 'name'> & JSX.IntrinsicElements['input'] & Omit<JSX.IntrinsicElements['textarea'], 'children'>;
|
|
6
13
|
export declare function AddressInput(props: Props): JSX.Element | null;
|
|
7
14
|
export default AddressInput;
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import { type ReactNode } from 'react';
|
|
2
2
|
interface Props {
|
|
3
3
|
children: ReactNode;
|
|
4
|
+
/**
|
|
5
|
+
* If true, the shipping address will be considered. Default is false.
|
|
6
|
+
*/
|
|
4
7
|
shipToDifferentAddress?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* If true, the address will be considered a business address.
|
|
10
|
+
*/
|
|
5
11
|
isBusiness?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* If true, the shipping address will be considered as primary address. Default is false.
|
|
14
|
+
*/
|
|
15
|
+
invertAddresses?: boolean;
|
|
6
16
|
}
|
|
7
17
|
export declare function AddressesContainer(props: Props): JSX.Element;
|
|
8
18
|
export default AddressesContainer;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx}from"react/jsx-runtime";import AddressesContext,{defaultAddressContext}from"../../context/AddressContext";import{useContext,useEffect,useReducer}from"react";import addressReducer,{addressInitialState,setAddressErrors,setCloneAddress,saveAddresses}from"../../reducers/AddressReducer";import OrderContext from"../../context/OrderContext";import CommerceLayerContext from"../../context/CommerceLayerContext";export function AddressesContainer(props){const{children,shipToDifferentAddress=!1,isBusiness}=props,[state,dispatch]=useReducer(addressReducer,addressInitialState),{order,orderId,updateOrder}=useContext(OrderContext),config=useContext(CommerceLayerContext);useEffect(()=>(dispatch({type:"setShipToDifferentAddress",payload:{shipToDifferentAddress,isBusiness}}),()=>{dispatch({type:"cleanup",payload:{}})}),[shipToDifferentAddress,isBusiness]);const contextValue={...state,setAddressErrors:(errors,resource)=>{setAddressErrors({errors,resource,dispatch,currentErrors:state.errors})},setAddress:params=>{defaultAddressContext.setAddress({...params,dispatch})},saveAddresses:async customerEmail=>await saveAddresses({config,dispatch,updateOrder,order,orderId,state,customerEmail}),setCloneAddress:(id,resource)=>{setCloneAddress(id,resource,dispatch)}};return _jsx(AddressesContext.Provider,{value:contextValue,children})}export default AddressesContainer;
|
|
2
|
+
import{jsx as _jsx}from"react/jsx-runtime";import AddressesContext,{defaultAddressContext}from"../../context/AddressContext";import{useContext,useEffect,useReducer}from"react";import addressReducer,{addressInitialState,setAddressErrors,setCloneAddress,saveAddresses}from"../../reducers/AddressReducer";import OrderContext from"../../context/OrderContext";import CommerceLayerContext from"../../context/CommerceLayerContext";export function AddressesContainer(props){const{children,shipToDifferentAddress=!1,isBusiness,invertAddresses=!1}=props,[state,dispatch]=useReducer(addressReducer,addressInitialState),{order,orderId,updateOrder}=useContext(OrderContext),config=useContext(CommerceLayerContext);useEffect(()=>(dispatch({type:"setShipToDifferentAddress",payload:{shipToDifferentAddress,isBusiness,invertAddresses}}),()=>{dispatch({type:"cleanup",payload:{}})}),[shipToDifferentAddress,isBusiness,invertAddresses]);const contextValue={...state,setAddressErrors:(errors,resource)=>{setAddressErrors({errors,resource,dispatch,currentErrors:state.errors})},setAddress:params=>{defaultAddressContext.setAddress({...params,dispatch})},saveAddresses:async customerEmail=>await saveAddresses({config,dispatch,updateOrder,order,orderId,state,customerEmail}),setCloneAddress:(id,resource)=>{setCloneAddress(id,resource,dispatch)}};return _jsx(AddressesContext.Provider,{value:contextValue,children})}export default AddressesContainer;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx}from"react/jsx-runtime";import{useContext,useState}from"react";import Parent from"../utils/Parent";import AddressContext from"../../context/AddressContext";import{
|
|
2
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{useContext,useState}from"react";import Parent from"../utils/Parent";import AddressContext from"../../context/AddressContext";import{countryLockController,addressesController}from"../../utils/addressesManager";import OrderContext from"../../context/OrderContext";import CustomerContext from"../../context/CustomerContext";import isFunction from"lodash/isFunction";export 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,invertAddresses}=useContext(AddressContext);console.log("invertAddresses",invertAddresses);const{order}=useContext(OrderContext),{customerEmail:email,addresses,isGuest,createCustomerAddress}=useContext(CustomerContext),[forceDisable,setForceDisable]=useState(disabled);let customerEmail=(isGuest===!0||typeof isGuest>"u")&&!order?.customer_email;email!=null&&email!==""&&(customerEmail=!1);const{billingDisable,shippingDisable}=addressesController({invertAddresses,billing_address:billingAddress,shipping_address:shippingAddress,shippingAddressId,billingAddressId,errors}),countryLockDisable=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 response={success:!1};if(setForceDisable(!0),order&&saveAddresses!=null)response=await saveAddresses(email);else if(createCustomerAddress&&billingAddress){const address={...billingAddress};addressId&&(address.id=addressId),createCustomerAddress(address),response={success:!0}}setForceDisable(!1),onClick&&response.success&&onClick(response)}},parentProps={...p,label,resource,handleClick,disabled:disable};return children?_jsx(Parent,{...parentProps,children}):_jsx("button",{type:"button",disabled:disable||forceDisable,onClick:()=>{handleClick()},...p,children:isFunction(label)?label():label})}export default SaveAddressesButton;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import type { Address } from '@commercelayer/sdk';
|
|
3
|
-
export type AddressValuesKeys = `${keyof Address}` | `billing_address_${keyof Address}` | `shipping_address_${keyof Address}` | `billing_address_${`metadata_${string}`}`;
|
|
3
|
+
export type AddressValuesKeys = `${keyof Address}` | `billing_address_${keyof Address}` | `shipping_address_${keyof Address}` | `billing_address_${`metadata_${string}`}` | `shipping_address_${`metadata_${string}`}`;
|
|
4
4
|
export interface DefaultContextAddress {
|
|
5
5
|
validation?: void;
|
|
6
6
|
setValue?: (name: AddressValuesKeys, value: string | number | readonly string[]) => void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Dispatch } from 'react';
|
|
2
2
|
import { type BaseError } from '../typings/errors';
|
|
3
3
|
import { type CommerceLayerConfig } from '../context/CommerceLayerContext';
|
|
4
|
-
import type
|
|
4
|
+
import { type Address, type AddressCreate, type Order } from '@commercelayer/sdk';
|
|
5
5
|
import { type updateOrder } from './OrderReducer';
|
|
6
6
|
import { type TCustomerAddress } from './CustomerReducer';
|
|
7
7
|
import { type TResourceError } from '../components/errors/Errors';
|
|
@@ -19,6 +19,7 @@ export interface AddressActionPayload {
|
|
|
19
19
|
billingAddressId: string;
|
|
20
20
|
shippingAddressId: string;
|
|
21
21
|
isBusiness: boolean;
|
|
22
|
+
invertAddresses: boolean;
|
|
22
23
|
}
|
|
23
24
|
export type AddressState = Partial<AddressActionPayload>;
|
|
24
25
|
export interface AddressAction {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import baseReducer from"../utils/baseReducer";import getSdk from"../utils/getSdk";import camelCase from"lodash/camelCase";export const addressFields=["city","company","country_code","first_name","last_name","line_1","line_2","phone","state_code","zip_code"],addressInitialState={errors:[]},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}})},setAddress=({values,resource,dispatch})=>{dispatch&&dispatch({type:"setAddress",payload:{[`${resource}`]:values}})},setCloneAddress=(id,resource,dispatch)=>{dispatch({type:"setCloneAddress",payload:{[`${camelCase(resource)}Id`]:id}})};export async function saveAddresses({config,updateOrder,order,state,customerEmail}){const{shipToDifferentAddress,billing_address:billingAddress,shipping_address:shippingAddress,billingAddressId,shippingAddressId}=state;try{const sdk=getSdk(config);if(order){const currentBillingAddressRef=order?.billing_address?.reference
|
|
2
|
+
import baseReducer from"../utils/baseReducer";import getSdk from"../utils/getSdk";import camelCase from"lodash/camelCase";import{invertedAddressesHandler}from"../utils/addressesManager";export const addressFields=["city","company","country_code","first_name","last_name","line_1","line_2","phone","state_code","zip_code"],addressInitialState={errors:[]},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}})},setAddress=({values,resource,dispatch})=>{dispatch&&dispatch({type:"setAddress",payload:{[`${resource}`]:values}})},setCloneAddress=(id,resource,dispatch)=>{dispatch({type:"setCloneAddress",payload:{[`${camelCase(resource)}Id`]:id}})};export async function saveAddresses({config,updateOrder,order,state,customerEmail}){const{shipToDifferentAddress,invertAddresses,billing_address:billingAddress,shipping_address:shippingAddress,billingAddressId,shippingAddressId}=state;try{const sdk=getSdk(config);if(order){let orderAttributes=null;if(invertAddresses)orderAttributes=await invertedAddressesHandler({billingAddress,billingAddressId,customerEmail,order,shipToDifferentAddress,shippingAddress,shippingAddressId,sdk});else{const currentBillingAddressRef=order?.billing_address?.reference;if(orderAttributes={id:order?.id,_billing_address_clone_id:billingAddressId,_shipping_address_clone_id:billingAddressId,customer_email:customerEmail},currentBillingAddressRef===billingAddressId&&(orderAttributes._billing_address_clone_id=order?.billing_address?.id,orderAttributes._shipping_address_clone_id=order?.shipping_address?.id),billingAddress!=null&&Object.keys(billingAddress).length>0){delete orderAttributes._billing_address_clone_id,delete orderAttributes._shipping_address_clone_id,orderAttributes._shipping_address_same_as_billing=!0;const hasMetadata=Object.keys(billingAddress).filter(key=>!!key.startsWith("metadata_"));hasMetadata?.length>0&&hasMetadata.forEach(key=>{const metadataKey=key.replace("metadata_","");billingAddress.metadata={...billingAddress.metadata||{},[metadataKey]:billingAddress[key]},delete billingAddress[key]});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),shippingAddress!=null&&Object.keys(shippingAddress).length>0)){delete orderAttributes._shipping_address_clone_id;const hasMetadata=Object.keys(shippingAddress).filter(key=>!!key.startsWith("metadata_"));hasMetadata?.length>0&&hasMetadata.forEach(key=>{const metadataKey=key.replace("metadata_","");shippingAddress.metadata={...shippingAddress.metadata||{},[metadataKey]:shippingAddress[key]},delete shippingAddress[key]});const address=await sdk.addresses.create(shippingAddress);orderAttributes.shipping_address=sdk.addresses.relationship(address.id)}}if(orderAttributes!=null&&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}}}const type=["setErrors","setAddress","setShipToDifferentAddress","setCloneAddress","cleanup"],addressReducer=(state,reducer)=>baseReducer(state,reducer,type);export default addressReducer;
|
|
@@ -51,7 +51,7 @@ export interface BaseInputComponentProps {
|
|
|
51
51
|
placeholder?: string;
|
|
52
52
|
}
|
|
53
53
|
export type GiftCardInputName = 'balanceCents' | 'balanceMaxCents' | 'singleUse' | 'rechargeable' | 'imageUrl' | 'expiresAt' | 'referenceOrigin' | 'email' | 'firstName' | 'lastName' | 'reference';
|
|
54
|
-
export type AddressInputName = 'billing_address_city' | 'billing_address_company' | 'billing_address_first_name' | 'billing_address_email' | 'billing_address_last_name' | 'billing_address_line_1' | 'billing_address_line_2' | 'billing_address_phone' | 'billing_address_state_code' | 'billing_address_zip_code' | 'billing_address_billing_info' | 'billing_address_save_to_customer_book' | `billing_address_${`metadata_${string}`}` | 'shipping_address_city' | 'shipping_address_company' | 'shipping_address_email' | 'shipping_address_first_name' | 'shipping_address_last_name' | 'shipping_address_line_1' | 'shipping_address_line_2' | 'shipping_address_phone' | 'shipping_address_state_code' | 'shipping_address_zip_code' | 'shipping_address_save_to_customer_book'
|
|
54
|
+
export type AddressInputName = 'billing_address_city' | 'billing_address_company' | 'billing_address_first_name' | 'billing_address_email' | 'billing_address_last_name' | 'billing_address_line_1' | 'billing_address_line_2' | 'billing_address_phone' | 'billing_address_state_code' | 'billing_address_zip_code' | 'billing_address_billing_info' | 'billing_address_save_to_customer_book' | `billing_address_${`metadata_${string}`}` | 'shipping_address_city' | 'shipping_address_company' | 'shipping_address_email' | 'shipping_address_first_name' | 'shipping_address_last_name' | 'shipping_address_line_1' | 'shipping_address_line_2' | 'shipping_address_phone' | 'shipping_address_state_code' | 'shipping_address_zip_code' | 'shipping_address_save_to_customer_book' | 'shipping_address_billing_info' | `shipping_address_${`metadata_${string}`}`;
|
|
55
55
|
export type AddressCountrySelectName = 'billing_address_country_code' | 'shipping_address_country_code';
|
|
56
56
|
export type AddressStateSelectName = 'billing_address_state_code' | 'shipping_address_state_code';
|
|
57
57
|
export type LoaderType = string | JSX.Element;
|
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
import { type BaseError } from '../typings/errors';
|
|
2
|
-
import { type Address, type AddressCreate } from '@commercelayer/sdk';
|
|
2
|
+
import { type OrderUpdate, type Address, type AddressCreate, type Order, type CommerceLayerClient } from '@commercelayer/sdk';
|
|
3
3
|
import { type TCustomerAddress } from '../reducers/CustomerReducer';
|
|
4
|
-
|
|
4
|
+
interface BillingAddressControllerProps {
|
|
5
5
|
billing_address?: AddressCreate;
|
|
6
6
|
billingAddressId?: string;
|
|
7
7
|
errors?: BaseError[];
|
|
8
8
|
requiresBillingInfo?: boolean | null;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
invertAddresses?: boolean;
|
|
10
|
+
shippingDisable?: boolean;
|
|
11
|
+
shipToDifferentAddress?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare function billingAddressController({ billing_address, billingAddressId, errors, requiresBillingInfo, invertAddresses, shipToDifferentAddress, shippingDisable }: BillingAddressControllerProps): boolean;
|
|
14
|
+
interface ShippingAddressControllerProps {
|
|
12
15
|
billingDisable?: boolean;
|
|
13
16
|
errors?: BaseError[];
|
|
17
|
+
requiresBillingInfo?: boolean | null;
|
|
14
18
|
shipToDifferentAddress?: boolean;
|
|
15
19
|
shipping_address?: AddressCreate;
|
|
16
20
|
shippingAddressId?: string;
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
invertAddresses?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare function shippingAddressController({ billingDisable, errors, shipToDifferentAddress, shipping_address, shippingAddressId, invertAddresses, requiresBillingInfo }: ShippingAddressControllerProps): boolean;
|
|
19
24
|
type CountryLockController = (params: {
|
|
20
25
|
addresses?: Address[] | null;
|
|
21
26
|
billing_address?: TCustomerAddress;
|
|
@@ -26,4 +31,29 @@ type CountryLockController = (params: {
|
|
|
26
31
|
shippingAddressId?: string;
|
|
27
32
|
}) => boolean;
|
|
28
33
|
export declare const countryLockController: CountryLockController;
|
|
34
|
+
interface InvertedAddressesHandlerParams {
|
|
35
|
+
billingAddress?: AddressCreate;
|
|
36
|
+
billingAddressId?: string;
|
|
37
|
+
customerEmail?: string;
|
|
38
|
+
order: Order;
|
|
39
|
+
shipToDifferentAddress?: boolean;
|
|
40
|
+
shippingAddress?: AddressCreate;
|
|
41
|
+
shippingAddressId?: string;
|
|
42
|
+
sdk: CommerceLayerClient;
|
|
43
|
+
}
|
|
44
|
+
export declare function invertedAddressesHandler({ order, billingAddress, billingAddressId, customerEmail, shipToDifferentAddress, shippingAddress, shippingAddressId, sdk }: InvertedAddressesHandlerParams): Promise<OrderUpdate | null>;
|
|
45
|
+
interface AddressControllerProps {
|
|
46
|
+
billing_address?: AddressCreate;
|
|
47
|
+
billingAddressId?: string;
|
|
48
|
+
shipToDifferentAddress?: boolean;
|
|
49
|
+
shipping_address?: AddressCreate;
|
|
50
|
+
shippingAddressId?: string;
|
|
51
|
+
errors?: BaseError[];
|
|
52
|
+
requiresBillingInfo?: boolean | null;
|
|
53
|
+
invertAddresses?: boolean;
|
|
54
|
+
}
|
|
55
|
+
export declare function addressesController({ billing_address, billingAddressId, shipToDifferentAddress, shipping_address, shippingAddressId, errors, requiresBillingInfo, invertAddresses }: AddressControllerProps): {
|
|
56
|
+
billingDisable: boolean;
|
|
57
|
+
shippingDisable: boolean;
|
|
58
|
+
};
|
|
29
59
|
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import isEmpty from"lodash/isEmpty";import{fieldsExist}from"./validateFormFields";import{addressFields}from"../reducers/AddressReducer";export
|
|
2
|
+
import isEmpty from"lodash/isEmpty";import{fieldsExist}from"./validateFormFields";import{addressFields}from"../reducers/AddressReducer";export function billingAddressController({billing_address,billingAddressId,errors,requiresBillingInfo=!1,invertAddresses=!1,shipToDifferentAddress,shippingDisable}){let billingDisable=invertAddresses?!!(!shippingDisable&&shipToDifferentAddress):!isEmpty(errors)||isEmpty(billing_address);if(isEmpty(errors)&&!isEmpty(billing_address))if(invertAddresses)billingDisable=!!(billing_address&&fieldsExist(billing_address));else{let billingInfo=[...addressFields];requiresBillingInfo&&(billingInfo=[...billingInfo,"billing_info"]),billingDisable=!!(billing_address&&fieldsExist(billing_address,billingInfo))}return billingDisable&&!isEmpty(billingAddressId)&&isEmpty(billing_address)&&(billingDisable=!1),billingDisable}export function shippingAddressController({billingDisable,errors,shipToDifferentAddress,shipping_address,shippingAddressId,invertAddresses=!1,requiresBillingInfo=!1}){let shippingDisable=invertAddresses?!isEmpty(errors)||isEmpty(shipping_address):!!(!billingDisable&&shipToDifferentAddress);if(isEmpty(errors)&&!isEmpty(shipping_address))if(invertAddresses){let billingInfo=[...addressFields];requiresBillingInfo&&(billingInfo=[...billingInfo,"billing_info"]),shippingDisable=!!(shipping_address&&fieldsExist(shipping_address,billingInfo))}else shippingDisable=!!(shipping_address&&fieldsExist(shipping_address));return shippingDisable&&!isEmpty(shippingAddressId)&&isEmpty(shipping_address)&&(shippingDisable=!1),shippingDisable}export const countryLockController=({addresses,billing_address,billingAddressId,countryCodeLock,shipToDifferentAddress,shipping_address,shippingAddressId})=>{if(countryCodeLock&&!isEmpty(addresses)&&billingAddressId&&!shipToDifferentAddress){const addressLocked=addresses?.find(a=>(a?.id===billingAddressId||a?.reference===billingAddressId)&&a?.country_code!==countryCodeLock);if(!isEmpty(addressLocked))return!0}if(countryCodeLock&&!isEmpty(billing_address)&&!shipToDifferentAddress)return billing_address?.country_code!==countryCodeLock;if(countryCodeLock&&!isEmpty(shipping_address)&&shipToDifferentAddress)return shipping_address?.country_code!==countryCodeLock;if(countryCodeLock&&!isEmpty(addresses)&&shippingAddressId&&shipToDifferentAddress){const addressLocked=addresses?.find(a=>(a?.id===shippingAddressId||a?.reference===shippingAddressId)&&a?.country_code!==countryCodeLock);if(!isEmpty(addressLocked))return!0}return!1};export async function invertedAddressesHandler({order,billingAddress,billingAddressId,customerEmail,shipToDifferentAddress,shippingAddress,shippingAddressId,sdk}){const currentShippingAddressRef=order?.shipping_address?.reference,orderAttributes={id:order?.id,_billing_address_clone_id:shippingAddressId,_shipping_address_clone_id:shippingAddressId,customer_email:customerEmail};if(currentShippingAddressRef===billingAddressId&&(orderAttributes._billing_address_clone_id=order?.billing_address?.id,orderAttributes._shipping_address_clone_id=order?.shipping_address?.id),shippingAddress!=null&&Object.keys(shippingAddress).length>0){delete orderAttributes._billing_address_clone_id,delete orderAttributes._shipping_address_clone_id,orderAttributes._billing_address_same_as_shipping=!0;const hasMetadata=Object.keys(shippingAddress).filter(key=>!!key.startsWith("metadata_"));hasMetadata?.length>0&&hasMetadata.forEach(key=>{const metadataKey=key.replace("metadata_","");shippingAddress.metadata={...shippingAddress.metadata||{},[metadataKey]:shippingAddress[key]},delete shippingAddress[key]});const address=await sdk.addresses.create(shippingAddress);orderAttributes.shipping_address=sdk.addresses.relationship(address.id)}if(shipToDifferentAddress&&(delete orderAttributes._billing_address_same_as_shipping,billingAddressId&&(orderAttributes._billing_address_clone_id=billingAddressId),billingAddress!=null&&Object.keys(billingAddress).length>0)){delete orderAttributes._billing_address_clone_id;const hasMetadata=Object.keys(billingAddress).filter(key=>!!key.startsWith("metadata_"));hasMetadata?.length>0&&hasMetadata.forEach(key=>{const metadataKey=key.replace("metadata_","");billingAddress.metadata={...billingAddress.metadata||{},[metadataKey]:billingAddress[key]},delete billingAddress[key]});const address=await sdk.addresses.create(billingAddress);orderAttributes.billing_address=sdk.addresses.relationship(address.id)}return orderAttributes}export function addressesController({billing_address,billingAddressId,shipToDifferentAddress,shipping_address,shippingAddressId,errors,requiresBillingInfo,invertAddresses}){if(invertAddresses){const shippingDisable2=shippingAddressController({errors,shipToDifferentAddress,shipping_address,shippingAddressId,invertAddresses}),billingDisable2=billingAddressController({shippingDisable:shippingDisable2,billing_address,billingAddressId,errors,requiresBillingInfo,invertAddresses});return{shippingDisable:shippingDisable2,billingDisable:billingDisable2}}const billingDisable=billingAddressController({billing_address,billingAddressId,errors,requiresBillingInfo}),shippingDisable=shippingAddressController({billingDisable,errors,shipToDifferentAddress,shipping_address,shippingAddressId});return{billingDisable,shippingDisable}}
|