File size: 2,885 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
import config from '@automattic/calypso-config';
import page from '@automattic/calypso-router';
import {
	campaignDetails,
	promoteWidget,
	promotedPosts,
	checkValidTabInNavigation,
} from 'calypso/my-sites/promote-post-i2/controller';
import { getAdvertisingDashboardPath } from 'calypso/my-sites/promote-post-i2/utils';
import { setSelectedSiteId } from 'calypso/state/ui/actions';
import { makeLayout, render as clientRender } from './page-middleware/layout';
import { setup } from './pages/controller';

import 'calypso/my-sites/promote-post-i2/style.scss';
// Needed because the placeholder component that we use doesn't import the css, webpack excludes it from the final build
// import 'calypso/blocks/site/style.scss';

const siteSelection = ( context, next ) => {
	context.store.dispatch( setSelectedSiteId( config( 'blog_id' ) ) );
	next( context, next );
};

const setupMode = ( context, next ) => {
	const setupPath = getAdvertisingDashboardPath( '/setup/' );

	// Redirect to root if setup mode is disabled and the user is trying to load the setup page
	if ( context.path?.includes( setupPath ) && ! config.isEnabled( 'blaze_setup_mode' ) ) {
		page.redirect( getAdvertisingDashboardPath( `/${ config( 'hostname' ) }` ) );
		return;
	}

	// Redirect to setup if setup mode is enabled and the user is trying load another section of the app
	if ( ! context.path?.includes( setupPath ) && config.isEnabled( 'blaze_setup_mode' ) ) {
		page.redirect( getAdvertisingDashboardPath( `/setup/${ config( 'hostname' ) }` ) );
		return;
	}

	next( context, next );
};

const blazePage = ( url, ...controller ) => {
	page( url, setupMode, ...controller, siteSelection, makeLayout, clientRender );
};

const redirectToReadyToPromote = () => {
	page.redirect( getAdvertisingDashboardPath( `/${ config( 'hostname' ) }` ) );
};

export default function ( pageBase = '/' ) {
	page.base( pageBase );

	blazePage( getAdvertisingDashboardPath( '/setup/:site' ), setup );

	blazePage( getAdvertisingDashboardPath( '/:site' ), promotedPosts );

	blazePage(
		getAdvertisingDashboardPath( '/:tab/:site' ),
		checkValidTabInNavigation,
		promotedPosts
	);

	blazePage( getAdvertisingDashboardPath( '/campaigns/:campaignId/:site' ), campaignDetails );

	blazePage( getAdvertisingDashboardPath( '/promote/:item/:site' ), promoteWidget );

	blazePage( getAdvertisingDashboardPath( '/:tab/promote/:item/:site' ), promoteWidget );

	// Compatibility: Redirects to new navigation style
	page( getAdvertisingDashboardPath( '/:site/:tab/promote/:item' ), ( { params } ) => {
		const { site, tab, item } = params;
		page.redirect( getAdvertisingDashboardPath( `/${ tab }/promote/${ item }/${ site }` ) );
	} );

	// Anything else should redirect to default advertising dashboard page
	blazePage( '*', redirectToReadyToPromote );

	// Enable hashbang for routing in Jetpack.
	page( { hashbang: true } );
}