File size: 3,286 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
import page from '@automattic/calypso-router';
import { getLanguageRouteParam } from '@automattic/i18n-utils';
import {
	makeLayout,
	redirectLoggedOut,
	redirectWithoutLocaleParamIfLoggedIn,
	render as clientRender,
} from 'calypso/controller';
import {
	addNavigationIfLoggedIn,
	navigation,
	noSite,
	siteSelection,
	sites,
} from 'calypso/my-sites/controller';
import { fetchThemeData, redirectToThemeDetails } from './controller';
import { renderThemes, upload } from './controller-logged-in';
import { getTierRouteParam } from './helpers';
import { fetchAndValidateVerticalsAndFilters } from './validate-filters';

export default function ( router ) {
	const siteId =
		'\\d+' + // numeric site id
		'|' + // or
		'[^\\\\/.]+\\.[^\\\\/]+'; // one-or-more non-slash-or-dot chars, then a dot, then one-or-more non-slashes

	const langParam = getLanguageRouteParam();
	const tierParam = getTierRouteParam();

	const routesWithoutSites = [
		`/${ langParam }/themes/${ tierParam }/:view(collection)?`,
		`/${ langParam }/themes/${ tierParam }/filter/:filter/:view(collection)?`,
		`/${ langParam }/themes/:category(all|my-themes)?/${ tierParam }/:view(collection)?`,
		`/${ langParam }/themes/:category(all|my-themes)?/${ tierParam }/filter/:filter/:view(collection)?`,
		`/${ langParam }/themes/:vertical?/${ tierParam }/:view(collection)?`,
		`/${ langParam }/themes/:vertical?/${ tierParam }/filter/:filter/:view(collection)?`,
	];
	const routesWithSites = [
		`/${ langParam }/themes/${ tierParam }/:view(collection)?/:site_id(${ siteId })`,
		`/${ langParam }/themes/${ tierParam }/filter/:filter/:view(collection)?/:site_id(${ siteId })`,
		`/${ langParam }/themes/:category(all|my-themes)?/${ tierParam }/:view(collection)?/:site_id(${ siteId })`,
		`/${ langParam }/themes/:category(all|my-themes)?/${ tierParam }/filter/:filter/:view(collection)?/:site_id(${ siteId })`,
		`/${ langParam }/themes/:vertical?/${ tierParam }/:view(collection)?/:site_id(${ siteId })`,
		`/${ langParam }/themes/:vertical?/${ tierParam }/filter/:filter/:view(collection)?/:site_id(${ siteId })`,
	];

	// Upload routes are valid only when logged in. In logged-out sessions they redirect to login page.
	router( '/themes/upload', redirectLoggedOut, siteSelection, sites, makeLayout, clientRender );
	router(
		'/themes/upload/:site_id',
		redirectLoggedOut,
		siteSelection,
		upload,
		navigation,
		makeLayout,
		clientRender
	);

	router(
		routesWithSites,
		redirectWithoutLocaleParamIfLoggedIn,
		redirectLoggedOut,
		fetchAndValidateVerticalsAndFilters,
		siteSelection,
		renderThemes,
		navigation,
		makeLayout,
		clientRender
	);

	router(
		routesWithoutSites,
		redirectWithoutLocaleParamIfLoggedIn,
		fetchAndValidateVerticalsAndFilters,
		noSite,
		renderThemes,
		addNavigationIfLoggedIn,
		makeLayout,
		clientRender
	);

	/**
	 * Although we redirect /themes/:theme from validateVerticals, we still need to redirect users from /themes/:theme/support.
	 */
	router(
		[
			'/themes/:theme/:section(support)?',
			`/themes/:theme/:section(support)?/:site_id(${ siteId })`,
		],
		( { params: { site_id, theme, section } }, next ) =>
			redirectToThemeDetails( page.redirect, site_id, theme, section, next )
	);

	router( '/themes/*', fetchThemeData, renderThemes, makeLayout );
}