forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.js
77 lines (66 loc) · 2.31 KB
/
root.js
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
/**
* External dependencies
*/
import page from 'page';
/**
* Internal dependencies
*/
import config from '@automattic/calypso-config';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import { requestSite } from 'calypso/state/sites/actions';
import getPrimarySiteId from 'calypso/state/selectors/get-primary-site-id';
import { canCurrentUserUseCustomerHome, getSite, getSiteSlug } from 'calypso/state/sites/selectors';
export default function () {
page( '/', ( context ) => {
const isLoggedIn = isUserLoggedIn( context.store.getState() );
if ( isLoggedIn ) {
handleLoggedIn( context );
} else {
handleLoggedOut( context );
}
} );
}
function handleLoggedOut() {
if ( config.isEnabled( 'devdocs/redirect-loggedout-homepage' ) ) {
page.redirect( '/devdocs/start' );
} else if ( config.isEnabled( 'jetpack-cloud' ) ) {
if ( config.isEnabled( 'oauth' ) ) {
page.redirect( '/connect' );
}
}
}
// Helper thunk that ensures that the requested site info is fetched into Redux state before we
// continue working with it.
// The `siteSelection` handler in `my-sites/controller` contains similar code.
const waitForSite = ( siteId ) => async ( dispatch, getState ) => {
if ( getSite( getState(), siteId ) ) {
return;
}
try {
await dispatch( requestSite( siteId ) );
} catch {
// if the fetching of site info fails, return gracefully and proceed to redirect to Reader
}
};
async function handleLoggedIn( context ) {
// determine the primary site ID (it's a property of "current user" object) and then
// ensure that the primary site info is loaded into Redux before proceeding.
const primarySiteId = getPrimarySiteId( context.store.getState() );
await context.store.dispatch( waitForSite( primarySiteId ) );
const state = context.store.getState();
const siteSlug = getSiteSlug( state, primarySiteId );
const isCustomerHomeEnabled = canCurrentUserUseCustomerHome( state, primarySiteId );
let redirectPath;
if ( ! siteSlug ) {
// there is no primary site or the site info couldn't be fetched. Redirect to Reader.
redirectPath = '/read';
} else if ( isCustomerHomeEnabled ) {
redirectPath = `/home/${ siteSlug }`;
} else {
redirectPath = `/stats/${ siteSlug }`;
}
if ( context.querystring ) {
redirectPath += `?${ context.querystring }`;
}
page.redirect( redirectPath );
}