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

Domain Site Redirect: Use shoppingCartManager to add products in SiteRedirectStep #50282

Merged
merged 7 commits into from
Feb 23, 2021
5 changes: 2 additions & 3 deletions client/my-sites/domains/controller.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
getSelectedSiteSlug,
} from 'calypso/state/ui/selectors';
import { getCurrentUser } from 'calypso/state/current-user/selectors';
import CartData from 'calypso/components/data/cart';
import DomainSearch from './domain-search';
import SiteRedirect from './domain-search/site-redirect';
import MapDomain from 'calypso/my-sites/domains/map-domain';
Expand Down Expand Up @@ -97,9 +96,9 @@ const siteRedirect = ( context, next ) => {
title="Domain Search > Site Redirect"
/>
<DocumentHead title={ translate( 'Redirect a Site' ) } />
<CartData>
<CalypsoShoppingCartProvider>
<SiteRedirect />
</CartData>
</CalypsoShoppingCartProvider>
</Main>
);
next();
Expand Down
33 changes: 27 additions & 6 deletions client/my-sites/domains/domain-search/site-redirect-step.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React from 'react';
import { connect } from 'react-redux';
import { localize } from 'i18n-calypso';
import { get } from 'lodash';
import { withShoppingCart } from '@automattic/shopping-cart';

/**
* Internal dependencies
Expand All @@ -18,9 +19,9 @@ import { hasProduct, siteRedirect } from 'calypso/lib/cart-values/cart-items';
import { errorNotice } from 'calypso/state/notices/actions';
import { canRedirect } from 'calypso/lib/domains';
import DomainProductPrice from 'calypso/components/domains/domain-product-price';
import { addItem } from 'calypso/lib/cart/actions';
import { recordGoogleEvent } from 'calypso/state/analytics/actions';
import { withoutHttp } from 'calypso/lib/url';
import { fillInSingleCartItemAttributes } from 'calypso/lib/cart-values';

/**
* Style dependencies
Expand All @@ -29,12 +30,21 @@ import './site-redirect-step.scss';

class SiteRedirectStep extends React.Component {
static propTypes = {
cart: PropTypes.object.isRequired,
products: PropTypes.object.isRequired,
selectedSite: PropTypes.object.isRequired,
};

state = { searchQuery: '' };
state = { searchQuery: '', isSubmitting: false };

isMounted = false;

componentWillUnmount() {
this.isMounted = false;
}

componentDidMount() {
this.isMounted = true;
}

render() {
const price = get( this.props, 'products.offsite_redirect.cost_display', null );
Expand Down Expand Up @@ -68,6 +78,8 @@ class SiteRedirectStep extends React.Component {
className="site-redirect-step__go"
type="submit"
onClick={ this.recordGoButtonClick }
busy={ this.state.isSubmitting }
disabled={ this.state.isSubmitting }
>
{ translate( 'Go', {
context: 'Upgrades: Label for adding Site Redirect',
Expand Down Expand Up @@ -95,6 +107,8 @@ class SiteRedirectStep extends React.Component {
handleFormSubmit = ( event ) => {
event.preventDefault();

this.setState( { isSubmitting: true } );

const domain = this.state.searchQuery;

this.props.recordFormSubmit( domain );
Expand All @@ -103,6 +117,7 @@ class SiteRedirectStep extends React.Component {
this.props.errorNotice(
this.getValidationErrorMessage( domain, { code: 'already_in_cart' } )
);
this.setState( { isSubmitting: false } );
return;
}

Expand All @@ -112,6 +127,7 @@ class SiteRedirectStep extends React.Component {
function ( error ) {
if ( error ) {
this.props.errorNotice( this.getValidationErrorMessage( domain, error ) );
this.setState( { isSubmitting: false } );
return;
}

Expand All @@ -121,8 +137,13 @@ class SiteRedirectStep extends React.Component {
};

addSiteRedirectToCart = ( domain ) => {
addItem( siteRedirect( { domain } ) );
page( '/checkout/' + this.props.selectedSite.slug );
this.props.shoppingCartManager
.addProductsToCart( [
fillInSingleCartItemAttributes( siteRedirect( { domain } ), this.props.products ),
] )
.then( () => {
this.isMounted && page( '/checkout/' + this.props.selectedSite.slug );
} );
};

getValidationErrorMessage = ( domain, error ) => {
Expand Down Expand Up @@ -189,4 +210,4 @@ export default connect( null, {
recordInputFocus,
recordGoButtonClick,
recordFormSubmit,
} )( localize( SiteRedirectStep ) );
} )( withShoppingCart( localize( SiteRedirectStep ) ) );
4 changes: 1 addition & 3 deletions client/my-sites/domains/domain-search/site-redirect.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { getProductsList } from 'calypso/state/products-list/selectors';

class SiteRedirect extends Component {
static propTypes = {
cart: PropTypes.object.isRequired,
selectedSite: PropTypes.object.isRequired,
selectedSiteSlug: PropTypes.string.isRequired,
isSiteAtomic: PropTypes.bool.isRequired,
Expand Down Expand Up @@ -59,7 +58,6 @@ class SiteRedirect extends Component {

render() {
const {
cart,
selectedSite,
selectedSiteAdminUrl,
isSiteAtomic,
Expand Down Expand Up @@ -87,7 +85,7 @@ class SiteRedirect extends Component {
{ translate( 'Redirect a Site' ) }
</HeaderCake>

<SiteRedirectStep cart={ cart } products={ productsList } selectedSite={ selectedSite } />
<SiteRedirectStep products={ productsList } selectedSite={ selectedSite } />
</Main>
);
}
Expand Down