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

Checkout: Update G Suite nudge to replace CartData with withShoppingCart #48438

Merged
merged 7 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 3 additions & 2 deletions client/my-sites/checkout/controller.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import UpsellNudge, {
CONCIERGE_SUPPORT_SESSION,
CONCIERGE_QUICKSTART_SESSION,
} from './upsell-nudge';
import CalypsoShoppingCartProvider from 'calypso/my-sites/checkout/calypso-shopping-cart-provider';

export function checkout( context, next ) {
const { feature, plan, domainOrProduct, purchaseId } = context.params;
Expand Down Expand Up @@ -175,13 +176,13 @@ export function gsuiteNudge( context, next ) {
}

context.primary = (
<CheckoutContainer purchaseId={ Number( receiptId ) }>
<CalypsoShoppingCartProvider>
<GSuiteNudge
domain={ domain }
receiptId={ Number( receiptId ) }
selectedSiteId={ selectedSite.ID }
/>
</CheckoutContainer>
</CalypsoShoppingCartProvider>
);

next();
Expand Down
57 changes: 41 additions & 16 deletions client/my-sites/checkout/gsuite-nudge/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { connect } from 'react-redux';
import { localize } from 'i18n-calypso';
import { get, some, compact } from 'lodash';
import page from 'page';
import { withShoppingCart } from '@automattic/shopping-cart';

/**
* Internal dependencies
Expand All @@ -19,10 +20,12 @@ import QuerySites from 'calypso/components/data/query-sites';
import { getSiteSlug, getSiteTitle } from 'calypso/state/sites/selectors';
import { getReceiptById } from 'calypso/state/receipts/selectors';
import isEligibleForDotcomChecklist from 'calypso/state/selectors/is-eligible-for-dotcom-checklist';
import { addItems, removeItem } from 'calypso/lib/cart/actions';
import { getAllCartItems } from 'calypso/lib/cart-values/cart-items';
import { isDotComPlan } from 'calypso/lib/products-values';
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
import { fillInSingleCartItemAttributes } from 'calypso/lib/cart-values';
import { getProductsList } from 'calypso/state/products-list/selectors/get-products-list';
import getThankYouPageUrl from 'calypso/my-sites/checkout/composite-checkout/hooks/use-get-thank-you-url/get-thank-you-page-url';

/**
* Style dependencies
Expand All @@ -36,29 +39,50 @@ export class GSuiteNudge extends React.Component {
selectedSiteId: PropTypes.number.isRequired,
};

isMounted = false;

componentDidMount() {
this.isMounted = true;
}

componentWillUnmount() {
this.isMounted = false;
}

handleSkipClick = () => {
this.props.handleCheckoutCompleteRedirect();
const getThankYouPageUrlArguments = {
siteSlug: this.props.siteSlug,
receiptId: this.props.receiptId,
cart: this.props.cart,
};
const url = getThankYouPageUrl( getThankYouPageUrlArguments );
page.redirect( url );
Comment on lines +52 to +58
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a pretty safe assumption for the redirect? I was looking through the previous handler and it seemed pretty complicated.

Copy link
Member Author

Choose a reason for hiding this comment

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

getThankYouPageUrl was created by meticulously going through the redirect portion of handleCheckoutCompleteRedirect and replicating it, so it's at least as accurate as checkout. (And after this PR, we'll be able to remove handleCheckoutCompleteRedirect entirely!)

};

handleAddEmailClick = ( cartItems ) => {
const { siteSlug, receiptId } = this.props;
const { siteSlug, receiptId, productsList } = this.props;
this.removePlanFromCart();

addItems(
// add `receipt_for_domain` to cartItem extras
cartItems.map( ( item ) => ( {
...item,
extra: { ...item.extra, receipt_for_domain: receiptId },
} ) )
);

page( `/checkout/${ siteSlug }` );
this.props.shoppingCartManager
.addProductsToCart(
// add `receipt_for_domain` to cartItem extras
cartItems
.map( ( item ) => ( {
...item,
extra: { ...item.extra, receipt_for_domain: receiptId },
} ) )
.map( ( item ) => fillInSingleCartItemAttributes( item, productsList ) )
)
.then( () => {
this.isMounted && page( `/checkout/${ siteSlug }` );
} );
};

removePlanFromCart() {
removePlanFromCart = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Upon further inspection maybe the plain is supposed to be removed. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think it is. But I think I actually translated this logic backwards.

Copy link
Member Author

Choose a reason for hiding this comment

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

But if you still saw the plan removed, that implies that it was happening on the server, so this code may in fact be unnecessary. I'll verify.

Copy link
Member Author

Choose a reason for hiding this comment

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

You know, I think it is unused because you're not likely to get to this upsell unless the cart is empty (as you've just made a purchase).

const items = getAllCartItems( this.props.cart );
items.filter( isDotComPlan ).forEach( ( item ) => removeItem( item, false ) );
}
const filteredProducts = items.filter( isDotComPlan );
this.props.shoppingCartManager.replaceProductsInCart( filteredProducts );
};

render() {
const { domain, receiptId, selectedSiteId, siteSlug, siteTitle, translate } = this.props;
Expand Down Expand Up @@ -101,5 +125,6 @@ export default connect( ( state, props ) => {
siteSlug: getSiteSlug( state, props.selectedSiteId ),
siteTitle: getSiteTitle( state, props.selectedSiteId ),
isEligibleForChecklist,
productsList: getProductsList( state ),
};
} )( localize( GSuiteNudge ) );
} )( withShoppingCart( localize( GSuiteNudge ) ) );