Skip to content

Commit

Permalink
WPCOM: Migrate wpcom.undocumented() site plans to wpcom.req (#57766)
Browse files Browse the repository at this point in the history
* WPCOM: Migrate wpcom.undocumented() site plans to wpcom.req

* Remove unused plan cancellation state and undocumented method
  • Loading branch information
tyxla authored and nelsonec87 committed Dec 9, 2021
1 parent bec8f3a commit cf86147
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 200 deletions.
35 changes: 0 additions & 35 deletions client/lib/wpcom-undocumented/lib/undocumented.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,30 +349,6 @@ Undocumented.prototype.validateDomainContactInformation = function (
);
};

/**
* Get a site specific details for WordPress.com plans
*
* @param {Function} siteDomain The site slug
* @param {Function} fn The callback function
*/
Undocumented.prototype.getSitePlans = function ( siteDomain, fn ) {
debug( '/sites/:site_domain:/plans query' );

// the site domain could be for a jetpack site installed in
// a subdirectory. encode any forward slash present before making
// the request
siteDomain = encodeURIComponent( siteDomain );

return this._sendRequest(
{
path: '/sites/' + siteDomain + '/plans',
method: 'get',
apiVersion: '1.3',
},
fn
);
};

/**
* Get a site specific details for WordPress.com featurs
*
Expand Down Expand Up @@ -976,17 +952,6 @@ Undocumented.prototype.uploadExportFile = function ( siteId, params ) {
} );
};

Undocumented.prototype.cancelPlanTrial = function ( planId, fn ) {
debug( '/upgrades/{planId}/cancel-plan-trial' );

return this.wpcom.req.post(
{
path: `/upgrades/${ planId }/cancel-plan-trial`,
},
fn
);
};

/**
* Get the available export configuration settings for a site
*
Expand Down
3 changes: 0 additions & 3 deletions client/state/action-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,9 +937,6 @@ export const SITE_PLANS_FETCH = 'SITE_PLANS_FETCH';
export const SITE_PLANS_FETCH_COMPLETED = 'SITE_PLANS_FETCH_COMPLETED';
export const SITE_PLANS_FETCH_FAILED = 'SITE_PLANS_FETCH_FAILED';
export const SITE_PLANS_REMOVE = 'SITE_PLANS_REMOVE';
export const SITE_PLANS_TRIAL_CANCEL = 'SITE_PLANS_TRIAL_CANCEL';
export const SITE_PLANS_TRIAL_CANCEL_COMPLETED = 'SITE_PLANS_TRIAL_CANCEL_COMPLETED';
export const SITE_PLANS_TRIAL_CANCEL_FAILED = 'SITE_PLANS_TRIAL_CANCEL_FAILED';
export const SITE_PLUGIN_UPDATED = 'SITE_PLUGIN_UPDATED';
export const SITE_PRODUCTS_FETCH = 'SITE_PRODUCTS_FETCH';
export const SITE_PRODUCTS_FETCH_COMPLETED = 'SITE_PRODUCTS_FETCH_COMPLETED';
Expand Down
90 changes: 19 additions & 71 deletions client/state/sites/plans/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import {
SITE_PLANS_FETCH_COMPLETED,
SITE_PLANS_FETCH_FAILED,
SITE_PLANS_REMOVE,
SITE_PLANS_TRIAL_CANCEL,
SITE_PLANS_TRIAL_CANCEL_COMPLETED,
SITE_PLANS_TRIAL_CANCEL_FAILED,
SITE_PLAN_OWNERSHIP_TRANSFER,
} from 'calypso/state/action-types';
import { createSitePlanObject } from './assembler';
Expand All @@ -19,52 +16,6 @@ import 'calypso/state/currency-code/init';

const debug = debugFactory( 'calypso:site-plans:actions' );

/**
* Cancels the specified plan trial for the given site.
*
* @param {number} siteId identifier of the site
* @param {number} planId identifier of the plan
* @returns {Function} a promise that will resolve once updating is completed
*/
export function cancelSitePlanTrial( siteId, planId ) {
return ( dispatch ) => {
dispatch( {
type: SITE_PLANS_TRIAL_CANCEL,
siteId,
} );

return new Promise( ( resolve, reject ) => {
wpcom.undocumented().cancelPlanTrial( planId, ( error, data ) => {
if ( data && data.success ) {
dispatch( {
type: SITE_PLANS_TRIAL_CANCEL_COMPLETED,
siteId,
plans: map( data.plans, createSitePlanObject ),
} );

resolve();
} else {
debug( 'Canceling site plan trial failed: ', error );

const errorMessage =
error.message ||
i18n.translate(
'There was a problem canceling the plan trial. Please try again later or contact support.'
);

dispatch( {
type: SITE_PLANS_TRIAL_CANCEL_FAILED,
siteId,
error: errorMessage,
} );

reject( errorMessage );
}
} );
} );
};
}

/**
* Returns an action object to be used in signalling that plans for the given site has been cleared.
*
Expand All @@ -91,29 +42,26 @@ export function fetchSitePlans( siteId ) {
siteId,
} );

return new Promise( ( resolve ) => {
wpcom.undocumented().getSitePlans( siteId, ( error, data ) => {
if ( error ) {
debug( 'Fetching site plans failed: ', error );

const errorMessage =
error.message ||
i18n.translate(
'There was a problem fetching site plans. Please try again later or contact support.'
);

dispatch( {
type: SITE_PLANS_FETCH_FAILED,
siteId,
error: errorMessage,
} );
} else {
dispatch( fetchSitePlansCompleted( siteId, data ) );
}

resolve();
return wpcom.req
.get( `/sites/${ siteId }/plans`, { apiVersion: '1.3' } )
.then( ( data ) => {
dispatch( fetchSitePlansCompleted( siteId, data ) );
} )
.catch( ( error ) => {
debug( 'Fetching site plans failed: ', error );

const errorMessage =
error.message ||
i18n.translate(
'There was a problem fetching site plans. Please try again later or contact support.'
);

dispatch( {
type: SITE_PLANS_FETCH_FAILED,
siteId,
error: errorMessage,
} );
} );
} );
};
}

Expand Down
22 changes: 0 additions & 22 deletions client/state/sites/plans/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import {
SITE_PLANS_FETCH_COMPLETED,
SITE_PLANS_FETCH_FAILED,
SITE_PLANS_REMOVE,
SITE_PLANS_TRIAL_CANCEL,
SITE_PLANS_TRIAL_CANCEL_COMPLETED,
SITE_PLANS_TRIAL_CANCEL_FAILED,
} from 'calypso/state/action-types';

export const initialSiteState = {
Expand Down Expand Up @@ -54,25 +51,6 @@ export function plans( state = {}, action ) {

case SITE_PLANS_REMOVE:
return omit( state, action.siteId );

case SITE_PLANS_TRIAL_CANCEL:
return updateSiteState( state, action.siteId, {
isRequesting: true,
} );

case SITE_PLANS_TRIAL_CANCEL_COMPLETED:
return updateSiteState( state, action.siteId, {
error: null,
hasLoadedFromServer: true,
isRequesting: false,
data: action.plans,
} );

case SITE_PLANS_TRIAL_CANCEL_FAILED:
return updateSiteState( state, action.siteId, {
error: action.error,
isRequesting: false,
} );
}

return state;
Expand Down
69 changes: 0 additions & 69 deletions client/state/sites/plans/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import {
SITE_PLANS_FETCH,
SITE_PLANS_FETCH_COMPLETED,
SITE_PLANS_FETCH_FAILED,
SITE_PLANS_TRIAL_CANCEL,
SITE_PLANS_TRIAL_CANCEL_FAILED,
SITE_PLANS_TRIAL_CANCEL_COMPLETED,
SITE_PLANS_REMOVE,
} from 'calypso/state/action-types';
import { plans } from '../reducer';
Expand Down Expand Up @@ -163,72 +160,6 @@ describe( 'reducer', () => {
} );
} );

test( 'should return the original state with updating enabled when trial cancelation is triggered', () => {
const original = Object.freeze( {
11111111: {
data: [],
error: null,
hasLoadedFromServer: false,
isRequesting: false,
},
} );
const state = plans( original, {
type: SITE_PLANS_TRIAL_CANCEL,
siteId: 11111111,
} );

expect( state ).to.eql( {
11111111: {
data: [],
error: null,
hasLoadedFromServer: false,
isRequesting: true,
},
} );
} );

test( 'should return the original state with an error and requesting disabled when trial cancelation failed', () => {
const original = Object.freeze( {
11111111: {
data: [],
error: null,
hasLoadedFromServer: true,
isRequesting: true,
},
} );
const state = plans( original, {
type: SITE_PLANS_TRIAL_CANCEL_FAILED,
siteId: 11111111,
error: 'Unable to cancel plan trial',
} );

expect( state ).to.eql( {
11111111: {
data: [],
error: 'Unable to cancel plan trial',
hasLoadedFromServer: true,
isRequesting: false,
},
} );
} );

test( 'should return a list of plans with loaded from server enabled and requesting disabled when trial cancelation completed', () => {
const state = plans( undefined, {
type: SITE_PLANS_TRIAL_CANCEL_COMPLETED,
siteId: 11111111,
plans: [],
} );

expect( state ).to.eql( {
11111111: {
data: [],
error: null,
hasLoadedFromServer: true,
isRequesting: false,
},
} );
} );

test( 'should return an empty state when original state is undefined and removal is triggered', () => {
const state = plans( undefined, {
type: SITE_PLANS_REMOVE,
Expand Down

0 comments on commit cf86147

Please sign in to comment.