-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Framework: Add makeLayout
#5081
Changes from 13 commits
db728f2
e779050
f448723
33ecc30
2b0e7fc
201a080
bda0b78
29571a7
97fce7c
4989186
69fb078
f892793
30d5ad7
cbcb62b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Isomorphic Routing Helpers | ||
========================== | ||
|
||
Provides middleware to be used agnostically with either `page.js`, or with | ||
`express` (through middleware adapters found in `server/`). Since some of the | ||
middleware needs to function differently on the server than on the client, | ||
both versions are provided transparently in those cases (by `index.web.js` and | ||
`index.node.js`, respectively.) | ||
|
||
These middlewares include: | ||
* `makeLayout`: creates a `Layout` (or `LayoutLoggedOut`) component in `context.layout`. | ||
Accepts `primary`, `secondary`, and `tertiary` arguments which it will use to | ||
populate the corresponding `<div>`s. | ||
* `clientRouter`: Essentially an alias for `page`, which invokes a client-side | ||
`render` middleware after all other middlewares. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* External Dependencies | ||
*/ | ||
import React from 'react'; | ||
import { Provider as ReduxProvider } from 'react-redux'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import LayoutLoggedOut from 'layout/logged-out'; | ||
import { getCurrentUser } from 'state/current-user/selectors'; | ||
|
||
/** | ||
* Re-export | ||
*/ | ||
export { setSection } from './shared.js'; | ||
|
||
export function makeLayoutMiddleware( LayoutComponent ) { | ||
return ( context, next ) => { | ||
const { store, primary, secondary, tertiary } = context; | ||
|
||
// On server, only render LoggedOutLayout when logged-out. | ||
if ( ! context.isServerSide || getCurrentUser( context.store.getState() ) ) { | ||
context.layout = ( | ||
<LayoutComponent store={ store } | ||
primary={ primary } | ||
secondary={ secondary } | ||
tertiary={ tertiary } | ||
/> | ||
); | ||
} | ||
next(); | ||
}; | ||
} | ||
|
||
const ReduxWrappedLoggedOutLayout = ( { store, primary, secondary, tertiary } ) => ( | ||
<ReduxProvider store={ store }> | ||
<LayoutLoggedOut primary={ primary } | ||
secondary={ secondary } | ||
tertiary={ tertiary } /> | ||
</ReduxProvider> | ||
); | ||
|
||
/** | ||
* @param { object } context -- Middleware context | ||
* @param { function } next -- Call next middleware in chain | ||
* | ||
* Produce a `LayoutLoggedOut` element in `context.layout`, using | ||
* `context.primary`, `context.secondary`, and `context.tertiary` to populate it. | ||
*/ | ||
export const makeLayout = makeLayoutMiddleware( ReduxWrappedLoggedOutLayout ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,48 +4,49 @@ | |
import React from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason for having controllers under a folder? I prefer to have the files directly in the root. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because we need separate client/server versions of the controller, in combination with the |
||
import ReactDom from 'react-dom'; | ||
import { Provider as ReduxProvider } from 'react-redux'; | ||
import { setSection as setSectionAction } from 'state/ui/actions'; | ||
import noop from 'lodash/noop'; | ||
import page from 'page'; | ||
|
||
/** | ||
* Internal dependencies | ||
* Internal Dependencies | ||
*/ | ||
import page from 'page'; | ||
import Layout from 'layout'; | ||
import LayoutLoggedOut from 'layout/logged-out'; | ||
import nuxWelcome from 'layout/nux-welcome'; | ||
import translatorInvitation from 'layout/community-translator/invitation-utils'; | ||
import { makeLayoutMiddleware } from './index.node.js'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should go in |
||
import { getCurrentUser } from 'state/current-user/selectors'; | ||
import userFactory from 'lib/user'; | ||
import sitesFactory from 'lib/sites-list'; | ||
import debugFactory from 'debug'; | ||
|
||
const debug = debugFactory( 'calypso:controller' ); | ||
|
||
/** | ||
* @param { object } context -- Middleware context | ||
* @param { function } next -- Call next middleware in chain | ||
*/ | ||
export function makeLayout( context, next ) { | ||
const isLoggedIn = !! getCurrentUser( context.store.getState() ); | ||
if ( ! isLoggedIn ) { | ||
context.layout = makeLoggedOutLayout( context ); | ||
} // TODO: else { makeLoggedInLayout( context ); } | ||
next(); | ||
} | ||
* Re-export | ||
*/ | ||
export { setSection } from './shared.js'; | ||
|
||
/** | ||
* @param { object } context -- Middleware context | ||
* @returns { object } `LoggedOutLayout` element | ||
* | ||
* Return a `LayoutLoggedOut` element, using `context.primary`, | ||
* `context.secondary`, and `context.tertiary` to populate it. | ||
*/ | ||
function makeLoggedOutLayout( context ) { | ||
const { store, primary, secondary, tertiary } = context; | ||
return ( | ||
<ReduxProvider store={ store }> | ||
<LayoutLoggedOut primary={ primary } | ||
const user = userFactory(); | ||
const sites = sitesFactory(); | ||
const debug = debugFactory( 'calypso:controller' ); | ||
|
||
export const ReduxWrappedLayout = ( { store, primary, secondary, tertiary } ) => ( | ||
<ReduxProvider store={ store }> | ||
{ getCurrentUser( store.getState() ) | ||
? <Layout primary={ primary } | ||
secondary={ secondary } | ||
tertiary={ tertiary } | ||
user={ user } | ||
sites={ sites } | ||
nuxWelcome={ nuxWelcome } | ||
translatorInvitation={ translatorInvitation } | ||
/> | ||
: <LayoutLoggedOut primary={ primary } | ||
secondary={ secondary } | ||
tertiary={ tertiary } /> | ||
</ReduxProvider> | ||
); | ||
} | ||
} | ||
</ReduxProvider> | ||
); | ||
|
||
export const makeLayout = makeLayoutMiddleware( ReduxWrappedLayout ); | ||
|
||
/** | ||
* Isomorphic routing helper, client side | ||
|
@@ -65,14 +66,6 @@ export function clientRouter( route, ...middlewares ) { | |
page( route, ...middlewares, render ); | ||
} | ||
|
||
export function setSection( section ) { | ||
return ( context, next = noop ) => { | ||
context.store.dispatch( setSectionAction( section ) ); | ||
|
||
next(); | ||
}; | ||
} | ||
|
||
function render( context ) { | ||
context.layout | ||
? renderSingleTree( context ) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"main": "index.node.js", | ||
"browser": "index.web.js" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { setSection as setSectionAction } from 'state/ui/actions'; | ||
import noop from 'lodash/noop'; | ||
|
||
export function setSection( section ) { | ||
return ( context, next = noop ) => { | ||
context.store.dispatch( setSectionAction( section ) ); | ||
|
||
next(); | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the motivation for adding this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem to fix the memory leak going /design -> /start and back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This: #5081 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(which was causing #5081 (comment))