Skip to content

Commit

Permalink
Merge pull request #536 from Automattic/fix/122-purchases-with-site-r…
Browse files Browse the repository at this point in the history
…edirect

Purchases: Individual purchases not loading on sites with Site Redirect
  • Loading branch information
drewblaisdell committed Dec 3, 2015
2 parents a7c39bd + f76af43 commit 4810232
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
10 changes: 8 additions & 2 deletions client/components/data/purchases/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React from 'react';
* Internal dependencies
*/
import { fetchUserPurchases } from 'lib/upgrades/actions';
import observe from 'lib/mixins/data-observe';
import PurchasesStore from 'lib/purchases/store';
import StoreConnection from 'components/data/store-connection';
import userFactory from 'lib/user';
Expand All @@ -20,16 +21,20 @@ const stores = [ PurchasesStore ],
function getStateFromStores( props ) {
return {
noticeType: props.noticeType,
purchases: PurchasesStore.getByUser( user.get().ID )
purchases: PurchasesStore.getByUser( user.get().ID ),
sites: props.sites
};
}

const PurchasesData = React.createClass( {
propTypes: {
component: React.PropTypes.func.isRequired,
noticeType: React.PropTypes.string
noticeType: React.PropTypes.string,
sites: React.PropTypes.object.isRequired
},

mixins: [ observe( 'sites' ) ],

componentDidMount() {
fetchUserPurchases();
},
Expand All @@ -39,6 +44,7 @@ const PurchasesData = React.createClass( {
<StoreConnection
component={ this.props.component }
noticeType={ this.props.noticeType }
sites={ this.props.sites }
stores={ stores }
getStateFromStores={ getStateFromStores } />
);
Expand Down
9 changes: 8 additions & 1 deletion client/lib/purchases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@ import { isDomainMapping, isDomainRegistration, isTheme, isPlan } from 'lib/prod
* Returns an array of sites objects, each of which contains an array of purchases.
*
* @param {array} purchases An array of purchase objects.
* @param {array} sites An array of site objects
* @return {array} An array of sites with purchases attached.
*/
function getPurchasesBySite( purchases ) {
function getPurchasesBySite( purchases, sites ) {
return purchases.reduce( ( result, currentValue ) => {
const site = find( result, { id: currentValue.siteId } );
if ( site ) {
site.purchases = site.purchases.concat( currentValue );
} else {
const siteObject = find( sites, { ID: currentValue.siteId } );

result = result.concat( {
domain: currentValue.domain,
id: currentValue.siteId,
name: currentValue.siteName,
/* if the purchase is attached to a deleted site,
* there will be no site with this ID in `sites`, so
* we fall back on the domain. */
slug: siteObject ? siteObject.slug : currentValue.domain,
title: currentValue.siteName ? currentValue.siteName : currentValue.domain,
purchases: [ currentValue ]
} );
Expand Down
3 changes: 2 additions & 1 deletion client/me/purchases/controller.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ export default {
renderPage(
<PurchasesData
component={ PurchasesList }
noticeType={ context.params.noticeType } />
noticeType={ context.params.noticeType }
sites={ sites } />
);
},

Expand Down
22 changes: 15 additions & 7 deletions client/me/purchases/list/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,32 @@ const PurchasesList = React.createClass( {
);
},

render() {
let content;
isDataLoading() {
if ( this.props.purchases.isFetching && ! this.props.purchases.hasLoadedFromServer ) {
return true;
}

if ( ! this.props.purchases.isFetching && ! this.props.purchases.hasLoadedFromServer ) {
return null;
if ( ! this.props.sites.initialized ) {
return true;
}

if ( this.props.purchases.isFetching && ! this.props.purchases.hasLoadedFromServer ) {
return false;
},

render() {
let content;

if ( this.isDataLoading() ) {
content = <PurchasesSite isPlaceholder />;
}

if ( this.props.purchases.hasLoadedFromServer && this.props.purchases.data.length ) {
content = getPurchasesBySite( this.props.purchases.data ).map(
content = getPurchasesBySite( this.props.purchases.data, this.props.sites.get() ).map(
site => (
<PurchasesSite
key={ site.id }
name={ site.title }
domain={ site.domain }
slug={ site.slug }
purchases={ site.purchases } />
)
);
Expand Down
4 changes: 2 additions & 2 deletions client/me/purchases/list/item/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {

const PurchaseItem = React.createClass( {
propTypes: {
domain: React.PropTypes.string,
slug: React.PropTypes.string,
purchase: React.PropTypes.object,
isPlaceholder: React.PropTypes.bool
},
Expand Down Expand Up @@ -131,7 +131,7 @@ const PurchaseItem = React.createClass( {

if ( ! isPlaceholder ) {
props = {
href: paths.managePurchase( this.props.domain, this.props.purchase.id ),
href: paths.managePurchase( this.props.slug, this.props.purchase.id ),
onClick: this.scrollToTop
};
}
Expand Down
4 changes: 2 additions & 2 deletions client/me/purchases/list/site/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import PurchaseItem from '../item';

const PurchasesSite = React.createClass( {
propTypes: {
domain: React.PropTypes.string,
slug: React.PropTypes.string,
name: React.PropTypes.string,
purchases: React.PropTypes.array,
isPlaceholder: React.PropTypes.bool
Expand All @@ -39,7 +39,7 @@ const PurchasesSite = React.createClass( {
this.props.purchases.map( purchase => (
<PurchaseItem
key={ purchase.id }
domain={ this.props.domain }
slug={ this.props.slug }
purchase={ purchase } />
)
)
Expand Down

0 comments on commit 4810232

Please sign in to comment.