-
Notifications
You must be signed in to change notification settings - Fork 219
Capture notices from hidden block into siblings block #8390
Changes from 1 commit
6e2d826
708c1b5
e3179cf
4abc453
a99279d
02b9cfa
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 |
---|---|---|
|
@@ -91,7 +91,14 @@ const Block = ( { | |
|
||
return ( | ||
<AddressFormWrapperComponent> | ||
<StoreNoticesContainer context={ noticeContexts.BILLING_ADDRESS } /> | ||
<StoreNoticesContainer | ||
context={ noticeContexts.BILLING_ADDRESS } | ||
capturedContexts={ | ||
useBillingAsShipping | ||
? [ noticeContexts.SHIPPING_ADDRESS ] | ||
: [] | ||
} | ||
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. I don't feel strongly about approach, another option would be to call |
||
/> | ||
<AddressForm | ||
id="billing" | ||
type="billing" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,11 @@ const Block = ( { | |
<AddressFormWrapperComponent> | ||
<StoreNoticesContainer | ||
context={ noticeContexts.SHIPPING_ADDRESS } | ||
capturedContexts={ | ||
useShippingAsBilling | ||
? [ noticeContexts.BILLING_ADDRESS ] | ||
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. Do we need to take into account the WC settings where a merchant can default to the shipping address or force shipping address to the customer billing address? This seems to work ok when I tested, but I'm not sure why. The logic you have here is that the error should be displayed in the billing address context, if 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.
The logic here is that if we have the billing form hidden and its value being synced to shipping, then we also want to capture those notices and present them in the shipping step.
The logic I have here is that if 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. Gotcha that makes more sense |
||
: [] | ||
} | ||
/> | ||
<AddressForm | ||
id="shipping" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import { | |
} from '@woocommerce/block-data'; | ||
import { getNoticeContexts } from '@woocommerce/base-utils'; | ||
import type { Notice } from '@wordpress/notices'; | ||
import { useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -24,9 +25,19 @@ const formatNotices = ( notices: Notice[], context: string ): StoreNotice[] => { | |
} ) ) as StoreNotice[]; | ||
}; | ||
|
||
const removeDuplicateNotices = ( | ||
notice: Notice, | ||
noticeIndex: number, | ||
noticesArray: Notice[] | ||
) => | ||
noticesArray.findIndex( | ||
( _notice: Notice ) => _notice.content === notice.content | ||
) === noticeIndex; | ||
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 makes sense. |
||
|
||
const StoreNoticesContainer = ( { | ||
className = '', | ||
context, | ||
capturedContexts = [], | ||
additionalNotices = [], | ||
}: StoreNoticesContainerProps ): JSX.Element | null => { | ||
const { suppressNotices, registeredContainers } = useSelect( | ||
|
@@ -46,23 +57,31 @@ const StoreNoticesContainer = ( { | |
subContext.includes( context + '/' ) && | ||
! registeredContainers.includes( subContext ) | ||
); | ||
|
||
const contexts = useMemo( | ||
() => [ context, ...capturedContexts ], | ||
[ capturedContexts, context ] | ||
); | ||
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 caused an infinite loop so that's why I added it. |
||
// Get notices from the current context and any sub-contexts and append the name of the context to the notice | ||
// objects for later reference. | ||
|
||
const notices = useSelect< StoreNotice[] >( ( select ) => { | ||
const { getNotices } = select( 'core/notices' ); | ||
|
||
return [ | ||
...unregisteredSubContexts.flatMap( ( subContext: string ) => | ||
formatNotices( getNotices( subContext ), subContext ) | ||
), | ||
...capturedContexts.flatMap( ( capturedNotice: string ) => | ||
formatNotices( getNotices( capturedNotice ), capturedNotice ) | ||
), | ||
...formatNotices( | ||
getNotices( context ).concat( additionalNotices ), | ||
context | ||
), | ||
].filter( Boolean ) as StoreNotice[]; | ||
] | ||
.filter( removeDuplicateNotices ) | ||
.filter( Boolean ) as StoreNotice[]; | ||
} ); | ||
|
||
if ( suppressNotices || ! notices.length ) { | ||
return null; | ||
} | ||
|
@@ -71,7 +90,7 @@ const StoreNoticesContainer = ( { | |
<> | ||
<StoreNotices | ||
className={ className } | ||
context={ context } | ||
contexts={ contexts } | ||
notices={ notices.filter( | ||
( notice ) => notice.type === 'default' | ||
) } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,11 @@ import { getClassNameFromStatus } from './utils'; | |
import type { StoreNotice } from './types'; | ||
|
||
const StoreNotices = ( { | ||
context, | ||
contexts, | ||
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. You'll need to update the docs here as well to reflect plural |
||
className, | ||
notices, | ||
}: { | ||
context: string; | ||
contexts: string[]; | ||
className: string; | ||
notices: StoreNotice[]; | ||
} ): JSX.Element => { | ||
|
@@ -67,11 +67,11 @@ const StoreNotices = ( { | |
|
||
// Register the container context with the parent. | ||
useEffect( () => { | ||
registerContainer( context ); | ||
contexts.map( ( context ) => registerContainer( context ) ); | ||
return () => { | ||
unregisterContainer( context ); | ||
contexts.map( ( context ) => unregisterContainer( context ) ); | ||
}; | ||
}, [ context, registerContainer, unregisterContainer ] ); | ||
}, [ contexts, registerContainer, unregisterContainer ] ); | ||
|
||
// Group notices by whether or not they are dismissible. Dismissible notices can be grouped. | ||
const dismissibleNotices = notices.filter( | ||
|
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.
I would prefer if we could somehow define an array of context here rather than the separate prop.