Skip to content
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

Merged
merged 4 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 62 additions & 53 deletions client/blocks/editor-checkout-modal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
* External dependencies
*/
import React, { Component } from 'react';
import React, { useMemo } from 'react';
import { connect } from 'react-redux';
import wp from 'calypso/lib/wp';
import classnames from 'classnames';
import { Button } from '@wordpress/components';
import { Icon, close, wordpress } from '@wordpress/icons';
import { ShoppingCartProvider, RequestCart } from '@automattic/shopping-cart';

/**
* Internal dependencies
Expand All @@ -15,6 +16,9 @@ import { StripeHookProvider } from 'calypso/lib/stripe';
import { fetchStripeConfiguration } from 'calypso/my-sites/checkout/composite-checkout/payment-method-helpers';
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';

/**
* Style dependencies
Expand All @@ -23,74 +27,79 @@ import './style.scss';

const wpcom = wp.undocumented();

type Site = {
ID: number;
slug: string;
};
const wpcomGetCart = ( cartKey: string ) => wpcom.getCart( cartKey );
const wpcomSetCart = ( cartKey: string, requestCart: RequestCart ) =>
wpcom.setCart( cartKey, requestCart );

export interface CartData {
products: Array< {
product_id: number;
product_slug: string;
} >;
function fetchStripeConfigurationWpcom( args: Record< string, unknown > ) {
return fetchStripeConfiguration( args, wpcom );
}

type Props = {
site: Site;
cartData: CartData;
onClose: () => void;
isOpen: boolean;
};

class EditorCheckoutModal extends Component< Props > {
static defaultProps = {
isOpen: false,
onClose: () => null,
cartData: {},
};

async getCart() {
// Important: If getCart or cartData is empty, it will redirect to the plans page in customer home.
const { site, cartData } = this.props;
const EditorCheckoutModal = ( props: Props ) => {
const { site, isOpen, onClose, cartData } = props;
const hasEmptyCart = ! cartData.products || cartData.products.length < 1;

try {
return await wpcom.setCart( site.ID, cartData );
} catch {
return;
}
}
const user = userFactory();
const isLoggedOutCart = ! user?.get();
const waitForOtherCartUpdates = false;
Copy link
Contributor Author

@tjcafferkey tjcafferkey Oct 28, 2020

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.

Copy link
Member

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.

Copy link
Member

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.

  1. 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.
  2. 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.

// We can assume if they're accessing the checkout in an editor that they have a site.
const isNoSiteCart = false;

render() {
const { site, isOpen, onClose, cartData } = this.props;
const cartKey = useMemo(
() =>
getCartKey( {
selectedSite: site,
isLoggedOutCart,
isNoSiteCart,
waitForOtherCartUpdates,
} ),
[ waitForOtherCartUpdates, site, isLoggedOutCart, isNoSiteCart ]
);

const hasEmptyCart = ! cartData.products || cartData.products.length < 1;
// We need to pass in a comma separated list of product
// slugs to be set in the cart otherwise we will be
// redirected to the plans page due to an empty cart
const productSlugs = hasEmptyCart
? null
: cartData.products.map( ( product ) => product.product_slug );
const commaSeparatedProductSlugs = productSlugs?.join( ',' ) || null;

return hasEmptyCart ? null : (
<div className={ classnames( 'editor-checkout-modal', isOpen ? 'is-open' : '' ) }>
<div className="editor-checkout-modal__header">
<div className="editor-checkout-modal__wp-logo">
<Icon icon={ wordpress } size={ 36 } />
</div>
<Button isLink className="editor-checkout-modal__close-button" onClick={ onClose }>
<Icon icon={ close } size={ 24 } />
</Button>
return hasEmptyCart ? null : (
<div className={ classnames( 'editor-checkout-modal', isOpen ? 'is-open' : '' ) }>
<div className="editor-checkout-modal__header">
<div className="editor-checkout-modal__wp-logo">
<Icon icon={ wordpress } size={ 36 } />
</div>
<Button isLink className="editor-checkout-modal__close-button" onClick={ onClose }>
<Icon icon={ close } size={ 24 } />
</Button>
</div>
<ShoppingCartProvider cartKey={ cartKey } getCart={ wpcomGetCart } setCart={ wpcomSetCart }>
<StripeHookProvider fetchStripeConfiguration={ fetchStripeConfigurationWpcom }>
<CompositeCheckout
isInEditor
siteId={ site.ID }
siteSlug={ site.slug }
getCart={ this.getCart.bind( this ) }
productAliasFromUrl={ commaSeparatedProductSlugs }
/>
</StripeHookProvider>
</div>
);
}
}
</ShoppingCartProvider>
</div>
);
};

function fetchStripeConfigurationWpcom( args: Record< string, unknown > ) {
return fetchStripeConfiguration( args, wpcom );
}
type Props = {
site: SiteData;
cartData: RequestCart;
onClose: () => void;
isOpen: boolean;
};

EditorCheckoutModal.defaultProps = {
isOpen: false,
onClose: () => null,
cartData: {},
};

export default connect( ( state ) => ( {
site: getSelectedSite( state ),
Expand Down
4 changes: 2 additions & 2 deletions client/gutenberg/editor/calypsoify-iframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import { REASON_BLOCK_EDITOR_UNKOWN_IFRAME_LOAD_FAILURE } from 'calypso/state/de
import { setMediaLibrarySelectedItems } from 'calypso/state/media/actions';
import { fetchMediaItem, getMediaItem } from 'calypso/state/media/thunks';
import Iframe from './iframe';
import type { CartData } from 'calypso/client/blocks/editor-checkout-modal';
import { RequestCart } from '@automattic/shopping-cart';
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved

/**
* Types
Expand Down Expand Up @@ -94,7 +94,7 @@ interface State {
multiple?: any;
postUrl?: T.URL;
previewUrl: T.URL;
cartData?: CartData;
cartData?: RequestCart;
}
/* eslint-enable @typescript-eslint/no-explicit-any */

Expand Down