Skip to content

Commit

Permalink
Don't display intro discount if not eligible (#95484)
Browse files Browse the repository at this point in the history
* Don't display intro discount if not eligible

* Fix tests

* Remove testing edit in config file

* Add global window object in tests

* Fix test
  • Loading branch information
CodeyGuyDylan authored Oct 22, 2024
1 parent c3ca7c8 commit a96495f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
8 changes: 7 additions & 1 deletion client/my-sites/plans/jetpack-plans/use-item-price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { getProductCost } from 'calypso/state/products-list/selectors/get-product-cost';
import { getProductPriceTierList } from 'calypso/state/products-list/selectors/get-product-price-tiers';
import { isProductsListFetching } from 'calypso/state/products-list/selectors/is-products-list-fetching';
import getIntroOfferEligibility from 'calypso/state/selectors/get-intro-offer-eligibility';
import getIntroOfferPrice from 'calypso/state/selectors/get-intro-offer-price';
import isRequestingIntroOffers from 'calypso/state/selectors/get-is-requesting-into-offers';
import {
Expand Down Expand Up @@ -113,7 +114,12 @@ const useIntroductoryOfferPrices = (
}

const introOfferPrice = getIntroOfferPrice( state, product.product_id, siteId ?? 'none' );
return isNumber( introOfferPrice ) ? introOfferPrice : null;
const isEligibleForIntroPrice = getIntroOfferEligibility(
state,
product.product_id,
siteId ?? 'none'
);
return isNumber( introOfferPrice ) && isEligibleForIntroPrice ? introOfferPrice : null;
} );

return {
Expand Down
22 changes: 17 additions & 5 deletions client/state/products-list/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,35 @@ export function receiveProductsList( productsList, type = null ) {
* or undefined, for all products
* @returns {Function} an Action thunk
*/
export function requestProductsList( query = {} ) {
export function requestProductsList( query = {}, siteQueryFailed = false ) {
const queryParams = new URLSearchParams( window.location.search );
const site = siteQueryFailed ? null : queryParams.get( 'site' );

const requestQuery = { ...query };
if ( query.product_slugs && query.product_slugs.length > 0 ) {
const product_slugs = query.product_slugs?.join( ',' );
requestQuery.product_slugs = product_slugs;
}

const path = site ? `/sites/${ site }/products` : '/products';

return ( dispatch ) => {
dispatch( { type: PRODUCTS_LIST_REQUEST } );

return wpcom.req
.get( '/products', requestQuery )
.get( path, requestQuery )
.then( ( productsList ) => dispatch( receiveProductsList( productsList, query.type ) ) )
.catch( ( error ) =>
.catch( ( error ) => {
// If the query had the site in context and it failed, it means there was either a server error or the site doesn't exist.
// In this case, we should retry to fetch the products list without the site in context.
if ( site ) {
return requestProductsList( query, true )( dispatch );
}

dispatch( {
type: PRODUCTS_LIST_REQUEST_FAILURE,
error,
} )
);
} );
} );
};
}
5 changes: 5 additions & 0 deletions client/state/products-list/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ describe( 'actions', () => {

beforeEach( () => {
spy = jest.fn();
global.window = {
location: {
search: '',
},
};
} );

const businessPlan = {
Expand Down
22 changes: 22 additions & 0 deletions client/state/selectors/get-intro-offer-eligibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { AppState } from 'calypso/types';

/**
* @param {Object} state Global state tree
* @param {number} productId The productId to check for an intro offer
* @param {number|'none'|undefined} siteId The ID of the site we're querying
* @returns {boolean} Whether the site is eligible for an intro offer
*/

export default function getIntroOfferEligibility(
state: AppState,
productId: number,
siteId: number | 'none' | undefined
): boolean {
const siteIdKey = siteId && typeof siteId === 'number' && siteId > 0 ? siteId : 'none';

const introOffer = state.sites?.introOffers?.items?.[ siteIdKey ]?.[ productId ];

const ineligibleReason = introOffer?.ineligibleReason;

return ! ineligibleReason || ineligibleReason?.length === 0;
}

0 comments on commit a96495f

Please sign in to comment.