-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathcontroller.jsx
More file actions
168 lines (141 loc) · 4.38 KB
/
controller.jsx
File metadata and controls
168 lines (141 loc) · 4.38 KB
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
/**
* External Dependencies
*/
import { compact, includes, isEmpty, startsWith } from 'lodash';
import debugFactory from 'debug';
import React from 'react';
/**
* Internal Dependencies
*/
import SingleSiteComponent from 'my-sites/themes/single-site';
import MultiSiteComponent from 'my-sites/themes/multi-site';
import LoggedOutComponent from './logged-out';
import Upload from 'my-sites/themes/theme-upload';
import trackScrollPage from 'lib/track-scroll-page';
import { DEFAULT_THEME_QUERY } from 'state/themes/constants';
import { requestThemes, requestThemeFilters, setBackPath } from 'state/themes/actions';
import { getThemesForQuery } from 'state/themes/selectors';
import { getAnalyticsData } from './helpers';
import { getThemeFilters } from 'state/selectors';
const debug = debugFactory( 'calypso:themes' );
function getProps( context ) {
const { tier, filter, vertical, site_id: siteId } = context.params;
const { basePath, analyticsPageTitle } = getAnalyticsData(
context.path,
tier,
siteId
);
const boundTrackScrollPage = function() {
trackScrollPage(
basePath,
analyticsPageTitle,
'Themes'
);
};
return {
tier,
filter,
vertical,
analyticsPageTitle,
analyticsPath: basePath,
search: context.query.s,
trackScrollPage: boundTrackScrollPage
};
}
export function upload( context, next ) {
// Store previous path to return to only if it was main showcase page
if ( startsWith( context.prevPath, '/themes' ) &&
! startsWith( context.prevPath, '/themes/upload' ) ) {
context.store.dispatch( setBackPath( context.prevPath ) );
}
context.primary = <Upload />;
next();
}
export function singleSite( context, next ) {
// Scroll to the top
if ( typeof window !== 'undefined' ) {
window.scrollTo( 0, 0 );
}
context.primary = <SingleSiteComponent { ...getProps( context ) } />;
next();
}
export function multiSite( context, next ) {
const props = getProps( context );
// Scroll to the top
if ( typeof window !== 'undefined' ) {
window.scrollTo( 0, 0 );
}
context.primary = <MultiSiteComponent { ...props } />;
next();
}
export function loggedOut( context, next ) {
if ( context.isServerSide && ! isEmpty( context.query ) ) {
// Don't server-render URLs with query params
return next();
}
const props = getProps( context );
context.primary = <LoggedOutComponent { ...props } />;
next();
}
export function fetchThemeData( context, next ) {
if ( ! context.isServerSide ) {
return next();
}
const siteId = 'wpcom';
const query = {
search: context.query.s,
tier: context.params.tier,
filter: compact( [ context.params.filter, context.params.vertical ] ).join( ',' ),
page: 1,
number: DEFAULT_THEME_QUERY.number,
};
const themes = getThemesForQuery( context.store.getState(), siteId, query );
if ( themes ) {
debug( 'found theme data in cache' );
return next();
}
context.store.dispatch( requestThemes( siteId, query ) ).then( next ).catch( next );
}
export function fetchThemeFilters( context, next ) {
const { store } = context;
if ( ! isEmpty( getThemeFilters( store.getState() ) ) ) {
debug( 'found theme filters in cache' );
return next();
}
const unsubscribe = store.subscribe( () => {
if ( ! isEmpty( getThemeFilters( store.getState() ) ) ) {
unsubscribe();
return next();
}
} );
store.dispatch( requestThemeFilters() );
}
// Legacy (Atlas-based Theme Showcase v4) route redirects
export function redirectSearchAndType( { res, params: { site, search, tier } } ) {
const target = '/themes/' + compact( [ tier, site ] ).join( '/' ); // tier before site!
if ( search ) {
res.redirect( `${ target }?s=${ search }` );
} else {
res.redirect( target );
}
}
export function redirectFilterAndType( { res, params: { site, filter, tier } } ) {
let parts;
if ( filter ) {
parts = [ tier, 'filter', filter.replace( '+', ',' ), site ]; // The Atlas Showcase used plusses, we use commas
} else {
parts = [ tier, site ];
}
res.redirect( '/themes/' + compact( parts ).join( '/' ) );
}
export function redirectToThemeDetails( { res, params: { site, theme, section } }, next ) {
// Make sure we aren't matching a site -- e.g. /themes/example.wordpress.com or /themes/1234567
if ( includes( theme, '.' ) || isFinite( theme ) ) {
return next();
}
let redirectedSection;
if ( section === 'support' ) {
redirectedSection = 'setup';
}
res.redirect( '/theme/' + compact( [ theme, redirectedSection, site ] ).join( '/' ) );
}