From 34af785965bf13d4d604cb6f7f9f329f64d680f7 Mon Sep 17 00:00:00 2001 From: Rajeev K Tomy Date: Fri, 8 Nov 2024 11:31:21 +0100 Subject: [PATCH] #375 Feed billing and shipping address form from graphql using with respect to field config mapping --- src/reactapp/src/addressFieldGqlMapping.js | 23 +++++++++++ .../api/cart/setBillingAddress/modifier.js | 39 ++++++++----------- .../api/cart/setShippingAddress/modifier.js | 35 +++++++---------- .../billingAddress/utility/index.js | 5 +-- src/reactapp/src/utils/address.js | 4 +- 5 files changed, 57 insertions(+), 49 deletions(-) create mode 100644 src/reactapp/src/addressFieldGqlMapping.js diff --git a/src/reactapp/src/addressFieldGqlMapping.js b/src/reactapp/src/addressFieldGqlMapping.js new file mode 100644 index 00000000..a52346eb --- /dev/null +++ b/src/reactapp/src/addressFieldGqlMapping.js @@ -0,0 +1,23 @@ +/** + * Here it maps field config data with graphql data. + * + * If you have custom fields, then most probably you need to pass this data + * via graphql (using custom implementation) and then if it is a part of graphql + * repsonse `cart.billing_address` or `cart.shipping_address[0]`, then you can + * add the corresponding mapping here. + * + * Key ==> Field config reference + * Value ==> Graphql reference. + */ +export default { + city: 'city', + street: 'street', + company: 'company', + lastname: 'lastname', + postcode: 'postcode', + region: 'region.code', + firstname: 'firstname', + telephone: 'telephone', + country_id: 'country.code', + // vat_id: 'vat_id', +}; diff --git a/src/reactapp/src/api/cart/setBillingAddress/modifier.js b/src/reactapp/src/api/cart/setBillingAddress/modifier.js index f36922fb..441d7e8d 100644 --- a/src/reactapp/src/api/cart/setBillingAddress/modifier.js +++ b/src/reactapp/src/api/cart/setBillingAddress/modifier.js @@ -1,39 +1,34 @@ import { get as _get } from 'lodash-es'; +import { + addressConfig, + addressTypeMapper, + isCartAddressValid, +} from '../../../utils/address'; +import { _objToArray } from '../../../utils'; +import { BILLING_ADDR_FORM } from '../../../config'; import LocalStorage from '../../../utils/localStorage'; import { prepareFullName } from '../../../utils/customer'; -import { isCartAddressValid } from '../../../utils/address'; +import addressFieldGqlMapping from '../../../addressFieldGqlMapping'; export function modifyBillingAddressData(billingAddress) { if (!isCartAddressValid(billingAddress)) { return {}; } - const { - company = '', - firstname = '', - lastname = '', - street = [], - telephone: phone = '', - postcode: zipcode = '', - city = '', - country: { code: countryCode = '' } = {}, - region: { code: regionCode = '' } = {}, - } = billingAddress; - return { + const fieldsConfig = addressConfig[addressTypeMapper[BILLING_ADDR_FORM]]; + const billingAddressData = { id: LocalStorage.getCustomerBillingAddressId(), - company, - firstname, - lastname, fullName: prepareFullName(billingAddress), - street, - phone, - zipcode, - city, - region: regionCode || '', - country_id: countryCode, isSameAsShipping: LocalStorage.getBillingSameAsShippingInfo(), }; + + _objToArray(fieldsConfig).forEach((config) => { + const fieldName = addressFieldGqlMapping[config.code] || config.code; + billingAddressData[config.code] = _get(billingAddress, fieldName) || ''; + }); + + return billingAddressData; } export default function setBillingAddressModifier(response) { diff --git a/src/reactapp/src/api/cart/setShippingAddress/modifier.js b/src/reactapp/src/api/cart/setShippingAddress/modifier.js index d0dfbc53..b318948f 100644 --- a/src/reactapp/src/api/cart/setShippingAddress/modifier.js +++ b/src/reactapp/src/api/cart/setShippingAddress/modifier.js @@ -1,7 +1,11 @@ import { get as _get } from 'lodash-es'; +import { _objToArray } from '../../../utils'; import { formatPrice } from '../../../utils/price'; +import { SHIPPING_ADDR_FORM } from '../../../config'; import { prepareFullName } from '../../../utils/customer'; +import addressFieldGqlMapping from '../../../addressFieldGqlMapping'; +import { addressConfig, addressTypeMapper } from '../../../utils/address'; export function modifySelectedShippingMethod(addressList) { const selectedMethod = _get(addressList, '0.selected_shipping_method'); @@ -65,30 +69,17 @@ export function modifyShippingAddressList(addressList) { return {}; } - const { - city, - street, - company, - lastname, - firstname, - telephone: phone, - postcode: zipcode, - region: { code: regionCode }, - country: { code: countryCode }, - } = shippingAddress; - - return { - city, - phone, - street, - company, - zipcode, - lastname, - firstname, - region: regionCode || '', - country_id: countryCode, + const fieldsConfig = addressConfig[addressTypeMapper[SHIPPING_ADDR_FORM]]; + const shippingAddressData = { fullName: prepareFullName(shippingAddress), }; + + _objToArray(fieldsConfig).forEach((config) => { + const fieldName = addressFieldGqlMapping[config.code] || config.code; + shippingAddressData[config.code] = _get(shippingAddress, fieldName) || ''; + }); + + return shippingAddressData; } export default function setShippingAddressModifier(result) { diff --git a/src/reactapp/src/components/billingAddress/utility/index.js b/src/reactapp/src/components/billingAddress/utility/index.js index 7d44318b..ac097b50 100644 --- a/src/reactapp/src/components/billingAddress/utility/index.js +++ b/src/reactapp/src/components/billingAddress/utility/index.js @@ -2,7 +2,7 @@ import { __ } from '../../../i18n'; import { _isObjEmpty } from '../../../utils'; import { BILLING_ADDR_FORM } from '../../../config'; import LocalStorage from '../../../utils/localStorage'; -import { addressInitValues, initialCountry } from '../../../utils/address'; +import { addressInitValues } from '../../../utils/address'; export const CART_BILLING_ADDRESS = 'cart_billing_address'; export const billingAddrOtherOptionField = @@ -10,9 +10,8 @@ export const billingAddrOtherOptionField = export const billingAddressFormInitValues = { ...addressInitValues(BILLING_ADDR_FORM), - country_id: initialCountry, isSameAsShipping: LocalStorage.getBillingSameAsShippingInfo(), - saveInBook: false, + saveInBook: LocalStorage.getBillingSameAsShippingInfo(), }; export function selectedAddressTitle(isLoggedIn, customerAddressList) { diff --git a/src/reactapp/src/utils/address.js b/src/reactapp/src/utils/address.js index 6b5cb253..61950d17 100644 --- a/src/reactapp/src/utils/address.js +++ b/src/reactapp/src/utils/address.js @@ -22,9 +22,9 @@ export const CART_SHIPPING_ADDRESS = 'cart_shipping_address'; export const billingSameAsShippingField = `${BILLING_ADDR_FORM}.isSameAsShipping`; -const addressConfig = checkoutConfig.address || {}; +export const addressConfig = checkoutConfig.address || {}; -const addressTypeMapper = { +export const addressTypeMapper = { [BILLING_ADDR_FORM]: 'billing', [SHIPPING_ADDR_FORM]: 'shipping', };