Skip to content

Commit d041699

Browse files
committed
Restore the downgrade
1 parent 3d11a86 commit d041699

File tree

2 files changed

+74
-65
lines changed

2 files changed

+74
-65
lines changed

client/me/purchases/cancel-purchase/button.jsx

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {
22
isDomainRegistration,
3-
getMonthlyPlanByYearly,
4-
getPlan,
53
isJetpackPlan,
64
isJetpackProduct,
75
isAkismetProduct,
@@ -21,15 +19,10 @@ import {
2119
isOneTimePurchase,
2220
isSubscription,
2321
} from 'calypso/lib/purchases';
24-
import {
25-
cancelAndRefundPurchase,
26-
extendPurchaseWithFreeMonth,
27-
} from 'calypso/lib/purchases/actions';
2822
import { getPurchaseCancellationFlowType } from 'calypso/lib/purchases/utils';
2923
import { purchasesRoot } from 'calypso/me/purchases/paths';
3024
import { errorNotice, successNotice } from 'calypso/state/notices/actions';
3125
import { clearPurchases } from 'calypso/state/purchases/actions';
32-
import { getDowngradePlanFromPurchase } from 'calypso/state/purchases/selectors';
3326
import { refreshSitePlans } from 'calypso/state/sites/plans/actions';
3427
import { MarketPlaceSubscriptionsDialog } from '../marketplace-subscriptions-dialog';
3528
import { willShowDomainOptionsRadioButtons } from './domain-options';
@@ -52,6 +45,9 @@ class CancelPurchaseButton extends Component {
5245
isLoading: PropTypes.bool,
5346
onDialogClose: PropTypes.func,
5447
onSetLoading: PropTypes.func,
48+
// Methods from parent component
49+
downgradeClick: PropTypes.func,
50+
freeMonthOfferClick: PropTypes.func,
5551
};
5652

5753
static defaultProps = {
@@ -105,59 +101,6 @@ class CancelPurchaseButton extends Component {
105101
page.redirect( this.props.purchaseListUrl );
106102
};
107103

108-
downgradeClick = ( upsell ) => {
109-
const { purchase } = this.props;
110-
let downgradePlan = getDowngradePlanFromPurchase( purchase );
111-
if ( 'downgrade-monthly' === upsell ) {
112-
const monthlyProductSlug = getMonthlyPlanByYearly( purchase.productSlug );
113-
downgradePlan = getPlan( monthlyProductSlug );
114-
}
115-
116-
this.setDisabled( true );
117-
118-
cancelAndRefundPurchase(
119-
purchase.id,
120-
{
121-
product_id: purchase.productId,
122-
type: 'downgrade',
123-
to_product_id: downgradePlan.getProductId(),
124-
},
125-
( error, response ) => {
126-
this.setDisabled( false );
127-
128-
if ( error ) {
129-
this.props.errorNotice( error.message );
130-
return;
131-
}
132-
133-
this.props.refreshSitePlans( purchase.siteId );
134-
this.props.clearPurchases();
135-
this.props.successNotice( response.message, { displayOnNextPage: true } );
136-
page.redirect( this.props.purchaseListUrl );
137-
}
138-
);
139-
};
140-
141-
freeMonthOfferClick = async () => {
142-
const { purchase } = this.props;
143-
144-
this.setDisabled( true );
145-
146-
try {
147-
const res = await extendPurchaseWithFreeMonth( purchase.id );
148-
if ( res.status === 'completed' ) {
149-
this.props.refreshSitePlans( purchase.siteId );
150-
this.props.clearPurchases();
151-
this.props.successNotice( res.message, { displayOnNextPage: true } );
152-
page.redirect( this.props.purchaseListUrl );
153-
}
154-
} catch ( err ) {
155-
this.props.errorNotice( err.message );
156-
} finally {
157-
this.setDisabled( false );
158-
}
159-
};
160-
161104
shouldHandleMarketplaceSubscriptions() {
162105
const { activeSubscriptions } = this.props;
163106

@@ -227,8 +170,8 @@ class CancelPurchaseButton extends Component {
227170
isVisible={ showDialog }
228171
onClose={ this.closeDialog }
229172
onSurveyComplete={ this.handleSurveyComplete }
230-
downgradeClick={ this.downgradeClick }
231-
freeMonthOfferClick={ this.freeMonthOfferClick }
173+
downgradeClick={ this.props.downgradeClick }
174+
freeMonthOfferClick={ this.props.freeMonthOfferClick }
232175
flowType={ getPurchaseCancellationFlowType( purchase ) }
233176
cancelBundledDomain={ cancelBundledDomain }
234177
includedDomainPurchase={ includedDomainPurchase }

client/me/purchases/cancel-purchase/index.jsx

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isJetpackProduct,
88
isAkismetProduct,
99
getPlan,
10+
getMonthlyPlanByYearly,
1011
} from '@automattic/calypso-products';
1112
import page from '@automattic/calypso-router';
1213
import { Card, CompactCard } from '@automattic/components';
@@ -34,7 +35,12 @@ import {
3435
isRefundable,
3536
isSubscription,
3637
} from 'calypso/lib/purchases';
37-
import { cancelPurchaseAsync, cancelAndRefundPurchaseAsync } from 'calypso/lib/purchases/actions';
38+
import {
39+
cancelPurchaseAsync,
40+
cancelAndRefundPurchaseAsync,
41+
cancelAndRefundPurchase,
42+
extendPurchaseWithFreeMonth,
43+
} from 'calypso/lib/purchases/actions';
3844
import { getPurchaseCancellationFlowType } from 'calypso/lib/purchases/utils';
3945
import CancelPurchaseLoadingPlaceholder from 'calypso/me/purchases/cancel-purchase/loading-placeholder';
4046
import { managePurchase, purchasesRoot } from 'calypso/me/purchases/paths';
@@ -43,14 +49,15 @@ import PurchaseSiteHeader from 'calypso/me/purchases/purchases-site/header';
4349
import TrackPurchasePageView from 'calypso/me/purchases/track-purchase-page-view';
4450
import { isDataLoading } from 'calypso/me/purchases/utils';
4551
import { recordTracksEvent } from 'calypso/state/analytics/actions';
46-
import { successNotice } from 'calypso/state/notices/actions';
52+
import { successNotice, errorNotice } from 'calypso/state/notices/actions';
4753
import { getProductsList } from 'calypso/state/products-list/selectors';
4854
import { clearPurchases } from 'calypso/state/purchases/actions';
4955
import {
5056
getByPurchaseId,
5157
getSitePurchases,
5258
hasLoadedUserPurchasesFromServer,
5359
getIncludedDomainPurchase,
60+
getDowngradePlanFromPurchase,
5461
} from 'calypso/state/purchases/selectors';
5562
import getAtomicTransfer from 'calypso/state/selectors/get-atomic-transfer';
5663
import { getDomainsBySiteId } from 'calypso/state/sites/domains/selectors';
@@ -316,6 +323,59 @@ class CancelPurchase extends Component {
316323
}
317324
};
318325

326+
downgradeClick = ( upsell ) => {
327+
const { purchase } = this.props;
328+
let downgradePlan = getDowngradePlanFromPurchase( purchase );
329+
if ( 'downgrade-monthly' === upsell ) {
330+
const monthlyProductSlug = getMonthlyPlanByYearly( purchase.productSlug );
331+
downgradePlan = getPlan( monthlyProductSlug );
332+
}
333+
334+
this.setState( { isLoading: true } );
335+
336+
cancelAndRefundPurchase(
337+
purchase.id,
338+
{
339+
product_id: purchase.productId,
340+
type: 'downgrade',
341+
to_product_id: downgradePlan.getProductId(),
342+
},
343+
( error, response ) => {
344+
this.setState( { isLoading: false } );
345+
346+
if ( error ) {
347+
this.props.errorNotice( error.message );
348+
return;
349+
}
350+
351+
this.props.refreshSitePlans( purchase.siteId );
352+
this.props.clearPurchases();
353+
this.props.successNotice( response.message, { displayOnNextPage: true } );
354+
page.redirect( this.props.purchaseListUrl );
355+
}
356+
);
357+
};
358+
359+
freeMonthOfferClick = async () => {
360+
const { purchase } = this.props;
361+
362+
this.setState( { isLoading: true } );
363+
364+
try {
365+
const res = await extendPurchaseWithFreeMonth( purchase.id );
366+
if ( res.status === 'completed' ) {
367+
this.props.refreshSitePlans( purchase.siteId );
368+
this.props.clearPurchases();
369+
this.props.successNotice( res.message, { displayOnNextPage: true } );
370+
page.redirect( this.props.purchaseListUrl );
371+
}
372+
} catch ( err ) {
373+
this.props.errorNotice( err.message );
374+
} finally {
375+
this.setState( { isLoading: false } );
376+
}
377+
};
378+
319379
onAtomicRevertConfirmationChange = ( isConfirmed ) => {
320380
this.setState( { atomicRevertConfirmed: isConfirmed } );
321381
};
@@ -498,6 +558,8 @@ class CancelPurchase extends Component {
498558
isLoading={ this.state.isLoading }
499559
onDialogClose={ this.onDialogClose }
500560
onSetLoading={ this.onSetLoading }
561+
downgradeClick={ this.downgradeClick }
562+
freeMonthOfferClick={ this.freeMonthOfferClick }
501563
/>
502564
);
503565
};
@@ -685,6 +747,8 @@ class CancelPurchase extends Component {
685747
isLoading={ this.state.isLoading }
686748
onDialogClose={ this.onDialogClose }
687749
onSetLoading={ this.onSetLoading }
750+
downgradeClick={ this.downgradeClick }
751+
freeMonthOfferClick={ this.freeMonthOfferClick }
688752
/>
689753
{ this.renderKeepSubscriptionButton() }
690754
</div>
@@ -742,6 +806,8 @@ class CancelPurchase extends Component {
742806
cancelBundledDomain={ this.state.cancelBundledDomain }
743807
includedDomainPurchase={ this.props.includedDomainPurchase }
744808
cancellationInProgress={ this.state.isLoading }
809+
downgradeClick={ this.downgradeClick }
810+
freeMonthOfferClick={ this.freeMonthOfferClick }
745811
/>
746812
) }
747813
<Card className="cancel-purchase__wrapper-card">
@@ -816,5 +882,5 @@ export default connect(
816882
atomicTransfer: getAtomicTransfer( state, purchase?.siteId ),
817883
};
818884
},
819-
{ recordTracksEvent, clearPurchases, refreshSitePlans, successNotice }
885+
{ recordTracksEvent, clearPurchases, refreshSitePlans, successNotice, errorNotice }
820886
)( localize( withLocalizedMoment( CancelPurchase ) ) );

0 commit comments

Comments
 (0)