File size: 4,256 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
import { isJetpackSearchSlug } from '@automattic/calypso-products';
import Debug from 'debug';
import QueryJetpackModules from 'calypso/components/data/query-jetpack-modules';
import QuerySiteSettings from 'calypso/components/data/query-site-settings';
import QueryUserPurchases from 'calypso/components/data/query-user-purchases';
import IsJetpackDisconnectedSwitch from 'calypso/components/jetpack/is-jetpack-disconnected-switch';
import { UpsellProductCardPlaceholder } from 'calypso/components/jetpack/upsell-product-card';
import UpsellSwitch from 'calypso/components/jetpack/upsell-switch';
import isJetpackCloud from 'calypso/lib/jetpack/is-jetpack-cloud';
import {
	hasLoadedUserPurchasesFromServer,
	isFetchingUserPurchases,
} from 'calypso/state/purchases/selectors';
import getSiteSetting from 'calypso/state/selectors/get-site-setting';
import isFetchingJetpackModules from 'calypso/state/selectors/is-fetching-jetpack-modules';
import isJetpackModuleActive from 'calypso/state/selectors/is-jetpack-module-active';
import { isRequestingSiteSettings } from 'calypso/state/site-settings/selectors';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import getSelectedSiteId from 'calypso/state/ui/selectors/get-selected-site-id';
import JetpackSearchDisconnected from './disconnected';
import JetpackSearchUpsell from './jetpack-search-upsell';
import SearchMain from './main';
import WpcomSearchUpsellPlaceholder from './wpcom-search-upsell-placeholder';

const debug = new Debug( 'calypso:my-sites:search:controller' );

export function showUpsellIfNoSearch( context, next ) {
	debug( 'controller: showUpsellIfNoSearch', context.params );

	const state = context.store.getState();
	const siteId = getSelectedSiteId( state );
	const isJetpack = isJetpackSite( state, siteId );

	const QueryComponent = isJetpack ? QueryJetpackModules : QuerySiteSettings;
	const getSearchState = isJetpack ? getJetpackSearchState : getWPComSearchState;
	const isRequestingForSite = ( asyncState, asyncSiteId ) => {
		// On Jetpack sites, we need to check if the search module is active, on WPCom sites we
		// check 'jetpack_search_enabled' in the site settings.
		const isRequestingSearchStatus = isJetpack
			? isFetchingJetpackModules
			: isRequestingSiteSettings;
		// We also need to wait for user purchases to be loaded, or we risk flashing the upsell to
		// customers who already own the product.
		const isRequestingPurchases =
			! hasLoadedUserPurchasesFromServer( asyncState ) || isFetchingUserPurchases( asyncState );
		return isRequestingPurchases || isRequestingSearchStatus( asyncState, asyncSiteId );
	};
	const UpsellPlaceholder = isJetpackCloud()
		? UpsellProductCardPlaceholder
		: WpcomSearchUpsellPlaceholder;

	context.primary = (
		<>
			<QueryUserPurchases />
			<UpsellSwitch
				UpsellComponent={ JetpackSearchUpsell }
				QueryComponent={ QueryComponent }
				getStateForSite={ getSearchState }
				isRequestingForSite={ isRequestingForSite }
				display={ context.primary }
				productSlugTest={ isJetpackSearchSlug }
			>
				<UpsellPlaceholder />
			</UpsellSwitch>
		</>
	);

	next();
}

export function showJetpackIsDisconnected( context, next ) {
	debug( 'controller: showJetpackIsDisconnected', context.params );
	context.primary = (
		<IsJetpackDisconnectedSwitch
			trueComponent={ <JetpackSearchDisconnected /> }
			falseComponent={ context.primary }
		/>
	);
	next();
}

/* handles /jetpack-search/:site, see `jetpackSearchMainPath` */
export function jetpackSearchMain( context, next ) {
	debug( 'controller: jetpackSearchMain', context.params );

	context.primary = <SearchMain />;
	next();
}

// Selectors used for the Search UpsellSwitch (above)
function getJetpackSearchState( state, siteId ) {
	const isSearchModuleActive = isJetpackModuleActive( state, siteId, 'search' );
	return {
		state: isSearchModuleActive ? 'active' : 'unavailable',
		...( ! isSearchModuleActive && { reason: 'search_not_active' } ),
	};
}

function getWPComSearchState( state, siteId ) {
	const isSearchSettingEnabled = getSiteSetting( state, siteId, 'jetpack_search_enabled' );
	return {
		state: isSearchSettingEnabled ? 'active' : 'unavailable',
		...( ! isSearchSettingEnabled && { reason: 'search_not_active' } ),
	};
}