Skip to content

Commit

Permalink
Donations: Fall back to default products if no products are already d…
Browse files Browse the repository at this point in the history
…efined. (#43665)
  • Loading branch information
kwight committed Jun 29, 2020
1 parent ad0168c commit 569c50b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Button } from '@wordpress/components';
*/
import StripeNudge from './stripe-nudge';

const Donations = ( { stripeConnectUrl } ) => {
const Donations = ( { products, stripeConnectUrl } ) => {
const [ activeTab, setActiveTab ] = useState( 'one-time' );

const isActive = ( button ) => ( activeTab === button ? 'active' : null );
Expand Down
40 changes: 33 additions & 7 deletions apps/full-site-editing/full-site-editing-plugin/donations/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,60 @@ import Donations from './donations';
import LoadingError from './loading-error';
import LoadingStatus from './loading-status';
import UpgradePlan from './upgrade-plan';
import fetchDefaultProducts from './fetch-default-products';
import fetchStatus from './fetch-status';

const Edit = () => {
const [ isLoading, setIsLoading ] = useState( true );
const [ loadingError, setLoadingError ] = useState( '' );
const [ shouldUpgrade, setShouldUpgrade ] = useState( false );
const [ stripeConnectUrl, setStripeConnectUrl ] = useState( false );
const [ products, setProducts ] = useState( [] );
const [ upgradeUrl, setUpgradeUrl ] = useState( '' );

const apiError = ( message ) => {
setLoadingError( message );
setIsLoading( false );
};

const hasRequiredProducts = ( productList, productCurrency ) => {
const productIntervals = productList.reduce( ( intervals, { currency, type, interval } ) => {
if ( currency === productCurrency && type === 'donation' ) {
intervals.push( interval );
}
return intervals;
}, [] );

return (
productIntervals.includes( 'one-time' ) &&
productIntervals.includes( '1 month' ) &&
productIntervals.includes( '1 year' )
);
};

const mapStatusToState = ( result ) => {
if ( ( ! result && typeof result !== 'object' ) || result.errors ) {
setLoadingError( __( 'Could not load data from WordPress.com.', 'full-site-editing' ) );
} else {
setShouldUpgrade( result.should_upgrade_to_access_memberships );
setUpgradeUrl( result.upgrade_url );
setStripeConnectUrl( result.connect_url );
}

setIsLoading( false );
};
if ( result.products.length && hasRequiredProducts( result.products, 'USD' ) ) {
setProducts( result.products );
} else {
fetchDefaultProducts( 'USD' ).then(
( defaultProducts ) => setProducts( defaultProducts ),
apiError
);
}
}

const apiError = ( message ) => {
setLoadingError( message );
setIsLoading( false );
};

useEffect( () => {
const updateData = () => fetchStatus().then( mapStatusToState, apiError );
const updateData = () => fetchStatus( 'donation' ).then( mapStatusToState, apiError );
updateData();
}, [] );

Expand All @@ -54,7 +80,7 @@ const Edit = () => {
return <UpgradePlan upgradeUrl={ upgradeUrl } />;
}

return <Donations stripeConnectUrl={ stripeConnectUrl } />;
return <Donations stripeConnectUrl={ stripeConnectUrl } products={ products } />;
};

export default Edit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable wpcalypso/import-docblock */
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';

const fetchDefaultProducts = async ( currency ) => {
try {
const result = await apiFetch( {
path: '/wpcom/v2/memberships/products',
method: 'POST',
data: {
type: 'donation',
currency,
},
} );
return result;
} catch ( error ) {
return Promise.reject( error.message );
}
};

export default fetchDefaultProducts;
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
*/
import apiFetch from '@wordpress/api-fetch';

const fetchStatus = async () => {
const fetchStatus = async ( type = null ) => {
let path = '/wpcom/v2/memberships/status';
if ( type ) {
path += `?type=${ type }`;
}
try {
const result = await apiFetch( {
path: '/wpcom/v2/memberships/status',
path,
method: 'GET',
} );
return result;
Expand Down

0 comments on commit 569c50b

Please sign in to comment.