@commercelayer/react-components 4.25.7-beta.3 → 4.25.7-beta.4
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.js +1 -1
- package/lib/cjs/utils/addressesManager.d.ts +2 -2
- package/lib/cjs/utils/addressesManager.js +1 -1
- package/lib/cjs/utils/validateFormFields.d.ts +6 -6
- package/lib/cjs/utils/validateFormFields.js +1 -1
- package/lib/esm/components/addresses/SaveAddressesButton.js +1 -1
- package/lib/esm/utils/addressesManager.d.ts +2 -2
- package/lib/esm/utils/addressesManager.js +1 -1
- package/lib/esm/utils/validateFormFields.d.ts +6 -6
- package/lib/esm/utils/validateFormFields.js +1 -1
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.SaveAddressesButton=SaveAddressesButton;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),isFunction_1=tslib_1.__importDefault(require("lodash/isFunction")),react_1=require("react"),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),AddressContext_1=tslib_1.__importDefault(require("../../context/AddressContext")),CustomerContext_1=tslib_1.__importDefault(require("../../context/CustomerContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),addressesManager_1=require("../../utils/addressesManager"),formCleaner_1=require("../../utils/formCleaner"),validateFormFields_1=require("../../utils/validateFormFields");function SaveAddressesButton(props){const{children,label="Continue to delivery",resource,disabled=!1,addressId,requiredMetadataFields,onClick}=props,p=tslib_1.__rest(props,["children","label","resource","disabled","addressId","requiredMetadataFields","onClick"]),{errors,billing_address:billingAddress,shipToDifferentAddress,shipping_address:shippingAddress,saveAddresses,billingAddressId,shippingAddressId,invertAddresses}=(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;if(email!=null&&email!==""){const isValidEmail=(0,validateFormFields_1.validateValue)(email,"customer_email","email","orders");customerEmail=Object.keys(isValidEmail).length>0}const shippingAddressCleaned=Object.keys(shippingAddress??{}).reduce((acc,key)=>Object.assign(Object.assign({},acc),{[key.replace("shipping_address_","")]:shippingAddress[key].value}),{}),{billingDisable,shippingDisable}=(0,addressesManager_1.addressesController)({invertAddresses,requiresBillingInfo:order?.requires_billing_info,billing_address:billingAddress,shipping_address:shippingAddressCleaned,shipToDifferentAddress,shippingAddressId,billingAddressId,errors,requiredMetadataFields}),countryLockDisable=(0,addressesManager_1.countryLockController)({countryCodeLock:order?.shipping_country_code_lock,addresses,shipToDifferentAddress,billingAddressId,billing_address:billingAddress,shipping_address:shippingAddress,shippingAddressId,lineItems:order?.line_items}),disable=disabled||customerEmail||billingDisable||(invertAddresses&&shippingAddressId?!1:shippingDisable)||countryLockDisable,handleClick=()=>tslib_1.__awaiter(this,void 0,void 0,function*(){if(errors&&Object.keys(errors).length===0&&!disable){let response={success:!1};switch(setForceDisable(!0),!0){case(order!=null&&addressId!=null&&createCustomerAddress!=null&&saveAddresses!=null):{response=yield saveAddresses({customerEmail:email,customerAddress:{resource:invertAddresses?"shipping_address":"billing_address",id:addressId}});break}case(order!=null&&saveAddresses!=null):{response=yield saveAddresses({customerEmail:email});break}case createCustomerAddress!=null:{const address=invertAddresses?Object.assign({},(0,formCleaner_1.formCleaner)(shippingAddress)):Object.assign({},(0,formCleaner_1.formCleaner)(billingAddress));addressId&&(address.id=addressId),createCustomerAddress(address),response={success:!0};break}}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.default=SaveAddressesButton;
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.SaveAddressesButton=SaveAddressesButton;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),isFunction_1=tslib_1.__importDefault(require("lodash/isFunction")),react_1=require("react"),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),AddressContext_1=tslib_1.__importDefault(require("../../context/AddressContext")),CustomerContext_1=tslib_1.__importDefault(require("../../context/CustomerContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),addressesManager_1=require("../../utils/addressesManager"),formCleaner_1=require("../../utils/formCleaner"),validateFormFields_1=require("../../utils/validateFormFields");function SaveAddressesButton(props){const{children,label="Continue to delivery",resource,disabled=!1,addressId,requiredMetadataFields,onClick}=props,p=tslib_1.__rest(props,["children","label","resource","disabled","addressId","requiredMetadataFields","onClick"]),{errors,billing_address:billingAddress,shipToDifferentAddress,shipping_address:shippingAddress,saveAddresses,billingAddressId,shippingAddressId,invertAddresses}=(0,react_1.useContext)(AddressContext_1.default),{order,setOrderErrors}=(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;if(email!=null&&email!==""){const isValidEmail=(0,validateFormFields_1.validateValue)(email,"customer_email","email","orders");customerEmail=Object.keys(isValidEmail).length>0}const shippingAddressCleaned=Object.keys(shippingAddress??{}).reduce((acc,key)=>Object.assign(Object.assign({},acc),{[key.replace("shipping_address_","")]:shippingAddress[key].value}),{}),{billingDisable,shippingDisable}=(0,addressesManager_1.addressesController)({invertAddresses,requiresBillingInfo:order?.requires_billing_info,billing_address:billingAddress,shipping_address:shippingAddressCleaned,shipToDifferentAddress,shippingAddressId,billingAddressId,errors,requiredMetadataFields}),countryLockDisable=(0,addressesManager_1.countryLockController)({countryCodeLock:order?.shipping_country_code_lock,addresses,shipToDifferentAddress,billingAddressId,billing_address:billingAddress,shipping_address:shippingAddress,shippingAddressId,lineItems:order?.line_items}),disable=disabled||customerEmail||billingDisable||(invertAddresses&&shippingAddressId?!1:shippingDisable)||countryLockDisable,handleClick=()=>tslib_1.__awaiter(this,void 0,void 0,function*(){if(errors&&Object.keys(errors).length===0&&!disable){setOrderErrors?.([]);let response={success:!1};switch(setForceDisable(!0),!0){case(order!=null&&addressId!=null&&createCustomerAddress!=null&&saveAddresses!=null):{response=yield saveAddresses({customerEmail:email,customerAddress:{resource:invertAddresses?"shipping_address":"billing_address",id:addressId}});break}case(order!=null&&saveAddresses!=null):{response=yield saveAddresses({customerEmail:email});break}case createCustomerAddress!=null:{const address=invertAddresses?Object.assign({},(0,formCleaner_1.formCleaner)(shippingAddress)):Object.assign({},(0,formCleaner_1.formCleaner)(billingAddress));addressId&&(address.id=addressId),createCustomerAddress(address),response={success:!0};break}}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.default=SaveAddressesButton;
|
|
@@ -11,7 +11,7 @@ interface BillingAddressControllerProps {
|
|
|
11
11
|
shipToDifferentAddress?: boolean;
|
|
12
12
|
requiredMetadataFields?: string[];
|
|
13
13
|
}
|
|
14
|
-
export declare function billingAddressController({ billing_address, billingAddressId, errors,
|
|
14
|
+
export declare function billingAddressController({ billing_address, billingAddressId, errors, invertAddresses, shipToDifferentAddress, shippingDisable, requiredMetadataFields, }: BillingAddressControllerProps): boolean;
|
|
15
15
|
interface ShippingAddressControllerProps {
|
|
16
16
|
billingDisable?: boolean;
|
|
17
17
|
errors?: BaseError[];
|
|
@@ -22,7 +22,7 @@ interface ShippingAddressControllerProps {
|
|
|
22
22
|
invertAddresses?: boolean;
|
|
23
23
|
requiredMetadataFields?: string[];
|
|
24
24
|
}
|
|
25
|
-
export declare function shippingAddressController({ billingDisable, errors, shipToDifferentAddress, shipping_address, shippingAddressId, invertAddresses,
|
|
25
|
+
export declare function shippingAddressController({ billingDisable, errors, shipToDifferentAddress, shipping_address, shippingAddressId, invertAddresses, requiredMetadataFields, }: ShippingAddressControllerProps): boolean;
|
|
26
26
|
interface CountryLockControllerProps {
|
|
27
27
|
addresses?: Address[] | null;
|
|
28
28
|
billing_address?: TCustomerAddress;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.billingAddressController=billingAddressController,exports.shippingAddressController=shippingAddressController,exports.countryLockController=countryLockController,exports.invertedAddressesHandler=invertedAddressesHandler,exports.addressesController=addressesController,exports.sanitizeMetadataFields=sanitizeMetadataFields;const tslib_1=require("tslib"),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),AddressReducer_1=require("../reducers/AddressReducer"),validateFormFields_1=require("./validateFormFields");function billingAddressController({billing_address,billingAddressId,errors,
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.billingAddressController=billingAddressController,exports.shippingAddressController=shippingAddressController,exports.countryLockController=countryLockController,exports.invertedAddressesHandler=invertedAddressesHandler,exports.addressesController=addressesController,exports.sanitizeMetadataFields=sanitizeMetadataFields;const tslib_1=require("tslib"),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),AddressReducer_1=require("../reducers/AddressReducer"),validateFormFields_1=require("./validateFormFields");function billingAddressController({billing_address,billingAddressId,errors,invertAddresses=!1,shipToDifferentAddress,shippingDisable,requiredMetadataFields}){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)){let formFields=[...AddressReducer_1.addressFields];requiredMetadataFields!=null&&requiredMetadataFields.length>0&&(formFields=[...formFields,...requiredMetadataFields]),billingDisable=!!(billing_address&&(0,validateFormFields_1.fieldsExist)(billing_address,formFields))}return billingDisable&&!(0,isEmpty_1.default)(billingAddressId)&&(0,isEmpty_1.default)(billing_address)&&(billingDisable=!1),billingDisable}function shippingAddressController({billingDisable,errors,shipToDifferentAddress,shipping_address,shippingAddressId,invertAddresses=!1,requiredMetadataFields}){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)){let formField=[...AddressReducer_1.addressFields];requiredMetadataFields!=null&&requiredMetadataFields.length>0&&(formField=[...formField,...requiredMetadataFields]),invertAddresses?shippingDisable=!!(shipping_address&&(0,validateFormFields_1.fieldsExist)(shipping_address,formField)):shipToDifferentAddress&&(shipping_address==null||delete shipping_address.billing_info,shippingDisable=!!(shipping_address&&(0,validateFormFields_1.fieldsExist)(shipping_address,formField)))}return shippingDisable&&!(0,isEmpty_1.default)(shippingAddressId)&&(0,isEmpty_1.default)(shipping_address)&&(shippingDisable=!1),shippingDisable}function countryLockController({addresses,billing_address,billingAddressId,countryCodeLock,shipToDifferentAddress,shipping_address,shippingAddressId,lineItems}){var _a;if((_a=lineItems?.filter(lineItem=>{var _a2,_b;return((_a2=lineItem?.item)===null||_a2===void 0?void 0:_a2.type)!=null&&["skus","bundles"].includes((_b=lineItem?.item)===null||_b===void 0?void 0:_b.type)}))===null||_a===void 0?void 0:_a.every(lineItem=>{var _a2;return((_a2=lineItem?.item)===null||_a2===void 0?void 0:_a2.do_not_ship)===!0}))return!1;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}function invertedAddressesHandler(_a){return tslib_1.__awaiter(this,arguments,void 0,function*({order,billingAddress,billingAddressId,customerEmail,shipToDifferentAddress,shippingAddress,shippingAddressId,sdk}){var _b,_c,_d,_e,_f;const currentShippingAddressRef=(_b=order?.shipping_address)===null||_b===void 0?void 0:_b.reference,orderAttributes={id:order?.id,_billing_address_clone_id:shippingAddressId,_shipping_address_clone_id:shippingAddressId,customer_email:customerEmail};if(currentShippingAddressRef===shippingAddressId&&(orderAttributes._billing_address_clone_id=(_c=order?.billing_address)===null||_c===void 0?void 0:_c.id,orderAttributes._shipping_address_clone_id=(_d=order?.shipping_address)===null||_d===void 0?void 0:_d.id),shippingAddress!=null&&Object.keys(shippingAddress).length>0&&!shippingAddressId){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]});let address;!((_e=order?.shipping_address)===null||_e===void 0)&&_e.id?address=yield sdk.addresses.update(Object.assign({id:order.shipping_address.id},shippingAddress)):(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]});let address;!((_f=order?.billing_address)===null||_f===void 0)&&_f.id?address=yield sdk.addresses.update(Object.assign({id:order.billing_address.id},billingAddress)):(address=yield sdk.addresses.create(billingAddress),orderAttributes.billing_address=sdk.addresses.relationship(address.id))}return orderAttributes})}function addressesController({billing_address,billingAddressId,shipToDifferentAddress,shipping_address,shippingAddressId,errors,requiresBillingInfo,invertAddresses,requiredMetadataFields}){if(invertAddresses){const shippingDisable2=shippingAddressController({errors,shipToDifferentAddress,shipping_address,shippingAddressId,invertAddresses,requiresBillingInfo,requiredMetadataFields}),billingDisable2=billingAddressController({shippingDisable:shippingDisable2,billing_address,billingAddressId,errors,requiresBillingInfo,invertAddresses,requiredMetadataFields});return{shippingDisable:shippingDisable2,billingDisable:billingDisable2}}const billingDisable=billingAddressController({billing_address,billingAddressId,errors,requiresBillingInfo,requiredMetadataFields}),shippingDisable=shippingAddressController({billingDisable,errors,shipToDifferentAddress,shipping_address,shippingAddressId,requiredMetadataFields});return{billingDisable,shippingDisable}}function sanitizeMetadataFields(address){const hasMetadata=Object.keys(address).filter(key=>!!key.startsWith("metadata_"));return hasMetadata?.length>0&&hasMetadata.forEach(key=>{const metadataKey=key.replace("metadata_","");address.metadata=Object.assign(Object.assign({},address.metadata||{}),{[metadataKey]:address[key]}),delete address[key]}),address}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import { type AddressField } from
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
1
|
+
import type { AddressCreate } from "@commercelayer/sdk";
|
|
2
|
+
import type { TResourceError } from "../components/errors/Errors";
|
|
3
|
+
import { type AddressField } from "../reducers/AddressReducer";
|
|
4
|
+
import type { AddressInputName } from "../typings/index";
|
|
5
|
+
import type { BaseError } from "../typings/errors";
|
|
6
|
+
import type { BaseState } from "../typings/index";
|
|
7
7
|
export type ValidateFormFields = <R extends string[]>(fields: HTMLFormControlsCollection, required: R, resourceType: TResourceError) => {
|
|
8
8
|
errors: BaseError[];
|
|
9
9
|
values: BaseState;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.validateValue=void 0,exports.fieldsExist=fieldsExist,exports.businessMandatoryField=businessMandatoryField;const tslib_1=require("tslib"),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),isString_1=tslib_1.__importDefault(require("lodash/isString")),
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.validateValue=void 0,exports.fieldsExist=fieldsExist,exports.businessMandatoryField=businessMandatoryField;const tslib_1=require("tslib"),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),isString_1=tslib_1.__importDefault(require("lodash/isString")),keys_1=tslib_1.__importDefault(require("lodash/keys")),map_1=tslib_1.__importDefault(require("lodash/map")),without_1=tslib_1.__importDefault(require("lodash/without")),AddressReducer_1=require("../reducers/AddressReducer"),EMAIL_PATTERN=/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,validateValue=(val,name,type,resource)=>val?type==="email"&&(0,isString_1.default)(val)&&!val.match(EMAIL_PATTERN)?{field:name,code:"VALIDATION_ERROR",message:"please enter a valid format",resource}:{}:{field:name,code:"VALIDATION_ERROR",message:`${name} - is required`,resource};exports.validateValue=validateValue;const validateFormFields=(fields,required,resourceType)=>{const errors=[];let values={metadata:{}};return(0,map_1.default)(fields,v=>{const val="checked"in v||(v.value==="on"?!1:v.value),attrName=v.getAttribute("name");if(attrName&&required.includes(attrName)||v.required){const error=(0,exports.validateValue)(val,v.name,v.type,resourceType);(0,isEmpty_1.default)(error)||errors.push(error),values=Object.assign(Object.assign({},values),{[`${v.name}`]:val})}v.getAttribute("name")&&(values=!!v.getAttribute("data-metadata")?Object.assign(Object.assign({},values),{metadata:Object.assign(Object.assign({},values.metadata),{[`${v.name}`]:val})}):Object.assign(Object.assign({},values),{[`${v.name}`]:val}))}),{errors,values}};function fieldsExist(address,schema=AddressReducer_1.addressFields){if(!address.business){const required2=(0,without_1.default)(schema,"line_2","company","state_code"),validAddress2=(0,keys_1.default)(address).filter(k=>required2.includes(k));return required2.length>validAddress2.length}const required=(0,without_1.default)(schema,"first_name","last_name","line_2","state_code"),validAddress=(0,keys_1.default)(address).filter(k=>required.includes(k));return required.length>validAddress.length}const businessOptionalFields=["billing_address_first_name","billing_address_last_name","shipping_address_first_name","shipping_address_last_name","first_name","last_name"],customerOptionalFields=["billing_address_company","shipping_address_company","company"];function businessMandatoryField(fieldName,isBusiness){return!(isBusiness&&businessOptionalFields.includes(fieldName)||!isBusiness&&customerOptionalFields.includes(fieldName))}exports.default=validateFormFields;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx}from"react/jsx-runtime";import isFunction from"lodash/isFunction";import{useContext,useState}from"react";import Parent from"../utils/Parent";import AddressContext from"../../context/AddressContext";import CustomerContext from"../../context/CustomerContext";import OrderContext from"../../context/OrderContext";import{addressesController,countryLockController}from"../../utils/addressesManager";import{formCleaner}from"../../utils/formCleaner";import{validateValue}from"../../utils/validateFormFields";export function SaveAddressesButton(props){const{children,label="Continue to delivery",resource,disabled=!1,addressId,requiredMetadataFields,onClick,...p}=props,{errors,billing_address:billingAddress,shipToDifferentAddress,shipping_address:shippingAddress,saveAddresses,billingAddressId,shippingAddressId,invertAddresses}=useContext(AddressContext),{order}=useContext(OrderContext),{customerEmail:email,addresses,isGuest,createCustomerAddress}=useContext(CustomerContext),[forceDisable,setForceDisable]=useState(disabled);let customerEmail=(isGuest===!0||typeof isGuest>"u")&&!order?.customer_email;if(email!=null&&email!==""){const isValidEmail=validateValue(email,"customer_email","email","orders");customerEmail=Object.keys(isValidEmail).length>0}const shippingAddressCleaned=Object.keys(shippingAddress??{}).reduce((acc,key)=>({...acc,[key.replace("shipping_address_","")]:shippingAddress[key].value}),{}),{billingDisable,shippingDisable}=addressesController({invertAddresses,requiresBillingInfo:order?.requires_billing_info,billing_address:billingAddress,shipping_address:shippingAddressCleaned,shipToDifferentAddress,shippingAddressId,billingAddressId,errors,requiredMetadataFields}),countryLockDisable=countryLockController({countryCodeLock:order?.shipping_country_code_lock,addresses,shipToDifferentAddress,billingAddressId,billing_address:billingAddress,shipping_address:shippingAddress,shippingAddressId,lineItems:order?.line_items}),disable=disabled||customerEmail||billingDisable||(invertAddresses&&shippingAddressId?!1:shippingDisable)||countryLockDisable,handleClick=async()=>{if(errors&&Object.keys(errors).length===0&&!disable){let response={success:!1};switch(setForceDisable(!0),!0){case(order!=null&&addressId!=null&&createCustomerAddress!=null&&saveAddresses!=null):{response=await saveAddresses({customerEmail:email,customerAddress:{resource:invertAddresses?"shipping_address":"billing_address",id:addressId}});break}case(order!=null&&saveAddresses!=null):{response=await saveAddresses({customerEmail:email});break}case createCustomerAddress!=null:{const address=invertAddresses?{...formCleaner(shippingAddress)}:{...formCleaner(billingAddress)};addressId&&(address.id=addressId),createCustomerAddress(address),response={success:!0};break}}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;
|
|
2
|
+
import{jsx as _jsx}from"react/jsx-runtime";import isFunction from"lodash/isFunction";import{useContext,useState}from"react";import Parent from"../utils/Parent";import AddressContext from"../../context/AddressContext";import CustomerContext from"../../context/CustomerContext";import OrderContext from"../../context/OrderContext";import{addressesController,countryLockController}from"../../utils/addressesManager";import{formCleaner}from"../../utils/formCleaner";import{validateValue}from"../../utils/validateFormFields";export function SaveAddressesButton(props){const{children,label="Continue to delivery",resource,disabled=!1,addressId,requiredMetadataFields,onClick,...p}=props,{errors,billing_address:billingAddress,shipToDifferentAddress,shipping_address:shippingAddress,saveAddresses,billingAddressId,shippingAddressId,invertAddresses}=useContext(AddressContext),{order,setOrderErrors}=useContext(OrderContext),{customerEmail:email,addresses,isGuest,createCustomerAddress}=useContext(CustomerContext),[forceDisable,setForceDisable]=useState(disabled);let customerEmail=(isGuest===!0||typeof isGuest>"u")&&!order?.customer_email;if(email!=null&&email!==""){const isValidEmail=validateValue(email,"customer_email","email","orders");customerEmail=Object.keys(isValidEmail).length>0}const shippingAddressCleaned=Object.keys(shippingAddress??{}).reduce((acc,key)=>({...acc,[key.replace("shipping_address_","")]:shippingAddress[key].value}),{}),{billingDisable,shippingDisable}=addressesController({invertAddresses,requiresBillingInfo:order?.requires_billing_info,billing_address:billingAddress,shipping_address:shippingAddressCleaned,shipToDifferentAddress,shippingAddressId,billingAddressId,errors,requiredMetadataFields}),countryLockDisable=countryLockController({countryCodeLock:order?.shipping_country_code_lock,addresses,shipToDifferentAddress,billingAddressId,billing_address:billingAddress,shipping_address:shippingAddress,shippingAddressId,lineItems:order?.line_items}),disable=disabled||customerEmail||billingDisable||(invertAddresses&&shippingAddressId?!1:shippingDisable)||countryLockDisable,handleClick=async()=>{if(errors&&Object.keys(errors).length===0&&!disable){setOrderErrors?.([]);let response={success:!1};switch(setForceDisable(!0),!0){case(order!=null&&addressId!=null&&createCustomerAddress!=null&&saveAddresses!=null):{response=await saveAddresses({customerEmail:email,customerAddress:{resource:invertAddresses?"shipping_address":"billing_address",id:addressId}});break}case(order!=null&&saveAddresses!=null):{response=await saveAddresses({customerEmail:email});break}case createCustomerAddress!=null:{const address=invertAddresses?{...formCleaner(shippingAddress)}:{...formCleaner(billingAddress)};addressId&&(address.id=addressId),createCustomerAddress(address),response={success:!0};break}}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;
|
|
@@ -11,7 +11,7 @@ interface BillingAddressControllerProps {
|
|
|
11
11
|
shipToDifferentAddress?: boolean;
|
|
12
12
|
requiredMetadataFields?: string[];
|
|
13
13
|
}
|
|
14
|
-
export declare function billingAddressController({ billing_address, billingAddressId, errors,
|
|
14
|
+
export declare function billingAddressController({ billing_address, billingAddressId, errors, invertAddresses, shipToDifferentAddress, shippingDisable, requiredMetadataFields, }: BillingAddressControllerProps): boolean;
|
|
15
15
|
interface ShippingAddressControllerProps {
|
|
16
16
|
billingDisable?: boolean;
|
|
17
17
|
errors?: BaseError[];
|
|
@@ -22,7 +22,7 @@ interface ShippingAddressControllerProps {
|
|
|
22
22
|
invertAddresses?: boolean;
|
|
23
23
|
requiredMetadataFields?: string[];
|
|
24
24
|
}
|
|
25
|
-
export declare function shippingAddressController({ billingDisable, errors, shipToDifferentAddress, shipping_address, shippingAddressId, invertAddresses,
|
|
25
|
+
export declare function shippingAddressController({ billingDisable, errors, shipToDifferentAddress, shipping_address, shippingAddressId, invertAddresses, requiredMetadataFields, }: ShippingAddressControllerProps): boolean;
|
|
26
26
|
interface CountryLockControllerProps {
|
|
27
27
|
addresses?: Address[] | null;
|
|
28
28
|
billing_address?: TCustomerAddress;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import isEmpty from"lodash/isEmpty";import{addressFields}from"../reducers/AddressReducer";import{fieldsExist}from"./validateFormFields";export function billingAddressController({billing_address,billingAddressId,errors,
|
|
2
|
+
import isEmpty from"lodash/isEmpty";import{addressFields}from"../reducers/AddressReducer";import{fieldsExist}from"./validateFormFields";export function billingAddressController({billing_address,billingAddressId,errors,invertAddresses=!1,shipToDifferentAddress,shippingDisable,requiredMetadataFields}){let billingDisable=invertAddresses?!!(!shippingDisable&&shipToDifferentAddress):!isEmpty(errors)||isEmpty(billing_address);if(isEmpty(errors)&&!isEmpty(billing_address)){let formFields=[...addressFields];requiredMetadataFields!=null&&requiredMetadataFields.length>0&&(formFields=[...formFields,...requiredMetadataFields]),invertAddresses?billingDisable=!!(billing_address&&fieldsExist(billing_address,formFields)):billingDisable=!!(billing_address&&fieldsExist(billing_address,formFields))}return billingDisable&&!isEmpty(billingAddressId)&&isEmpty(billing_address)&&(billingDisable=!1),billingDisable}export function shippingAddressController({billingDisable,errors,shipToDifferentAddress,shipping_address,shippingAddressId,invertAddresses=!1,requiredMetadataFields}){let shippingDisable=invertAddresses?!isEmpty(errors)||isEmpty(shipping_address):!!(!billingDisable&&shipToDifferentAddress);if(isEmpty(errors)&&!isEmpty(shipping_address)){let formField=[...addressFields];requiredMetadataFields!=null&&requiredMetadataFields.length>0&&(formField=[...formField,...requiredMetadataFields]),invertAddresses?shippingDisable=!!(shipping_address&&fieldsExist(shipping_address,formField)):shipToDifferentAddress&&(delete shipping_address?.billing_info,shippingDisable=!!(shipping_address&&fieldsExist(shipping_address,formField)))}return shippingDisable&&!isEmpty(shippingAddressId)&&isEmpty(shipping_address)&&(shippingDisable=!1),shippingDisable}export function countryLockController({addresses,billing_address,billingAddressId,countryCodeLock,shipToDifferentAddress,shipping_address,shippingAddressId,lineItems}){if(lineItems?.filter(lineItem=>lineItem?.item?.type!=null&&["skus","bundles"].includes(lineItem?.item?.type))?.every(lineItem=>lineItem?.item?.do_not_ship===!0))return!1;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===shippingAddressId&&(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&&!shippingAddressId){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]});let address;order?.shipping_address?.id?address=await sdk.addresses.update({id:order.shipping_address.id,...shippingAddress}):(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]});let address;order?.billing_address?.id?address=await sdk.addresses.update({id:order.billing_address.id,...billingAddress}):(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,requiredMetadataFields}){if(invertAddresses){const shippingDisable2=shippingAddressController({errors,shipToDifferentAddress,shipping_address,shippingAddressId,invertAddresses,requiresBillingInfo,requiredMetadataFields}),billingDisable2=billingAddressController({shippingDisable:shippingDisable2,billing_address,billingAddressId,errors,requiresBillingInfo,invertAddresses,requiredMetadataFields});return{shippingDisable:shippingDisable2,billingDisable:billingDisable2}}const billingDisable=billingAddressController({billing_address,billingAddressId,errors,requiresBillingInfo,requiredMetadataFields}),shippingDisable=shippingAddressController({billingDisable,errors,shipToDifferentAddress,shipping_address,shippingAddressId,requiredMetadataFields});return{billingDisable,shippingDisable}}export function sanitizeMetadataFields(address){const hasMetadata=Object.keys(address).filter(key=>!!key.startsWith("metadata_"));return hasMetadata?.length>0&&hasMetadata.forEach(key=>{const metadataKey=key.replace("metadata_","");address.metadata={...address.metadata||{},[metadataKey]:address[key]},delete address[key]}),address}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import { type AddressField } from
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
1
|
+
import type { AddressCreate } from "@commercelayer/sdk";
|
|
2
|
+
import type { TResourceError } from "../components/errors/Errors";
|
|
3
|
+
import { type AddressField } from "../reducers/AddressReducer";
|
|
4
|
+
import type { AddressInputName } from "../typings/index";
|
|
5
|
+
import type { BaseError } from "../typings/errors";
|
|
6
|
+
import type { BaseState } from "../typings/index";
|
|
7
7
|
export type ValidateFormFields = <R extends string[]>(fields: HTMLFormControlsCollection, required: R, resourceType: TResourceError) => {
|
|
8
8
|
errors: BaseError[];
|
|
9
9
|
values: BaseState;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import isEmpty from"lodash/isEmpty";import isString from"lodash/isString";import
|
|
2
|
+
import isEmpty from"lodash/isEmpty";import isString from"lodash/isString";import keys from"lodash/keys";import map from"lodash/map";import without from"lodash/without";import{addressFields}from"../reducers/AddressReducer";const EMAIL_PATTERN=/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;export const validateValue=(val,name,type,resource)=>val?type==="email"&&isString(val)&&!val.match(EMAIL_PATTERN)?{field:name,code:"VALIDATION_ERROR",message:"please enter a valid format",resource}:{}:{field:name,code:"VALIDATION_ERROR",message:`${name} - is required`,resource};const validateFormFields=(fields,required,resourceType)=>{const errors=[];let values={metadata:{}};return map(fields,v=>{const val="checked"in v||(v.value==="on"?!1:v.value),attrName=v.getAttribute("name");if(attrName&&required.includes(attrName)||v.required){const error=validateValue(val,v.name,v.type,resourceType);isEmpty(error)||errors.push(error),values={...values,[`${v.name}`]:val}}v.getAttribute("name")&&(values=!!v.getAttribute("data-metadata")?{...values,metadata:{...values.metadata,[`${v.name}`]:val}}:{...values,[`${v.name}`]:val})}),{errors,values}};export function fieldsExist(address,schema=addressFields){if(!address.business){const required2=without(schema,"line_2","company","state_code"),validAddress2=keys(address).filter(k=>required2.includes(k));return required2.length>validAddress2.length}const required=without(schema,"first_name","last_name","line_2","state_code"),validAddress=keys(address).filter(k=>required.includes(k));return required.length>validAddress.length}const businessOptionalFields=["billing_address_first_name","billing_address_last_name","shipping_address_first_name","shipping_address_last_name","first_name","last_name"],customerOptionalFields=["billing_address_company","shipping_address_company","company"];export function businessMandatoryField(fieldName,isBusiness){return!(isBusiness&&businessOptionalFields.includes(fieldName)||!isBusiness&&customerOptionalFields.includes(fieldName))}export default validateFormFields;
|