Skip to content

Commit

Permalink
Merge pull request #10936 from Automattic/update/create-cart-without-…
Browse files Browse the repository at this point in the history
…site

Signup: Change signup flow for domain only sites
  • Loading branch information
drewblaisdell authored Feb 2, 2017
2 parents 636ea15 + dd9d46b commit 87b7652
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 73 deletions.
10 changes: 5 additions & 5 deletions client/lib/cart/store/cart-synchronizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ function preprocessCartForServer( cart ) {
return newCart;
}

function CartSynchronizer( siteID, wpcom ) {
function CartSynchronizer( cartKey, wpcom ) {
if ( ! ( this instanceof CartSynchronizer ) ) {
return new CartSynchronizer( siteID, wpcom );
return new CartSynchronizer( cartKey, wpcom );
}

this._siteID = siteID;
this._cartKey = cartKey;
this._wpcom = wpcom;
this._latestValue = null;
this._hasLoadedFromServer = false;
Expand Down Expand Up @@ -151,7 +151,7 @@ CartSynchronizer.prototype._processQueuedChanges = function() {
};

CartSynchronizer.prototype._postToServer = function( callback ) {
this._wpcom.cart( this._siteID, 'POST', preprocessCartForServer( this._latestValue ), function( error, newValue ) {
this._wpcom.cart( this._cartKey, 'POST', preprocessCartForServer( this._latestValue ), function( error, newValue ) {
if ( error ) {
callback( error );
return;
Expand All @@ -170,7 +170,7 @@ CartSynchronizer.prototype.fetch = function() {
};

CartSynchronizer.prototype._getFromServer = function( callback ) {
this._wpcom.cart( this._siteID, 'GET', function( error, newValue ) {
this._wpcom.cart( this._cartKey, 'GET', function( error, newValue ) {
if ( error ) {
callback( error );
return;
Expand Down
15 changes: 7 additions & 8 deletions client/lib/cart/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var UpgradesActionTypes = require( 'lib/upgrades/constants' ).action,
applyCoupon = cartValues.applyCoupon,
cartItems = cartValues.cartItems;

var _selectedSiteID = null,
var _cartKey = null,
_synchronizer = null,
_poller = null;

Expand Down Expand Up @@ -50,23 +50,22 @@ function hasPendingServerUpdates() {
function setSelectedSite() {
var selectedSite = sites.getSelectedSite();

if ( ! selectedSite ) {
_selectedSiteID = null;
if ( _cartKey === selectedSite.ID ) {
return;
}

if ( _selectedSiteID === selectedSite.ID ) {
return;
if ( ! selectedSite ) {
_cartKey = 'no-site';
} else {
_cartKey = selectedSite.ID;
}

if ( _synchronizer && _poller ) {
PollerPool.remove( _poller );
_synchronizer.off( 'change', emitChange );
}

_selectedSiteID = selectedSite.ID;

_synchronizer = cartSynchronizer( selectedSite.ID, wpcom );
_synchronizer = cartSynchronizer( _cartKey, wpcom );
_synchronizer.on( 'change', emitChange );

_poller = PollerPool.add( CartStore, _synchronizer._poll.bind( _synchronizer ) );
Expand Down
16 changes: 8 additions & 8 deletions client/lib/cart/store/test/cart-synchronizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CartSynchronizer from '../cart-synchronizer';
import FakeWPCOM from './fake-wpcom';
import useFilesystemMocks from 'test/helpers/use-filesystem-mocks';

var TEST_SITE_ID = 91234567890;
var TEST_CART_KEY = 91234567890;

var poller = {
add: function() {}
Expand All @@ -31,7 +31,7 @@ describe( 'cart-synchronizer', function() {
describe( '*before* the first fetch from the server', function() {
it( 'should *not* allow the value to be read', function() {
var wpcom = FakeWPCOM(),
synchronizer = CartSynchronizer( TEST_SITE_ID, wpcom, poller );
synchronizer = CartSynchronizer( TEST_CART_KEY, wpcom, poller );

assert.throws( () => {
synchronizer.getLatestValue();
Expand All @@ -40,8 +40,8 @@ describe( 'cart-synchronizer', function() {

it( 'should enqueue local changes and POST them after fetching', function() {
var wpcom = FakeWPCOM(),
synchronizer = CartSynchronizer( TEST_SITE_ID, wpcom, poller ),
serverCart = emptyCart( TEST_SITE_ID );
synchronizer = CartSynchronizer( TEST_CART_KEY, wpcom, poller ),
serverCart = emptyCart( TEST_CART_KEY );

synchronizer.fetch();
synchronizer.update( applyCoupon( 'foo' ) );
Expand All @@ -63,8 +63,8 @@ describe( 'cart-synchronizer', function() {
describe( '*after* the first fetch from the server', function() {
it( 'should allow the value to be read', function() {
var wpcom = FakeWPCOM(),
synchronizer = CartSynchronizer( TEST_SITE_ID, wpcom, poller ),
serverCart = emptyCart( TEST_SITE_ID );
synchronizer = CartSynchronizer( TEST_CART_KEY, wpcom, poller ),
serverCart = emptyCart( TEST_CART_KEY );

synchronizer.fetch();
wpcom.resolveRequest( 0, serverCart );
Expand All @@ -75,8 +75,8 @@ describe( 'cart-synchronizer', function() {

it( 'should make local changes visible immediately', function() {
var wpcom = FakeWPCOM(),
synchronizer = CartSynchronizer( TEST_SITE_ID, wpcom, poller ),
serverCart = emptyCart( TEST_SITE_ID );
synchronizer = CartSynchronizer( TEST_CART_KEY, wpcom, poller ),
serverCart = emptyCart( TEST_CART_KEY );

synchronizer.fetch();
wpcom.resolveRequest( 0, serverCart );
Expand Down
6 changes: 3 additions & 3 deletions client/lib/signup/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var wpcom = require( 'lib/wp' ),
cartItems = cartValues.cartItems;

module.exports = {
addToCart: function( siteSlug, newCartItems, callback ) {
wpcom.undocumented().cart( siteSlug, function( error, data ) {
addToCart: function( cartKey, newCartItems, callback ) {
wpcom.undocumented().cart( cartKey, function( error, data ) {
if ( error ) {
return callback( error );
}
Expand All @@ -33,7 +33,7 @@ module.exports = {
newCart = cartValues.fillInAllCartItemAttributes( addFunction( newCart ), productsList.get() );
} );

wpcom.undocumented().cart( siteSlug, 'POST', newCart, function( postError ) {
wpcom.undocumented().cart( cartKey, 'POST', newCart, function( postError ) {
callback( postError );
} );
} );
Expand Down
27 changes: 24 additions & 3 deletions client/lib/signup/step-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ import {
import { getSiteTitle } from 'state/signup/steps/site-title/selectors';
import { getSurveyVertical, getSurveySiteType } from 'state/signup/steps/survey/selectors';

function createCart( callback, dependencies, data ) {
const { designType } = dependencies;
const { domainItem, themeItem } = data;

if ( designType === 'domain' ) {
const cartKey = 'no-site';
const providedDependencies = {
siteId: null,
siteSlug: cartKey,
domainItem,
themeItem
};

SignupCart.addToCart( cartKey, [ domainItem ], error => callback( error, providedDependencies ) );
} else {
createSiteWithCart( callback, dependencies, data );
}
}

function createSiteWithCart( callback, dependencies, {
cartItem,
domainItem,
Expand All @@ -49,8 +68,6 @@ function createSiteWithCart( callback, dependencies, {
// query. See `getThemeSlug` in `DomainsStep`.
theme: dependencies.themeSlugWithRepo || themeSlugWithRepo,
vertical: surveyVertical || undefined,
// the API wants the `is_domain_only` flag provided as a number
is_domain_only: dependencies.designType === 'domain' ? 1 : 0
},
validate: false,
find_available_url: isPurchasingItem
Expand Down Expand Up @@ -222,7 +239,9 @@ function getUsernameSuggestion( username, reduxState ) {
}

module.exports = {
createSiteWithCart: createSiteWithCart,
createCart,

createSiteWithCart,

createSiteWithCartAndStartFreeTrial( callback, dependencies, data ) {
createSiteWithCart( ( error, providedDependencies ) => {
Expand Down Expand Up @@ -302,6 +321,8 @@ module.exports = {
} );
},

fetchSitesAndUser: fetchSitesAndUser,

setThemeOnSite: setThemeOnSite,

getUsernameSuggestion: getUsernameSuggestion
Expand Down
8 changes: 4 additions & 4 deletions client/lib/wpcom-undocumented/lib/undocumented.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,14 @@ Undocumented.prototype.getSitePlans = function( siteDomain, fn ) {
/**
* GET/POST cart
*
* @param {string} [siteDomain] The site's slug
* @param {string} [cartKey] The cart's key
* @param {string} [method] The request method
* @param {object} [data] The REQUEST data
* @param {Function} fn The callback function
* @api public
*/
Undocumented.prototype.cart = function( siteDomain, method, data, fn ) {
debug( '/sites/:site_id:/shopping-cart query' );
Undocumented.prototype.cart = function( cartKey, method, data, fn ) {
debug( '/me/shopping-cart/:cart-key query' );
if ( arguments.length === 2 ) {
fn = method;
method = 'GET';
Expand All @@ -661,7 +661,7 @@ Undocumented.prototype.cart = function( siteDomain, method, data, fn ) {
data = {};
}
return this._sendRequestWithLocale( {
path: '/sites/' + siteDomain + '/shopping-cart',
path: '/me/shopping-cart/' + cartKey,
method: method,
body: data
}, fn );
Expand Down
12 changes: 11 additions & 1 deletion client/my-sites/upgrades/cart/cart-plan-ad.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { connect } from 'react-redux';
import { localize } from 'i18n-calypso';
import page from 'page';
import React, { Component } from 'react';
import React, { Component, PropTypes } from 'react';

/**
* Internal dependencies
Expand All @@ -31,6 +31,7 @@ class CartPlanAd extends Component {
cart.hasLoadedFromServer &&
! cartItems.hasDomainCredit( cart ) &&
cartItems.getDomainRegistrations( cart ).length === 1 &&
selectedSite &&
selectedSite.plan &&
! isPlan( selectedSite.plan );
}
Expand All @@ -54,6 +55,15 @@ class CartPlanAd extends Component {
}
}

CartPlanAd.propTypes = {
cart: PropTypes.object.isRequired,
isDomainOnlySite: PropTypes.bool,
selectedSite: PropTypes.oneOfType( [
PropTypes.bool,
PropTypes.object
] )
};

export default connect(
( state ) => {
const selectedSiteId = getSelectedSiteId( state );
Expand Down
10 changes: 9 additions & 1 deletion client/my-sites/upgrades/cart/secondary-cart.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import React from 'react';
import React, { PropTypes } from 'react';
import { localize } from 'i18n-calypso';

/**
Expand All @@ -17,6 +17,14 @@ import observe from 'lib/mixins/data-observe';
import CartBodyLoadingPlaceholder from 'my-sites/upgrades/cart/cart-body/loading-placeholder';

const SecondaryCart = React.createClass( {
propTypes: {
cart: PropTypes.object.isRequired,
selectedSite: PropTypes.oneOfType( [
PropTypes.bool,
PropTypes.object
] )
},

mixins: [ CartMessagesMixin, observe( 'sites' ) ],

render() {
Expand Down
Loading

0 comments on commit 87b7652

Please sign in to comment.