Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Create Context Provider for Notices w/ Notices API #1843

Merged
merged 14 commits into from
Mar 3, 2020

Conversation

mikejolley
Copy link
Member

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.

import StoreNoticesProvider from '@woocommerce/base-context/store-notices-context';
<StoreNoticesProvider context="wc/cart">
		{children}
</StoreNoticesProvider>

Too create notices, use the useStoreNotices hook. Example:

const {
		addSuccessNotice
} = useStoreNotices();

addSuccessNotice( "Well done!" );

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

Screenshot 2020-03-02 at 14 17 45

How to test the changes in this Pull Request:

  1. Go to the cart.
  2. Apply a coupon - see message.
  3. Apply it again. See error.
  4. Remove the coupon. See info message.

@mikejolley mikejolley added status: needs review focus: components Work that introduces new or updates existing components. skip-changelog PRs that you don't want to appear in the changelog. labels Mar 2, 2020
@mikejolley mikejolley requested a review from a team as a code owner March 2, 2020 14:31
@mikejolley mikejolley self-assigned this Mar 2, 2020
@mikejolley mikejolley requested review from Aljullu and removed request for a team March 2, 2020 14:31
Copy link
Contributor

@nerrad nerrad left a 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.

assets/js/base/components/store-notices-container/index.js Outdated Show resolved Hide resolved
assets/js/base/components/store-notices-container/index.js Outdated Show resolved Hide resolved
assets/js/base/components/store-notices-container/index.js Outdated Show resolved Hide resolved
assets/js/base/context/store-notices-context.js Outdated Show resolved Hide resolved
assets/js/base/context/store-notices-context.js Outdated Show resolved Hide resolved
Comment on lines +40 to +62
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',
} );
} );
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Comment on lines +10 to +12
const noticesApi = useMemo(
() => ( {
addDefaultNotice: ( text, noticeProps = {} ) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noice!

@mikejolley
Copy link
Member Author

@nerrad When I was working on this I noticed DEFAULT_EMPTY_ARRAY was being imported from the wrong file, but the test was using undefined instead of [] (DEFAULT_EMPTY_ARRAY). I updated that and the test here. Should pass now.

Copy link
Contributor

@nerrad nerrad left a 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';
Copy link
Contributor

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 }>
Copy link
Contributor

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
focus: components Work that introduces new or updates existing components. skip-changelog PRs that you don't want to appear in the changelog.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create notices Context Provider and implement with hook
3 participants