File size: 2,929 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
import config from '@automattic/calypso-config';
import {
	PRODUCT_JETPACK_SEARCH,
	PRODUCT_JETPACK_SEARCH_MONTHLY,
	PRODUCT_WPCOM_SEARCH,
	PRODUCT_WPCOM_SEARCH_MONTHLY,
} from '@automattic/calypso-products';
import page from '@automattic/calypso-router';
import Debug from 'debug';
import { get, some } from 'lodash';
import { recordPageView } from 'calypso/lib/analytics/page-view';
import { login } from 'calypso/lib/paths';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import { hideMasterbar, showMasterbar } from 'calypso/state/ui/actions';
import { ALLOWED_MOBILE_APP_REDIRECT_URL_LIST } from '../../jetpack-connect/constants';
import {
	persistMobileRedirect,
	retrieveMobileRedirect,
	storePlan,
} from '../../jetpack-connect/persistence-utils';
import SearchPurchase from './search';

/**
 * Module variables
 */
const debug = new Debug( 'calypso:purchase-product:controller' );
const analyticsPageTitleByType = {
	jetpack_search: 'Jetpack Search',
};

const getPlanSlugFromFlowType = ( type, interval = 'yearly' ) => {
	const planSlugs = {
		yearly: {
			jetpack_search: PRODUCT_JETPACK_SEARCH,
			wpcom_search: PRODUCT_WPCOM_SEARCH,
		},
		monthly: {
			jetpack_search: PRODUCT_JETPACK_SEARCH_MONTHLY,
			wpcom_search: PRODUCT_WPCOM_SEARCH_MONTHLY,
		},
	};

	return get( planSlugs, [ interval, type ], '' );
};

export function redirectToLogin( context, next ) {
	const loggedIn = isUserLoggedIn( context.store.getState() );

	if ( ! loggedIn ) {
		page( login( { isJetpack: true, redirectTo: context.path } ) );
		return;
	}

	next();
}

export function persistMobileAppFlow( context, next ) {
	const { query } = context;
	if ( config.isEnabled( 'jetpack/connect/mobile-app-flow' ) ) {
		if (
			some( ALLOWED_MOBILE_APP_REDIRECT_URL_LIST, ( pattern ) =>
				pattern.test( query.mobile_redirect )
			)
		) {
			debug( `In mobile app flow with redirect url: ${ query.mobile_redirect }` );
			persistMobileRedirect( query.mobile_redirect );
		} else {
			persistMobileRedirect( '' );
		}
	}
	next();
}

export function setMasterbar( context, next ) {
	if ( config.isEnabled( 'jetpack/connect/mobile-app-flow' ) ) {
		const masterbarToggle = retrieveMobileRedirect() ? hideMasterbar() : showMasterbar();
		context.store.dispatch( masterbarToggle );
	}
	next();
}

// Purchase Jetpack Search
export function purchase( context, next ) {
	const { path, pathname, params, query } = context;
	const { type = false, interval } = params;
	const analyticsPageTitle = get( type, analyticsPageTitleByType, 'Jetpack Connect' );
	const planSlug = getPlanSlugFromFlowType( type, interval );

	planSlug && storePlan( planSlug );
	recordPageView( pathname, analyticsPageTitle );

	context.primary = (
		<SearchPurchase
			ctaFrom={ query.cta_from /* origin tracking params */ }
			ctaId={ query.cta_id /* origin tracking params */ }
			path={ path }
			type={ type }
			url={ query.url }
		/>
	);
	next();
}