Skip to content

Commit

Permalink
Marketplace Themes: Fixes the bug activating themes after resetting t…
Browse files Browse the repository at this point in the history
…he site (#73113)

* Create reducers, actions and selectors for handling the products reinstall

* Add error to the state

* Adding the request to reinstall the products in the activateTheme action

* Fix activateTheme test
  • Loading branch information
valterlorran authored Feb 14, 2023
1 parent 08451d5 commit 7bca130
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 1 deletion.
5 changes: 5 additions & 0 deletions client/state/action-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,11 @@ export const MARKETING_JETPACK_SALE_COUPON_FETCH_FAILURE =
export const MARKETING_JETPACK_SALE_COUPON_FETCH = 'MARKETING_JETPACK_SALE_COUPON_FETCH';
export const MARKETING_JETPACK_SALE_COUPON_RECEIVE = 'MARKETING_JETPACK_SALE_COUPON_RECEIVE';
export const MARKETPLACE_BILLING_INTERVAL_SELECT = 'MARKETPLACE_BILLING_INTERVAL_SELECT';
export const MARKETPLACE_PRODUCTS_REINSTALL_COMPLETED = 'MARKETPLACE_PRODUCTS_REINSTALL_COMPLETED';
export const MARKETPLACE_PRODUCTS_REINSTALL_FAILED = 'MARKETPLACE_PRODUCTS_REINSTALL_FAILED';
export const MARKETPLACE_PRODUCTS_REINSTALL_NOT_STARTED =
'MARKETPLACE_PRODUCTS_REINSTALL_NOT_STARTED';
export const MARKETPLACE_PRODUCTS_REINSTALL_STARTED = 'MARKETPLACE_PRODUCTS_REINSTALL_STARTED';
export const MARKETPLACE_PLUGIN_INSTALLATION_STATE_CHANGE =
'MARKETPLACE_PLUGIN_INSTALLATION_STATE_CHANGE';
export const MARKETPLACE_PRIMARY_DOMAIN_SELECT = 'MARKETPLACE_PRIMARY_DOMAIN_SELECT';
Expand Down
63 changes: 63 additions & 0 deletions client/state/marketplace/products-reinstall/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import wpcom from 'calypso/lib/wp';
import {
MARKETPLACE_PRODUCTS_REINSTALL_COMPLETED,
MARKETPLACE_PRODUCTS_REINSTALL_FAILED,
MARKETPLACE_PRODUCTS_REINSTALL_STARTED,
MARKETPLACE_PRODUCTS_REINSTALL_NOT_STARTED,
} from 'calypso/state/action-types';
import { activateTheme } from 'calypso/state/themes/actions';
import type { AnyAction } from 'redux';
import 'calypso/state/marketplace/init';

/**
* Reinstall products on a site.
*
* @param siteId the site id
* @returns Promise
*/
export function productsReinstall( siteId: number, themeId: string ) {
return async ( dispatch: CallableFunction ) => {
dispatch( productsReinstallStarted( siteId ) );

try {
await wpcom.req.get( {
path: `/sites/${ siteId }/marketplace/products/reinstall`,
apiNamespace: 'wpcom/v2',
} );

dispatch( productsReinstallCompleted( siteId ) );
} catch ( error ) {
dispatch( productsReinstallFailed( siteId, ( error as Error ).message ) );
}
dispatch( activateTheme( themeId, siteId ) );
};
}

export function productsReinstallStarted( siteId: number ): AnyAction {
return {
type: MARKETPLACE_PRODUCTS_REINSTALL_STARTED,
siteId,
};
}

export function productsReinstallFailed( siteId: number, error: string ): AnyAction {
return {
type: MARKETPLACE_PRODUCTS_REINSTALL_FAILED,
siteId,
error,
};
}

export function productsReinstallCompleted( siteId: number ): AnyAction {
return {
type: MARKETPLACE_PRODUCTS_REINSTALL_COMPLETED,
siteId,
};
}

export function productsReinstallNotStarted( siteId: number ): AnyAction {
return {
type: MARKETPLACE_PRODUCTS_REINSTALL_NOT_STARTED,
siteId,
};
}
54 changes: 54 additions & 0 deletions client/state/marketplace/products-reinstall/reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
MARKETPLACE_PRODUCTS_REINSTALL_COMPLETED,
MARKETPLACE_PRODUCTS_REINSTALL_FAILED,
MARKETPLACE_PRODUCTS_REINSTALL_STARTED,
MARKETPLACE_PRODUCTS_REINSTALL_NOT_STARTED,
} from 'calypso/state/action-types';
import { withSchemaValidation } from 'calypso/state/utils';
import { IReinstallProductsState, IReinstallProductsStatus } from '../types';
import { productsReinstallSchema } from './schema';
import type { AnyAction } from 'redux';

export const defaultState: IReinstallProductsState = {};

const productsReinstall = withSchemaValidation(
productsReinstallSchema,
( state = defaultState, action: AnyAction ): IReinstallProductsState => {
const { type, siteId, error } = action;
switch ( type ) {
case MARKETPLACE_PRODUCTS_REINSTALL_STARTED:
return {
...state,
[ siteId ]: {
status: IReinstallProductsStatus.IN_PROGRESS,
},
};
case MARKETPLACE_PRODUCTS_REINSTALL_COMPLETED:
return {
...state,
[ siteId ]: {
status: IReinstallProductsStatus.COMPLETED,
},
};
case MARKETPLACE_PRODUCTS_REINSTALL_FAILED:
return {
...state,
[ siteId ]: {
status: IReinstallProductsStatus.FAILED,
error,
},
};
case MARKETPLACE_PRODUCTS_REINSTALL_NOT_STARTED:
return {
...state,
[ siteId ]: {
status: IReinstallProductsStatus.NOT_STARTED,
},
};
}

return state;
}
);

export default productsReinstall;
14 changes: 14 additions & 0 deletions client/state/marketplace/products-reinstall/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const productsReinstallSchema = {
type: 'object',
additionalProperties: false,
properties: {
'^\\d+$': {
type: 'object',
properties: {
started: { type: 'boolean' },
failed: { type: 'boolean' },
completed: { type: 'boolean' },
},
},
},
};
29 changes: 29 additions & 0 deletions client/state/marketplace/products-reinstall/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'calypso/state/marketplace/init';
import { IAppState } from 'calypso/state/types';
import { IReinstallProductsStatus } from '../types';

export function isReinstallingProducts( state: IAppState, siteId: number ) {
return (
state.marketplace.reinstallProducts?.[ siteId ]?.status === IReinstallProductsStatus.IN_PROGRESS
);
}

export function failedReinstallingProducts( state: IAppState, siteId: number ) {
return (
state.marketplace.reinstallProducts?.[ siteId ]?.status === IReinstallProductsStatus.FAILED
);
}

export function completedReinstallingProducts( state: IAppState, siteId: number ) {
return (
state.marketplace.reinstallProducts?.[ siteId ]?.status === IReinstallProductsStatus.COMPLETED
);
}

export function requestedReinstallProducts( state: IAppState, siteId: number ) {
return (
state.marketplace.reinstallProducts?.[ siteId ]?.status ===
IReinstallProductsStatus.COMPLETED ||
state.marketplace.reinstallProducts?.[ siteId ]?.status === IReinstallProductsStatus.FAILED
);
}
2 changes: 2 additions & 0 deletions client/state/marketplace/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { withStorageKey } from '@automattic/state-utils';
import { combineReducers } from 'calypso/state/utils';
import billingInterval from './billing-interval/reducer';
import reinstallProducts from './products-reinstall/reducer';
import purchaseFlow from './purchase-flow/reducer';

const combinedReducers = combineReducers( {
purchaseFlow,
billingInterval,
reinstallProducts,
} );

export default withStorageKey( 'marketplace', combinedReducers );
15 changes: 15 additions & 0 deletions client/state/marketplace/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,22 @@ export interface IBillingIntervalState {
interval: IntervalLength.MONTHLY | IntervalLength.ANNUALLY;
}

export enum IReinstallProductsStatus {
COMPLETED = 'completed',
FAILED = 'failed',
IN_PROGRESS = 'in-progress',
NOT_STARTED = 'not-started',
}

export interface IReinstallProductsState {
[ siteId: number ]: {
status: IReinstallProductsStatus;
error?: string;
};
}

export interface IMarketplaceState {
purchaseFlow: IPurchaseFlowState;
billingInterval: IBillingIntervalState;
reinstallProducts: IReinstallProductsState;
}
16 changes: 15 additions & 1 deletion client/state/themes/actions/activate-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import {
getGlobalStylesVariations,
updateGlobalStyles,
} from 'calypso/state/global-styles/actions';
import {
productsReinstall,
productsReinstallNotStarted,
} from 'calypso/state/marketplace/products-reinstall/actions';
import { requestedReinstallProducts } from 'calypso/state/marketplace/products-reinstall/selectors';
import { errorNotice } from 'calypso/state/notices/actions';
import { THEME_ACTIVATE, THEME_ACTIVATE_FAILURE } from 'calypso/state/themes/action-types';
import { themeActivated } from 'calypso/state/themes/actions/theme-activated';
import { getThemePreviewThemeOptions } from 'calypso/state/themes/selectors';
import {
getThemePreviewThemeOptions,
isMarketplaceThemeSubscribed,
} from 'calypso/state/themes/selectors';

import 'calypso/state/themes/init';

Expand Down Expand Up @@ -78,6 +86,12 @@ export function activateTheme(
);
} )
.catch( ( error ) => {
if ( isMarketplaceThemeSubscribed( getState(), themeId, siteId ) ) {
if ( ! requestedReinstallProducts( getState(), siteId ) ) {
return dispatch( productsReinstall( siteId, themeId ) );
}
dispatch( productsReinstallNotStarted( siteId ) );
}
dispatch( {
type: THEME_ACTIVATE_FAILURE,
themeId,
Expand Down
6 changes: 6 additions & 0 deletions client/state/themes/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,12 @@ describe( 'actions', () => {
sites: {
items: {},
},
productsList: {
items: {},
},
purchases: {
data: {},
},
} );

const trackingData = {
Expand Down

0 comments on commit 7bca130

Please sign in to comment.