-
Notifications
You must be signed in to change notification settings - Fork 219
Introduce Additional Fields extensibility API. #12073
base: trunk
Are you sure you want to change the base?
Changes from 33 commits
e1e8306
50c372a
33a07c1
6f90954
48ee2dd
88ee619
95cf895
164f469
c09894c
9e70517
17a604a
5140eaa
25f4394
d1fcd85
4ab27fd
8f0c8b4
2f0c1b3
42fbad6
a368f42
d323cd1
443a133
fa8e4c8
d48c613
36f1460
041d724
552b0f5
60fb0b6
8ccaee8
3a02c06
6c14f15
58d422b
c416753
9d35d14
856cf89
6361137
3641977
0fdfd6c
db497dc
70bf9ef
5ada340
0ab9b3f
704ce90
9d25427
53694ad
f8567fb
b9aa65e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,12 @@ import type { | |
CartResponseBillingAddress, | ||
CartResponseShippingAddress, | ||
} from '@woocommerce/types'; | ||
import { | ||
AddressFields, | ||
defaultAddressFields, | ||
ShippingAddress, | ||
BillingAddress, | ||
} from '@woocommerce/settings'; | ||
import { ShippingAddress, BillingAddress } from '@woocommerce/settings'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { | ||
SHIPPING_COUNTRIES, | ||
SHIPPING_STATES, | ||
ADDRESS_FIELDS_KEYS, | ||
} from '@woocommerce/block-settings'; | ||
|
||
/** | ||
|
@@ -26,10 +22,9 @@ export const isSameAddress = < T extends ShippingAddress | BillingAddress >( | |
address1: T, | ||
address2: T | ||
): boolean => { | ||
return Object.keys( defaultAddressFields ).every( | ||
( field: string ) => | ||
address1[ field as keyof T ] === address2[ field as keyof T ] | ||
); | ||
return Object.keys( ADDRESS_FIELDS_KEYS ).every( ( field: string ) => { | ||
return address1[ field as keyof T ] === address2[ field as keyof T ]; | ||
} ); | ||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would previously check against all fields, it's changed to only check address. |
||
}; | ||
|
||
/** | ||
|
@@ -94,10 +89,11 @@ export const emptyHiddenAddressFields = < | |
>( | ||
address: T | ||
): T => { | ||
const fields = Object.keys( | ||
defaultAddressFields | ||
) as ( keyof AddressFields )[]; | ||
const addressFields = prepareAddressFields( fields, {}, address.country ); | ||
const addressFields = prepareAddressFields( | ||
ADDRESS_FIELDS_KEYS, | ||
{}, | ||
address.country | ||
); | ||
const newAddress = Object.assign( {}, address ) as T; | ||
|
||
addressFields.forEach( ( { key = '', hidden = false } ) => { | ||
|
@@ -160,10 +156,11 @@ export const isAddressComplete = ( | |
if ( ! address.country ) { | ||
return false; | ||
} | ||
const fields = Object.keys( | ||
defaultAddressFields | ||
) as ( keyof AddressFields )[]; | ||
const addressFields = prepareAddressFields( fields, {}, address.country ); | ||
const addressFields = prepareAddressFields( | ||
ADDRESS_FIELDS_KEYS, | ||
{}, | ||
address.country | ||
); | ||
|
||
return addressFields.every( | ||
( { key = '', hidden = false, required = false } ) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,8 @@ export const shippingAddressHasValidationErrors = () => { | |
|
||
export type BaseAddressKey = | ||
| keyof CartBillingAddress | ||
| keyof CartShippingAddress; | ||
| keyof CartShippingAddress | ||
| string; // string here because custom checkout fields can be added with arbitrary keys. | ||
|
||
/** | ||
* Normalizes address values before push. | ||
|
@@ -82,12 +83,23 @@ export const getDirtyKeys = < | |
previousAddress | ||
) as BaseAddressKey[]; | ||
|
||
return previousAddressKeys.filter( ( key: BaseAddressKey ) => { | ||
return ( | ||
normalizeAddressProp( key, previousAddress[ key ] ) !== | ||
normalizeAddressProp( key, address[ key ] ) | ||
); | ||
} ); | ||
const addedKeys = Object.keys( address ).filter( | ||
( key ) => ! previousAddressKeys.includes( key ) | ||
); | ||
|
||
const removedKeys = previousAddressKeys.filter( | ||
( key ) => ! Object.keys( address ).includes( key ) | ||
); | ||
|
||
return previousAddressKeys | ||
.filter( ( key: BaseAddressKey ) => { | ||
return ( | ||
normalizeAddressProp( key, previousAddress[ key ] ) !== | ||
normalizeAddressProp( key, address[ key ] ) | ||
); | ||
} ) | ||
.concat( removedKeys ) | ||
.concat( addedKeys ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add an inline comment explaining why this is needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @opr as I'm also not sure what this mean, but it helped get additional fields picked up and pushed. |
||
}; | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,12 @@ type CountryData = { | |
locale: Record< string, LocaleSpecificAddressField >; | ||
}; | ||
|
||
type FieldsLocations = { | ||
address: string[]; | ||
contact: string[]; | ||
additional: string[]; | ||
}; | ||
|
||
// Contains country names. | ||
const countries = getSetting< Record< string, string > >( 'countries', {} ); | ||
|
||
|
@@ -111,3 +117,35 @@ export const COUNTRY_LOCALE = Object.fromEntries( | |
return [ countryCode, countryData[ countryCode ].locale || [] ]; | ||
} ) | ||
); | ||
|
||
const defaultFieldsLocations: FieldsLocations = { | ||
address: [ | ||
'first_name', | ||
'last_name', | ||
'company', | ||
'address_1', | ||
'address_2', | ||
'city', | ||
'postcode', | ||
'country', | ||
'state', | ||
'phone', | ||
], | ||
contact: [ 'email' ], | ||
additional: [], | ||
}; | ||
Comment on lines
+121
to
+136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This default alone isn't enough actually, because without field definition, Checkout would still not render right. |
||
|
||
export const ADDRESS_FIELDS_KEYS = getSetting< FieldsLocations >( | ||
'addressFieldsLocations', | ||
defaultFieldsLocations | ||
).address; | ||
|
||
export const CONTACT_FIELDS_KEYS = getSetting< FieldsLocations >( | ||
'addressFieldsLocations', | ||
defaultFieldsLocations | ||
).contact; | ||
|
||
export const ADDITIONAL_FIELDS_KEYS = getSetting< FieldsLocations >( | ||
'addressFieldsLocations', | ||
defaultFieldsLocations | ||
).additional; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing the whole fields as default no longer make sense,
fields
is now required.