-
Notifications
You must be signed in to change notification settings - Fork 219
Create Context Provider for Notices w/ Notices API #1843
Conversation
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.
Super nice job here Mike! I love how this turned out. I have a few comments needing addressed, but on the whole this looks really good.
applyCoupon( couponCode ) | ||
.then( ( result ) => { | ||
if ( result === true ) { | ||
addSuccessNotice( | ||
sprintf( | ||
// translators: %s coupon code. | ||
__( | ||
'Coupon code "%s" has been applied to your cart', | ||
'woo-gutenberg-products-block' | ||
), | ||
couponCode | ||
), | ||
{ | ||
id: 'coupon-form', | ||
} | ||
); | ||
} | ||
} ) | ||
.catch( ( error ) => { | ||
addErrorNotice( error.message, { | ||
id: 'coupon-form', | ||
} ); | ||
} ); |
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'm fine with leaving this for now (and great job 👏 ), but it relies on actionCreators always returning a promise that resolves to the action result. There was some discussion around this behaviour in GB core at one point but it looks like that got resolved by a pull (I did!) done here. For some reason, I thought that we were eventually going to revert that but looks like it's okay. Worth noting that the discussion here, might have some impact.
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'll defer to your advice here @nerrad. The alternative was having these dispatch calls in the applyCoupon
action itself (resolver.js) but it felt very opinionated to throw real notices there rather than via this hook. Any other/better suggestions I'm open.
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.
Let's leave it, even considering the unresolved discussion I don't think it will affect this implementation.
const noticesApi = useMemo( | ||
() => ( { | ||
addDefaultNotice: ( text, noticeProps = {} ) => |
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.
noice!
@nerrad When I was working on this I noticed |
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.
Still a couple more comments but this can be merged after those are resolved so pre-approving 👍
@@ -6,6 +6,7 @@ import classnames from 'classnames'; | |||
import withComponentId from '@woocommerce/base-hocs/with-component-id'; |
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.
This is an unused import that can be removed.
</Notice> | ||
) | ||
) } | ||
<div className={ wrapperClass } key={ instanceId }> |
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.
You don't need to have a key on the container (I should have caught this earlier). Keys are only needed on children within a specific component. My first review I was thinking the instanceId was being used for an actual ID, but it looks like it's not needed at all here?
This PR implements a system for creating and outputting notices for a given context.
Fixes #1795
Usage
Wrap your components in a
StoreNoticesProvider
component and give it a custom context.Too create notices, use the
useStoreNotices
hook. Example:This hook providers a series of methods for notice manipulation under the current context:
addDefaultNotice
addErrorNotice
addWarningNotice
addInfoNotice
addSuccessNotice
removeNotices
And finally, notices are rendered in a container using the Notice component (https://github.com/WordPress/gutenberg/tree/master/packages/components/src/notice) -- these are given woocommerce class names for styling.
Screenshots
How to test the changes in this Pull Request: