File size: 4,240 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { isEnabled } from '@automattic/calypso-config';
import { PLAN_100_YEARS, isValidFeatureKey } from '@automattic/calypso-products';
import page from '@automattic/calypso-router';
import { isPlansPageUntangled } from 'calypso/lib/plans/untangling-plans-experiment';
import { productSelect } from 'calypso/my-sites/plans/jetpack-plans/controller';
import setJetpackPlansHeader from 'calypso/my-sites/plans/jetpack-plans/plans-header';
import { PLAN } from 'calypso/sites/components/site-preview-pane/constants';
import { siteDashboard } from 'calypso/sites/controller';
import isSiteWpcom from 'calypso/state/selectors/is-site-wpcom';
import { getSelectedSite, getSelectedSiteId } from 'calypso/state/ui/selectors';
import Plans from './main';

function showJetpackPlans( context ) {
	const state = context.store.getState();
	const siteId = getSelectedSiteId( state );
	const isWpcom = isSiteWpcom( state, siteId );
	return ! isWpcom;
}

function is100YearPlanUser( context ) {
	const state = context.store.getState();
	const selectedSite = getSelectedSite( state );
	return selectedSite?.plan?.product_slug === PLAN_100_YEARS;
}

export function plans( context, next ) {
	// Redirecting users for the 100-Year plan to the my-plan page.
	if ( is100YearPlanUser( context ) && ! isEnabled( 'untangling/plans' ) ) {
		return page.redirect( `/plans/my-plan/${ context.params.site }` );
	}
	if ( showJetpackPlans( context ) ) {
		if ( context.params.intervalType ) {
			return page.redirect( `/plans/${ context.params.site }` );
		}
		setJetpackPlansHeader( context );
		return productSelect( '/plans' )( context, next );
	}

	// Emails rely on the `discount` query param to auto-apply coupons
	// from the Calypso admin plans page. The `/start` onboarding flow
	// plans page, however, relies on the `coupon` query param for the
	// same purpose. We handle both coupon and discount here for the time
	// being to avoid confusion and to continue support for legacy
	// coupons. We'll consolidate to just `coupon` in the future.
	const coupon = context.query.coupon || context.query.discount;

	context.primary = (
		<Plans
			context={ context }
			intervalType={ context.params.intervalType }
			customerType={ context.query.customerType }
			selectedFeature={ context.query.feature }
			selectedPlan={ context.query.plan }
			coupon={ coupon }
			discountEndDate={ context.query.ts }
			redirectTo={ context.query.redirect_to }
			redirectToAddDomainFlow={
				context.query.addDomainFlow !== undefined
					? context.query.addDomainFlow === 'true'
					: undefined
			}
			domainAndPlanPackage={ context.query.domainAndPlanPackage === 'true' }
			jetpackAppPlans={ context.query.jetpackAppPlans === 'true' }
		/>
	);

	if ( isPlansPageUntangled( context.store.getState() ) ) {
		siteDashboard( PLAN )( context, next );
	} else {
		next();
	}
}

export function features( context ) {
	const { feature, domain } = context.params;
	let comparePath = domain ? `/plans/${ domain }` : '/plans/';

	if ( isValidFeatureKey( feature ) ) {
		comparePath += '?feature=' + feature;
	}

	// otherwise redirect to the compare page if not found
	page.redirect( comparePath );
}

export function redirectToCheckout( context ) {
	// this route is deprecated, use `/checkout/:site/:plan` to link to plan checkout
	page.redirect( `/checkout/${ context.params.domain }/${ context.params.plan }` );
}

export function redirectToPlans( context ) {
	const siteDomain = context.params.domain;

	if ( siteDomain ) {
		return page.redirect( `/plans/${ siteDomain }` );
	}

	return page.redirect( '/plans' );
}

export function redirectToPlansIfNotJetpack( context, next ) {
	if ( ! showJetpackPlans( context ) ) {
		page.redirect( `/plans/${ context.params.site }` );
	}
	next();
}

export const redirectIfInvalidInterval = ( context, next ) => {
	const { intervalType } = context.params;
	const state = context.store.getState();
	const selectedSite = getSelectedSite( state );

	// Passlist the intervals here to avoid "foo" values passing through
	if ( intervalType && ! [ 'monthly', 'yearly', '2yearly', '3yearly' ].includes( intervalType ) ) {
		page.redirect( selectedSite ? `/plans/yearly/${ selectedSite.slug }` : '/plans' );
		return null;
	}

	next();
};