File size: 1,231 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import { planMatches } from '@automattic/calypso-products';
import { activeDiscounts } from 'calypso/lib/discounts';
import { hasActivePromotion } from 'calypso/state/active-promotions/selectors';
import { getSitePlanSlug } from 'calypso/state/sites/selectors';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
export const isDiscountActive = ( discount, state ) => {
const now = new Date();
if ( ! discount.startsAt || ! discount.endsAt ) {
return false;
}
if ( discount.startsAt > now || discount.endsAt < now ) {
return false;
}
if ( ! hasActivePromotion( state, discount.name ) ) {
return false;
}
if ( discount.targetPlans ) {
const targetPlans = Array.isArray( discount.targetPlans ) ? discount.targetPlans : [];
const selectedSitePlanSlug = getSitePlanSlug( state, getSelectedSiteId( state ) );
return targetPlans.some( ( plan ) => planMatches( selectedSitePlanSlug, plan ) );
}
return true;
};
/**
* Returns info whether the site is eligible for spring discount or not.
* @param {Object} state Global state tree.
* @returns {Object | null} Promo description
*/
export default ( state ) => {
return activeDiscounts.find( ( p ) => isDiscountActive( p, state ) ) ?? null;
};
|