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

Donations: Fall back to default products if no products are already defined. #43665

Merged
merged 5 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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