From cf86147eaef4ca40d163fe6dcfa3c3798c574fce Mon Sep 17 00:00:00 2001 From: Marin Atanasov <8436925+tyxla@users.noreply.github.com> Date: Tue, 9 Nov 2021 13:45:05 +0200 Subject: [PATCH] WPCOM: Migrate `wpcom.undocumented()` site plans to `wpcom.req` (#57766) * WPCOM: Migrate wpcom.undocumented() site plans to wpcom.req * Remove unused plan cancellation state and undocumented method --- .../wpcom-undocumented/lib/undocumented.js | 35 -------- client/state/action-types.ts | 3 - client/state/sites/plans/actions.js | 90 ++++--------------- client/state/sites/plans/reducer.js | 22 ----- client/state/sites/plans/test/reducer.js | 69 -------------- 5 files changed, 19 insertions(+), 200 deletions(-) diff --git a/client/lib/wpcom-undocumented/lib/undocumented.js b/client/lib/wpcom-undocumented/lib/undocumented.js index 5db402acd472..4f2ce5d83ef1 100644 --- a/client/lib/wpcom-undocumented/lib/undocumented.js +++ b/client/lib/wpcom-undocumented/lib/undocumented.js @@ -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 * @@ -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 * diff --git a/client/state/action-types.ts b/client/state/action-types.ts index b2e6a1a08726..2f910ea7400e 100644 --- a/client/state/action-types.ts +++ b/client/state/action-types.ts @@ -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'; diff --git a/client/state/sites/plans/actions.js b/client/state/sites/plans/actions.js index 37bf91281ddb..c3afdf0b2901 100644 --- a/client/state/sites/plans/actions.js +++ b/client/state/sites/plans/actions.js @@ -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'; @@ -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. * @@ -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, + } ); } ); - } ); }; } diff --git a/client/state/sites/plans/reducer.js b/client/state/sites/plans/reducer.js index ec15d3ce501b..ca05e94d12e7 100644 --- a/client/state/sites/plans/reducer.js +++ b/client/state/sites/plans/reducer.js @@ -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 = { @@ -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; diff --git a/client/state/sites/plans/test/reducer.js b/client/state/sites/plans/test/reducer.js index 3ac40292fe8e..c8885474499d 100644 --- a/client/state/sites/plans/test/reducer.js +++ b/client/state/sites/plans/test/reducer.js @@ -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'; @@ -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,