File size: 9,173 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import { isEnabled } from '@automattic/calypso-config';
import page, { type Callback, type Context } from '@automattic/calypso-router';
import {
	publicToInternalLicenseFilter,
	publicToInternalLicenseSortField,
	valueToEnum,
	ensurePartnerPortalReturnUrl,
} from 'calypso/jetpack-cloud/sections/partner-portal/lib';
import LicenseSelectPartnerKey from 'calypso/jetpack-cloud/sections/partner-portal/license-select-partner-key';
import AssignLicense from 'calypso/jetpack-cloud/sections/partner-portal/primary/assign-license';
import BillingDashboard from 'calypso/jetpack-cloud/sections/partner-portal/primary/billing-dashboard';
import CompanyDetailsDashboard from 'calypso/jetpack-cloud/sections/partner-portal/primary/company-details-dashboard';
import DownloadProducts from 'calypso/jetpack-cloud/sections/partner-portal/primary/download-products';
import InvoicesDashboard from 'calypso/jetpack-cloud/sections/partner-portal/primary/invoices-dashboard';
import IssueLicense from 'calypso/jetpack-cloud/sections/partner-portal/primary/issue-license';
import Licenses from 'calypso/jetpack-cloud/sections/partner-portal/primary/licenses';
import PartnerAccess from 'calypso/jetpack-cloud/sections/partner-portal/primary/partner-access';
import PaymentMethodAdd from 'calypso/jetpack-cloud/sections/partner-portal/primary/payment-method-add';
import PaymentMethodList from 'calypso/jetpack-cloud/sections/partner-portal/primary/payment-method-list';
import TermsOfServiceConsent from 'calypso/jetpack-cloud/sections/partner-portal/primary/terms-of-service-consent';
import {
	LicenseFilter,
	LicenseSortDirection,
	LicenseSortField,
} from 'calypso/jetpack-cloud/sections/partner-portal/types';
import JetpackManageSidebar from 'calypso/jetpack-cloud/sections/sidebar-navigation/jetpack-manage';
import PurchasesSidebar from 'calypso/jetpack-cloud/sections/sidebar-navigation/purchases';
import { addQueryArgs } from 'calypso/lib/route';
import {
	getCurrentPartner,
	hasActivePartnerKey,
	doesPartnerRequireAPaymentMethod,
} from 'calypso/state/partner-portal/partner/selectors';
import { ToSConsent } from 'calypso/state/partner-portal/types';
import getSites from 'calypso/state/selectors/get-sites';
import { setAllSitesSelected } from 'calypso/state/ui/actions/set-sites';
import Header from './header';
import PaymentMethodAddV2 from './primary/payment-method-add-v2';
import PaymentMethodListV2 from './primary/payment-methods-v2';
import WPCOMAtomicHosting from './primary/wpcom-atomic-hosting';

const isNewCardAdditionEnabled = isEnabled( 'jetpack/card-addition-improvements' );

const setSidebar = ( context: Context, isLicenseContext: boolean = false ): void => {
	context.secondary = isLicenseContext ? (
		<JetpackManageSidebar path={ context.path } />
	) : (
		<PurchasesSidebar path={ context.path } />
	);
};

export const allSitesContext: Callback = ( context, next ) => {
	// Many (if not all) Partner Portal pages do not select any one specific site
	context.store.dispatch( setAllSitesSelected() );
	next();
};

export const partnerContext: Callback = ( context, next ) => {
	context.header = <Header />;
	context.primary = <PartnerAccess />;
	next();
};

export const termsOfServiceContext: Callback = ( context, next ) => {
	context.header = <Header />;
	context.primary = <TermsOfServiceConsent />;
	next();
};

export const partnerKeyContext: Callback = ( context, next ) => {
	context.header = <Header />;
	context.primary = <LicenseSelectPartnerKey />;
	next();
};

export const billingDashboardContext: Callback = ( context, next ) => {
	context.header = <Header />;
	setSidebar( context );
	context.primary = <BillingDashboard />;
	next();
};

export const licensesContext: Callback = ( context, next ) => {
	const { s: search, sort_field, sort_direction, page } = context.query;
	const filter = publicToInternalLicenseFilter( context.params.filter, LicenseFilter.NotRevoked );
	const currentPage = parseInt( page ) || 1;
	const sortField = publicToInternalLicenseSortField( sort_field, LicenseSortField.IssuedAt );
	const sortDirection = valueToEnum< LicenseSortDirection >(
		LicenseSortDirection,
		sort_direction,
		LicenseSortDirection.Descending
	);

	context.header = <Header />;
	setSidebar( context, true );
	context.primary = (
		<Licenses
			filter={ filter }
			search={ search || '' }
			currentPage={ currentPage }
			sortDirection={ sortDirection }
			sortField={ sortField }
		/>
	);
	next();
};

export const issueLicenseContext: Callback = ( context, next ) => {
	const { site_id: siteId, product_slug: suggestedProduct } = context.query;
	const state = context.store.getState();
	const sites = getSites( state );
	const selectedSite = siteId ? sites.find( ( site ) => site?.ID === parseInt( siteId ) ) : null;
	context.header = <Header />;
	setSidebar( context, true );
	context.primary = (
		<IssueLicense selectedSite={ selectedSite } suggestedProduct={ suggestedProduct } />
	);
	next();
};

export const downloadProductsContext: Callback = ( context, next ) => {
	context.header = <Header />;
	setSidebar( context );
	context.primary = <DownloadProducts />;
	next();
};

export const assignLicenseContext: Callback = ( context, next ) => {
	const { page, search } = context.query;
	const state = context.store.getState();
	const sites = getSites( state );
	const currentPage = parseInt( page ) || 1;

	context.header = <Header />;
	setSidebar( context, true );
	context.primary = (
		<AssignLicense sites={ sites } currentPage={ currentPage } search={ search || '' } />
	);
	next();
};

export const paymentMethodListContext: Callback = ( context, next ) => {
	context.header = <Header />;
	setSidebar( context );
	context.primary = isNewCardAdditionEnabled ? <PaymentMethodListV2 /> : <PaymentMethodList />;
	next();
};

export const paymentMethodAddContext: Callback = ( context, next ) => {
	context.header = <Header />;
	setSidebar( context );

	const { site_id: siteId } = context.query;
	const state = context.store.getState();
	const sites = getSites( state );
	const selectedSite = siteId ? sites?.find( ( site ) => site?.ID === parseInt( siteId ) ) : null;
	context.primary = isNewCardAdditionEnabled ? (
		<PaymentMethodAddV2 withAssignLicense={ ! selectedSite } />
	) : (
		<PaymentMethodAdd selectedSite={ selectedSite } />
	);
	next();
};

export const invoicesDashboardContext: Callback = ( context, next ) => {
	context.header = <Header />;
	setSidebar( context );
	context.primary = <InvoicesDashboard />;
	next();
};

export const companyDetailsDashboardContext: Callback = ( context, next ) => {
	context.header = <Header />;
	setSidebar( context );
	context.primary = <CompanyDetailsDashboard />;
	next();
};

export const pricesContext: Callback = () => {
	page.redirect( '/partner-portal/issue-license' );
};

export const landingPageContext: Callback = () => {
	page.redirect( '/partner-portal/billing' );
};

export const wpcomAtomicHostingContext: Callback = ( context, next ) => {
	context.header = <Header />;
	setSidebar( context );
	context.primary = <WPCOMAtomicHosting />;
	next();
};

/**
 * Require the user to have a partner with at least 1 active partner key.
 */
export const requireAccessContext: Callback = ( context, next ) => {
	const state = context.store.getState();
	const partner = getCurrentPartner( state );
	const { pathname, search } = window.location;

	if ( partner ) {
		next();
		return;
	}

	page.redirect(
		addQueryArgs(
			{
				return: ensurePartnerPortalReturnUrl( pathname + search ),
			},
			'/partner-portal/partner'
		)
	);
};

/**
 * Require the user to have consented to the terms of service.
 */
export const requireTermsOfServiceConsentContext: Callback = ( context, next ) => {
	const { pathname, search } = window.location;
	const state = context.store.getState();
	const partner = getCurrentPartner( state );

	if ( partner && partner.tos !== ToSConsent.NotConsented ) {
		next();
		return;
	}

	const returnUrl = ensurePartnerPortalReturnUrl( pathname + search );

	page.redirect(
		addQueryArgs(
			{
				return: returnUrl,
			},
			'/partner-portal/terms-of-service'
		)
	);
};

/**
 * Require the user to have selected a partner key to use.
 */
export const requireSelectedPartnerKeyContext: Callback = ( context, next ) => {
	const state = context.store.getState();
	const hasKey = hasActivePartnerKey( state );
	const { pathname, search } = window.location;

	if ( hasKey ) {
		next();
		return;
	}

	const returnUrl = ensurePartnerPortalReturnUrl( pathname + search );

	page.redirect(
		addQueryArgs(
			{
				return: returnUrl,
			},
			'/partner-portal/partner-key'
		)
	);
};

/**
 * Require the user to have a valid payment method registered.
 */
export const requireValidPaymentMethod: Callback = ( context, next ) => {
	const state = context.store.getState();
	const paymentMethodRequired = doesPartnerRequireAPaymentMethod( state );
	const { pathname, search } = window.location;

	if ( paymentMethodRequired ) {
		const returnUrl = ensurePartnerPortalReturnUrl( pathname + search );

		page.redirect(
			addQueryArgs(
				{
					return: returnUrl,
				},
				'/partner-portal/payment-methods/add'
			)
		);
		return;
	}

	next();
};