Skip to content

Commit

Permalink
#375 Feed billing and shipping address form from graphql using with r…
Browse files Browse the repository at this point in the history
…espect to field config mapping
  • Loading branch information
rajeev-k-tomy committed Nov 8, 2024
1 parent 8b94a08 commit 34af785
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 49 deletions.
23 changes: 23 additions & 0 deletions src/reactapp/src/addressFieldGqlMapping.js
Original file line number Diff line number Diff line change
@@ -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',
};
39 changes: 17 additions & 22 deletions src/reactapp/src/api/cart/setBillingAddress/modifier.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
35 changes: 13 additions & 22 deletions src/reactapp/src/api/cart/setShippingAddress/modifier.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions src/reactapp/src/components/billingAddress/utility/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ 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 =
'additional.billing_address_selected_other_option';

export const billingAddressFormInitValues = {
...addressInitValues(BILLING_ADDR_FORM),
country_id: initialCountry,
isSameAsShipping: LocalStorage.getBillingSameAsShippingInfo(),
saveInBook: false,
saveInBook: LocalStorage.getBillingSameAsShippingInfo(),
};

export function selectedAddressTitle(isLoggedIn, customerAddressList) {
Expand Down
4 changes: 2 additions & 2 deletions src/reactapp/src/utils/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand Down

0 comments on commit 34af785

Please sign in to comment.