-
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
Checkout: Update G Suite nudge to replace CartData with withShoppingCart #48438
Changes from 6 commits
8ebe8ad
0dd7591
c36e790
55fb669
c1bdf79
c981672
c8ab957
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 ); | ||
}; | ||
|
||
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 = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Upon further inspection maybe the plain is supposed to be removed. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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 ) ) ); |
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.
Is this a pretty safe assumption for the redirect? I was looking through the previous handler and it seemed pretty complicated.
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.
getThankYouPageUrl
was created by meticulously going through the redirect portion ofhandleCheckoutCompleteRedirect
and replicating it, so it's at least as accurate as checkout. (And after this PR, we'll be able to removehandleCheckoutCompleteRedirect
entirely!)