File size: 6,336 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { isJetpackBackupSlug } from '@automattic/calypso-products';
import Debug from 'debug';
import QueryRewindState from 'calypso/components/data/query-rewind-state';
import HasVaultPressSwitch from 'calypso/components/jetpack/has-vaultpress-switch';
import IsCurrentUserAdminSwitch from 'calypso/components/jetpack/is-current-user-admin-switch';
import IsJetpackDisconnectedSwitch from 'calypso/components/jetpack/is-jetpack-disconnected-switch';
import NotAuthorizedPage from 'calypso/components/jetpack/not-authorized-page';
import { UpsellProductCardPlaceholder } from 'calypso/components/jetpack/upsell-product-card';
import UpsellSwitch from 'calypso/components/jetpack/upsell-switch';
import SidebarNavigation from 'calypso/components/sidebar-navigation';
import isJetpackCloud from 'calypso/lib/jetpack/is-jetpack-cloud';
import { setFilter } from 'calypso/state/activity-log/actions';
import getRewindState from 'calypso/state/selectors/get-rewind-state';
import getSelectedSiteId from 'calypso/state/ui/selectors/get-selected-site-id';
import BackupContentsPage from './backup-contents-page';
import BackupUpsell from './backup-upsell';
import BackupCloneFlow from './clone-flow';
import BackupsPage from './main';
import MultisiteNoBackupPlanSwitch from './multisite-no-backup-plan-switch';
import BackupRewindFlow, { RewindFlowPurpose } from './rewind-flow';
import WPCOMBackupUpsell from './wpcom-backup-upsell';
import WpcomBackupUpsellPlaceholder from './wpcom-backup-upsell-placeholder';

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

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

	const UpsellComponent = isJetpackCloud() ? BackupUpsell : WPCOMBackupUpsell;
	const UpsellPlaceholder = isJetpackCloud()
		? UpsellProductCardPlaceholder
		: WpcomBackupUpsellPlaceholder;
	context.primary = (
		<>
			<UpsellSwitch
				UpsellComponent={ UpsellComponent }
				QueryComponent={ QueryRewindState }
				getStateForSite={ getRewindState }
				isRequestingForSite={ ( state, siteId ) =>
					'uninitialized' === getRewindState( state, siteId )?.state
				}
				display={ context.primary }
				productSlugTest={ isJetpackBackupSlug }
			>
				{ isJetpackCloud() && <SidebarNavigation /> }
				<UpsellPlaceholder />
			</UpsellSwitch>
		</>
	);
	next();
}

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

	const JetpackConnectionFailed = isJetpackCloud() ? (
		<BackupUpsell reason="no_connected_jetpack" />
	) : (
		<WPCOMBackupUpsell reason="no_connected_jetpack" />
	);
	context.primary = (
		<IsJetpackDisconnectedSwitch
			trueComponent={ JetpackConnectionFailed }
			falseComponent={ context.primary }
		/>
	);
	next();
}

export function showNotAuthorizedForNonAdmins( context, next ) {
	context.primary = (
		<IsCurrentUserAdminSwitch
			trueComponent={ context.primary }
			falseComponent={ <NotAuthorizedPage /> }
		/>
	);

	next();
}

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

	const message = isJetpackCloud() ? (
		<BackupUpsell reason="vp_active_on_site" />
	) : (
		<WPCOMBackupUpsell reason="vp_active_on_site" />
	);

	context.primary = (
		<HasVaultPressSwitch trueComponent={ message } falseComponent={ context.primary } />
	);

	next();
}

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

	// Only show "Multisite not supported" card if the multisite does not already own a Backup subscription.
	// https://href.li/?https://wp.me/pbuNQi-1jg
	const message = isJetpackCloud() ? (
		<BackupUpsell reason="multisite_not_supported" />
	) : (
		<WPCOMBackupUpsell reason="multisite_not_supported" />
	);

	context.primary = (
		<MultisiteNoBackupPlanSwitch trueComponent={ message } falseComponent={ context.primary } />
	);

	next();
}

/* handles /backup/:site, see `backupMainPath` */
export function backups( context, next ) {
	debug( 'controller: backups', context.params );

	// When a user visits `/backup`, we don't want to carry over any filter
	// selection that could've happened in the Activity Log, otherwise,
	// the app will render the `SearchResults` component instead of the
	// `BackupStatus`.
	const state = context.store.getState();
	const siteId = getSelectedSiteId( state );
	context.store.dispatch( setFilter( siteId, {}, true ) );

	const { date } = context.query;

	context.primary = <BackupsPage queryDate={ date } />;
	next();
}

/* handles /backup/:site/download/:rewindId, see `backupDownloadPath` */
export function backupDownload( context, next ) {
	debug( 'controller: backupDownload', context.params );

	context.primary = (
		<BackupRewindFlow rewindId={ context.params.rewindId } purpose={ RewindFlowPurpose.DOWNLOAD } />
	);
	next();
}

/* handles /backup/:site/restore/:rewindId, see `backupRestorePath` */
export function backupRestore( context, next ) {
	debug( 'controller: backupRestore', context.params );

	context.primary = (
		<BackupRewindFlow rewindId={ context.params.rewindId } purpose={ RewindFlowPurpose.RESTORE } />
	);
	next();
}

/* handles /backup/:site/granular-restore/:rewindId, see `backupGranularRestorePath` */
export function backupGranularRestore( context, next ) {
	debug( 'controller: backupGranularRestore', context.params );

	context.primary = (
		<>
			<BackupRewindFlow
				rewindId={ context.params.rewindId }
				purpose={ RewindFlowPurpose.GRANULAR_RESTORE }
			/>
		</>
	);
	next();
}

/* handles /backup/:site/clone, see `backupClonePath` */
export function backupClone( context, next ) {
	debug( 'controller: backupClone', context.params );
	const state = context.store.getState();
	const siteId = getSelectedSiteId( state );

	context.primary = <BackupCloneFlow siteId={ siteId } />;
	next();
}

/* handles /backup/:site/contents/:backupDate, see `backupContentsPath` */
export function backupContents( context, next ) {
	debug( 'controller: backupContents', context.params );
	const state = context.store.getState();
	const siteId = getSelectedSiteId( state );

	context.primary = <BackupContentsPage siteId={ siteId } rewindId={ context.params.rewindId } />;
	next();
}