|
|
import { isUserLoggedIn } from 'calypso/state/current-user/selectors'; |
|
|
import { setSection } from 'calypso/state/ui/actions'; |
|
|
import { setLocale } from 'calypso/state/ui/language/actions'; |
|
|
|
|
|
const noop = () => {}; |
|
|
|
|
|
export function makeLayoutMiddleware( LayoutComponent ) { |
|
|
return ( context, next ) => { |
|
|
const { |
|
|
i18n, |
|
|
store, |
|
|
queryClient, |
|
|
section, |
|
|
pathname, |
|
|
query, |
|
|
primary, |
|
|
secondary, |
|
|
renderHeaderSection, |
|
|
showGdprBanner, |
|
|
cachedMarkup, |
|
|
} = context; |
|
|
|
|
|
if ( cachedMarkup ) { |
|
|
return next(); |
|
|
} |
|
|
|
|
|
|
|
|
if ( ! ( context.isServerSide && isUserLoggedIn( context.store.getState() ) ) ) { |
|
|
context.layout = ( |
|
|
<LayoutComponent |
|
|
i18n={ i18n } |
|
|
store={ store } |
|
|
queryClient={ queryClient } |
|
|
currentSection={ section } |
|
|
currentRoute={ pathname } |
|
|
currentQuery={ query } |
|
|
primary={ primary } |
|
|
secondary={ secondary } |
|
|
renderHeaderSection={ renderHeaderSection } |
|
|
redirectUri={ context.originalUrl } |
|
|
showGdprBanner={ showGdprBanner } |
|
|
/> |
|
|
); |
|
|
} |
|
|
next(); |
|
|
}; |
|
|
} |
|
|
|
|
|
export function setSectionMiddleware( section ) { |
|
|
return ( context, next = noop ) => { |
|
|
|
|
|
context.section = section; |
|
|
|
|
|
|
|
|
context.store.dispatch( setSection( section ) ); |
|
|
next(); |
|
|
}; |
|
|
} |
|
|
|
|
|
export function setLocaleMiddleware( param = 'lang' ) { |
|
|
return ( context, next ) => { |
|
|
const queryLocale = context.query[ param ]; |
|
|
if ( queryLocale ) { |
|
|
context.lang = queryLocale; |
|
|
context.store.dispatch( setLocale( queryLocale ) ); |
|
|
} |
|
|
|
|
|
const paramsLocale = context.params[ param ]; |
|
|
if ( paramsLocale ) { |
|
|
context.lang = paramsLocale; |
|
|
context.store.dispatch( setLocale( paramsLocale ) ); |
|
|
} |
|
|
next(); |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function composeHandlers( ...handlers ) { |
|
|
return ( context, next ) => { |
|
|
const it = handlers.values(); |
|
|
function handleNext() { |
|
|
const nextHandler = it.next().value; |
|
|
if ( ! nextHandler ) { |
|
|
next(); |
|
|
} else { |
|
|
nextHandler( context, handleNext ); |
|
|
} |
|
|
} |
|
|
handleNext(); |
|
|
}; |
|
|
} |
|
|
|