-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update EditorCheckoutModal to use ShoppingCartProvider #46860
Conversation
const { site, isOpen, onClose, cartData } = this.props; | ||
const user = userFactory(); | ||
const isLoggedOutCart = ! user?.get(); | ||
const waitForOtherCartUpdates = false; |
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.
The function getCartKey
expects waitForOtherCartUpdates
but whether we need it or not I am unsure. I am also not too sure what this value is for, or if we can safely assume it's value is going to be false
.
It's also used in this file to reference but could use another opinion on this.
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 think we'll have to ask @sirbrillig for this one.
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.
There are two situations where the ShoppingCartProvider needs to wait for the old CartStore in new checkout. I'm not sure if either of them apply to your use-case.
- If some other page has added a product to the cart and then redirected to checkout without waiting for the cart transaction to complete. This creates a race condition between the two cart managers, since ShoppingCartProvider might query the cart before the change request completes, causing it to get the wrong version of the cart. This should really be fixed on the other end by waiting before the page redirects, but that requires finding everywhere that might modify the cart in calypso and I haven't had the time yet. So
waitForOtherCartUpdates
is true if the CartStore is pending updates. - The Registrationless checkout flow uses localStorage to keep a list of products for the cart which are loaded by the CartStore during signup. We need to make sure that when the ShoppingCartProvider loads the cart, that those changes have completed and that we pull them in from the CartStore properly. Ultimately, we should be solving this by making the ShoppingCartProvider capable of dealing with localStorage instead of the CartStore, but I haven't had time for that yet.
Here is how your PR affects size of JS and CSS bundles shipped to the user's browser: Sections (~48 bytes removed 📉 [gzipped])
Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to. Async-loaded Components (~3114 bytes added 📈 [gzipped])
React components that are loaded lazily, when a certain part of UI is displayed for the first time. Legend What is parsed and gzip size?Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory. Generated by performance advisor bot at iscalypsofastyet.com. |
<ShoppingCartProvider | ||
cartKey={ cartKey } | ||
getCart={ () => getCart( site, cartData ) } | ||
setCart={ () => getCart( site, cartData ) } |
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.
Updating cart isn't working as can be seen in https://cloudup.com/cmgW2kDO5yC
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.
🤔 It's missing the arguments and is also not actually calling the endpoint. Here's the types for these two functions:
setCart: ( cartKey: string, requestCart: RequestCart ) => Promise< ResponseCart >;
getCart: ( cartKey: string ) => Promise< ResponseCart >;
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.
Reviewed and tested. LGTM!
const { site, isOpen, onClose, cartData } = this.props; | ||
const user = userFactory(); | ||
const isLoggedOutCart = ! user?.get(); | ||
const waitForOtherCartUpdates = false; |
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 think we'll have to ask @sirbrillig for this one.
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.
We should maintain the capability to update cart items. See #46860 (comment).
The following changes would fix the setCart functionality, but it would ignore the diff --git a/client/blocks/editor-checkout-modal/index.tsx b/client/blocks/editor-checkout-modal/index.tsx
index 6581e4394f..2bdaadd8de 100644
--- a/client/blocks/editor-checkout-modal/index.tsx
+++ b/client/blocks/editor-checkout-modal/index.tsx
@@ -7,7 +7,7 @@ import wp from 'calypso/lib/wp';
import classnames from 'classnames';
import { Button } from '@wordpress/components';
import { Icon, close, wordpress } from '@wordpress/icons';
-import { ShoppingCartProvider } from '@automattic/shopping-cart';
+import { ShoppingCartProvider, RequestCart } from '@automattic/shopping-cart';
/**
* Internal dependencies
@@ -17,9 +17,8 @@ import { fetchStripeConfiguration } from 'calypso/my-sites/checkout/composite-ch
import CompositeCheckout from 'calypso/my-sites/checkout/composite-checkout/composite-checkout';
import { getSelectedSite } from 'calypso/state/ui/selectors';
import getCartKey from 'calypso/my-sites/checkout/get-cart-key';
-import type { SiteData } from 'calypso/state/ui/selectors/site-data';
import userFactory from 'calypso/lib/user';
-import type { CartData, Props } from './types';
+import type { Props } from './types';
/**
* Style dependencies
@@ -27,14 +26,9 @@ import type { CartData, Props } from './types';
import './style.scss';
const wpcom = wp.undocumented();
-
-async function getCart( site: SiteData, cartData: CartData ) {
- try {
- return await wpcom.setCart( site.ID, cartData );
- } catch {
- return;
- }
-}
+const wpcomGetCart = ( cartKey: string ) => wpcom.getCart( cartKey );
+const wpcomSetCart = ( cartKey: string, requestCart: RequestCart ) =>
+ wpcom.setCart( cartKey, requestCart );
function fetchStripeConfigurationWpcom( args: Record< string, unknown > ) {
return fetchStripeConfiguration( args, wpcom );
@@ -71,18 +65,9 @@ const EditorCheckoutModal = ( props: Props ) => {
<Icon icon={ close } size={ 24 } />
</Button>
</div>
- <ShoppingCartProvider
- cartKey={ cartKey }
- getCart={ () => getCart( site, cartData ) }
- setCart={ () => getCart( site, cartData ) }
- >
+ <ShoppingCartProvider cartKey={ cartKey } getCart={ wpcomGetCart } setCart={ wpcomSetCart }>
<StripeHookProvider fetchStripeConfiguration={ fetchStripeConfigurationWpcom }>
- <CompositeCheckout
- isInEditor
- siteId={ site.ID }
- siteSlug={ site.slug }
- getCart={ () => getCart( site, cartData ) }
- />
+ <CompositeCheckout isInEditor siteId={ site.ID } siteSlug={ site.slug } />
</StripeHookProvider>
</ShoppingCartProvider>
</div> |
Ah, it comes from
I might also be misunderstanding this! In which case, just ignore me, but I'm happy to help fix this in any way I can. Sorry for the breakage caused by #46532! I totally forgot that there was another consumer of |
I might have found where that action gets triggered from some JS on the back-end. It looks like the value of this might only be a plan slug. I'm not sure if you can use the slugs raw or not, but I think it should work. |
… cart prior to opening modal.
These changes have been implemented.
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.
Tested editing the cart and continued the checkout. Works as expected.
…ic/shopping-cart`
Changes proposed in this Pull Request
EditorCheckoutModal
component to use theShoppingCartProvider
which is needed as the sub-components within this checkout modal make use ofuseShoppingCart
which need the provider.Testing instructions