File size: 9,800 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
292
293
294
295
296
297
298
299
300
301
302
303
import page from '@automattic/calypso-router';
import { CheckoutErrorBoundary } from '@automattic/composite-checkout';
import { localize, useTranslate } from 'i18n-calypso';
import { Fragment, useCallback } from 'react';
import DocumentHead from 'calypso/components/data/document-head';
import NoSitesMessage from 'calypso/components/empty-content/no-sites-message';
import HeaderCake from 'calypso/components/header-cake';
import Main from 'calypso/components/main';
import NavigationHeader from 'calypso/components/navigation-header';
import { makeLayout, render as clientRender } from 'calypso/controller';
import { useGeoLocationQuery } from 'calypso/data/geo/use-geolocation-query';
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
import AddNewPaymentMethod from 'calypso/me/purchases/add-new-payment-method';
import ChangePaymentMethod from 'calypso/me/purchases/manage-purchase/change-payment-method';
import {
	managePurchase as managePurchaseUrl,
	purchasesRoot,
	vatDetails as vatDetailsPath,
	billingHistory,
} from 'calypso/me/purchases/paths';
import PurchasesNavigation from 'calypso/me/purchases/purchases-navigation';
import { useTaxName } from 'calypso/my-sites/checkout/src/hooks/use-country-list';
import { logStashLoadErrorEvent } from 'calypso/my-sites/checkout/src/lib/analytics';
import CrmDownloads from 'calypso/my-sites/purchases/crm-downloads';
import { useSelector } from 'calypso/state';
import { getCurrentUser, getCurrentUserSiteCount } from 'calypso/state/current-user/selectors';
import getPreviousRoute from 'calypso/state/selectors/get-previous-route';
import CancelPurchase from './cancel-purchase';
import ConfirmCancelDomain from './confirm-cancel-domain';
import { Downgrade } from './downgrade';
import ManagePurchase from './manage-purchase';
import { ManagePurchaseByOwnership } from './manage-purchase/manage-purchase-by-ownership';
import PurchasesListDataView from './purchases-list-in-dataviews';
import titles from './titles';
import VatInfoPage from './vat-info';
import useVatDetails from './vat-info/use-vat-details';

/**
 * Returns the previous page URL if it is one of the two purchases lists
 * (account-level or site-level), including query strings.
 * @returns string|undefined
 */
function usePreviousUrlIfPurchasesList() {
	const previousRoute = useSelector( getPreviousRoute );
	return /\/me\/purchases\/?[^/]*$/.test( previousRoute ) ||
		/\/purchases\/subscriptions\/?[^/]*$/.test( previousRoute )
		? previousRoute
		: undefined;
}

function useLogPurchasesError( message ) {
	return useCallback(
		( error ) => {
			logStashLoadErrorEvent( 'account_level_purchases', error, { message } );
		},
		[ message ]
	);
}

const PurchasesWrapper = ( { title = null, children } ) => {
	return (
		<Fragment>
			<DocumentHead title={ title } />
			{ children }
		</Fragment>
	);
};
const noop = () => {};
const userHasNoSites = ( state ) => getCurrentUserSiteCount( state ) <= 0;

function noSites( context, analyticsPath ) {
	const NoSitesWrapper = localize( () => {
		return (
			<PurchasesWrapper>
				<Main wideLayout className="purchases__no-site">
					<PageViewTracker path={ analyticsPath } title="Purchases > No Sites" />
					<PurchasesNavigation section="activeUpgrades" />
					<NoSitesMessage />
				</Main>
			</PurchasesWrapper>
		);
	} );

	context.primary = <NoSitesWrapper />;
	makeLayout( context, noop );
	clientRender( context );
}

export function addCreditCard( context, next ) {
	context.primary = <AddNewPaymentMethod />;
	next();
}

export function cancelPurchase( context, next ) {
	const CancelPurchaseWrapper = localize( () => {
		return (
			<PurchasesWrapper title={ titles.cancelPurchase }>
				<Main wideLayout className="purchases__cancel">
					<CancelPurchase
						purchaseId={ parseInt( context.params.purchaseId, 10 ) }
						siteSlug={ context.params.site }
					/>
				</Main>
			</PurchasesWrapper>
		);
	} );

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

export function downgradePurchase( context, next ) {
	const DowngradePurchaseWrapper = localize( () => {
		return (
			<PurchasesWrapper title={ titles.downgradeSubscription() }>
				<Main wideLayout className="purchases__cancel">
					<Downgrade
						purchaseId={ parseInt( context.params.purchaseId, 10 ) }
						siteSlug={ context.params.site }
					/>
				</Main>
			</PurchasesWrapper>
		);
	} );

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

export function confirmCancelDomain( context, next ) {
	const state = context.store.getState();

	if ( userHasNoSites( state ) ) {
		return noSites( context, '/me/purchases/:site/:purchaseId/confirm-cancel-domain' );
	}

	const ConfirmCancelDomainWrapper = localize( () => {
		return (
			<PurchasesWrapper title={ titles.confirmCancelDomain }>
				<Main wideLayout className="purchases__cancel-domain confirm-cancel-domain">
					<ConfirmCancelDomain
						purchaseId={ parseInt( context.params.purchaseId, 10 ) }
						siteSlug={ context.params.site }
					/>
				</Main>
			</PurchasesWrapper>
		);
	} );

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

export function list( context, next ) {
	const state = context.store.getState();
	const currentUser = getCurrentUser( state );
	const userId = currentUser?.ID;
	const ListWrapper = localize( () => {
		return (
			<PurchasesWrapper>
				<PurchasesListDataView
					userId={ userId }
					noticeType={ context.params.noticeType }
					getManagePurchaseUrlFor={ managePurchaseUrl }
				/>
			</PurchasesWrapper>
		);
	} );

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

export function vatDetails( context, next ) {
	const VatInfoWrapper = localize( () => {
		const goToBillingHistory = () => page( billingHistory );
		const classes = 'vat-details';

		const translate = useTranslate();
		const { data: geoData } = useGeoLocationQuery();
		const { vatDetails: vatDetailsFromServer } = useVatDetails();
		const taxName = useTaxName( vatDetailsFromServer.country ?? geoData?.country_short ?? 'GB' );
		const genericTaxName =
			/* translators: This is a generic name for taxes to use when we do not know the user's country. */
			translate( 'tax (VAT/GST/CT)' );
		const fallbackTaxName = genericTaxName;
		/* translators: %s is the name of taxes in the country (eg: "VAT" or "GST"). */
		const title = translate( 'Add %s details', {
			textOnly: true,
			args: [ taxName ?? fallbackTaxName ],
		} );

		return (
			<PurchasesWrapper title={ title }>
				<Main wideLayout className={ classes }>
					<PageViewTracker path={ vatDetailsPath } title="Purchases > VAT Details" />

					<NavigationHeader navigationItems={ [] } title={ titles.sectionTitle } />
					<HeaderCake onClick={ goToBillingHistory }>{ title }</HeaderCake>

					<VatInfoPage siteSlug={ context.params.site } />
				</Main>
			</PurchasesWrapper>
		);
	} );

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

export function managePurchase( context, next ) {
	const ManagePurchasesWrapper = localize( () => {
		const classes = 'manage-purchase';
		const purchaseListUrl = usePreviousUrlIfPurchasesList();

		return (
			<PurchasesWrapper title={ titles.managePurchase }>
				<Main wideLayout className={ classes }>
					<NavigationHeader navigationItems={ [] } title={ titles.sectionTitle } />
					<PageViewTracker
						path="/me/purchases/:site/:purchaseId"
						title="Purchases > Manage Purchase"
					/>
					<ManagePurchase
						purchaseId={ parseInt( context.params.purchaseId, 10 ) }
						siteSlug={ context.params.site }
						purchaseListUrl={ purchaseListUrl }
					/>
				</Main>
			</PurchasesWrapper>
		);
	} );

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

export function managePurchaseByOwnership( context, next ) {
	const ManagePurchasesByOwnershipWrapper = localize( () => {
		const classes = 'manage-purchase';

		return (
			<PurchasesWrapper title={ titles.managePurchase }>
				<Main wideLayout className={ classes }>
					<NavigationHeader navigationItems={ [] } title={ titles.sectionTitle } />
					<PageViewTracker
						path="/me/purchases/:ownershipId"
						title="Purchases > Manage Purchase by Ownership"
					/>
					<ManagePurchaseByOwnership ownershipId={ parseInt( context.params.ownershipId, 10 ) } />
				</Main>
			</PurchasesWrapper>
		);
	} );

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

export function addNewPaymentMethod( context, next ) {
	const AddNewPaymentMethodTopWrapper = () => {
		const purchaseListUrl = usePreviousUrlIfPurchasesList();
		return <AddNewPaymentMethod purchaseListUrl={ purchaseListUrl } />;
	};
	context.primary = <AddNewPaymentMethodTopWrapper />;
	next();
}

export function changePaymentMethod( context, next ) {
	const ChangePaymentMethodWrapper = () => {
		const translate = useTranslate();
		const logPurchasesError = useLogPurchasesError(
			'account level purchases change payment method load error'
		);
		return (
			<PurchasesWrapper title={ titles.changePaymentMethod }>
				<Main wideLayout className="purchases__edit-payment-method">
					<NavigationHeader navigationItems={ [] } title={ titles.sectionTitle } />
					<CheckoutErrorBoundary
						errorMessage={ translate( 'Sorry, there was an error loading this page.' ) }
						onError={ logPurchasesError }
					>
						<ChangePaymentMethod
							purchaseId={ parseInt( context.params.purchaseId, 10 ) }
							siteSlug={ context.params.site }
							getManagePurchaseUrlFor={ managePurchaseUrl }
							purchaseListUrl={ purchasesRoot }
						/>
					</CheckoutErrorBoundary>
				</Main>
			</PurchasesWrapper>
		);
	};

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

export function crmDownloads( context, next ) {
	context.primary = <CrmDownloads subscription={ context.params.subscription } />;
	next();
}