7
7
isJetpackProduct ,
8
8
isAkismetProduct ,
9
9
getPlan ,
10
+ getMonthlyPlanByYearly ,
10
11
} from '@automattic/calypso-products' ;
11
12
import page from '@automattic/calypso-router' ;
12
13
import { Card , CompactCard } from '@automattic/components' ;
@@ -34,7 +35,12 @@ import {
34
35
isRefundable ,
35
36
isSubscription ,
36
37
} 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' ;
38
44
import { getPurchaseCancellationFlowType } from 'calypso/lib/purchases/utils' ;
39
45
import CancelPurchaseLoadingPlaceholder from 'calypso/me/purchases/cancel-purchase/loading-placeholder' ;
40
46
import { managePurchase , purchasesRoot } from 'calypso/me/purchases/paths' ;
@@ -43,14 +49,15 @@ import PurchaseSiteHeader from 'calypso/me/purchases/purchases-site/header';
43
49
import TrackPurchasePageView from 'calypso/me/purchases/track-purchase-page-view' ;
44
50
import { isDataLoading } from 'calypso/me/purchases/utils' ;
45
51
import { recordTracksEvent } from 'calypso/state/analytics/actions' ;
46
- import { successNotice } from 'calypso/state/notices/actions' ;
52
+ import { successNotice , errorNotice } from 'calypso/state/notices/actions' ;
47
53
import { getProductsList } from 'calypso/state/products-list/selectors' ;
48
54
import { clearPurchases } from 'calypso/state/purchases/actions' ;
49
55
import {
50
56
getByPurchaseId ,
51
57
getSitePurchases ,
52
58
hasLoadedUserPurchasesFromServer ,
53
59
getIncludedDomainPurchase ,
60
+ getDowngradePlanFromPurchase ,
54
61
} from 'calypso/state/purchases/selectors' ;
55
62
import getAtomicTransfer from 'calypso/state/selectors/get-atomic-transfer' ;
56
63
import { getDomainsBySiteId } from 'calypso/state/sites/domains/selectors' ;
@@ -316,6 +323,59 @@ class CancelPurchase extends Component {
316
323
}
317
324
} ;
318
325
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
+
319
379
onAtomicRevertConfirmationChange = ( isConfirmed ) => {
320
380
this . setState ( { atomicRevertConfirmed : isConfirmed } ) ;
321
381
} ;
@@ -498,6 +558,8 @@ class CancelPurchase extends Component {
498
558
isLoading = { this . state . isLoading }
499
559
onDialogClose = { this . onDialogClose }
500
560
onSetLoading = { this . onSetLoading }
561
+ downgradeClick = { this . downgradeClick }
562
+ freeMonthOfferClick = { this . freeMonthOfferClick }
501
563
/>
502
564
) ;
503
565
} ;
@@ -685,6 +747,8 @@ class CancelPurchase extends Component {
685
747
isLoading = { this . state . isLoading }
686
748
onDialogClose = { this . onDialogClose }
687
749
onSetLoading = { this . onSetLoading }
750
+ downgradeClick = { this . downgradeClick }
751
+ freeMonthOfferClick = { this . freeMonthOfferClick }
688
752
/>
689
753
{ this . renderKeepSubscriptionButton ( ) }
690
754
</ div >
@@ -742,6 +806,8 @@ class CancelPurchase extends Component {
742
806
cancelBundledDomain = { this . state . cancelBundledDomain }
743
807
includedDomainPurchase = { this . props . includedDomainPurchase }
744
808
cancellationInProgress = { this . state . isLoading }
809
+ downgradeClick = { this . downgradeClick }
810
+ freeMonthOfferClick = { this . freeMonthOfferClick }
745
811
/>
746
812
) }
747
813
< Card className = "cancel-purchase__wrapper-card" >
@@ -816,5 +882,5 @@ export default connect(
816
882
atomicTransfer : getAtomicTransfer ( state , purchase ?. siteId ) ,
817
883
} ;
818
884
} ,
819
- { recordTracksEvent, clearPurchases, refreshSitePlans, successNotice }
885
+ { recordTracksEvent, clearPurchases, refreshSitePlans, successNotice, errorNotice }
820
886
) ( localize ( withLocalizedMoment ( CancelPurchase ) ) ) ;
0 commit comments