fix: Dedupe layout selection logic

This commit is contained in:
Bouncey
2019-02-28 15:44:35 +00:00
committed by mrugesh
parent 7a88eadf7d
commit 15f18d1cda
3 changed files with 52 additions and 37 deletions

View File

@ -4,13 +4,7 @@ import { Provider } from 'react-redux';
import { createStore } from './src/redux/createStore'; import { createStore } from './src/redux/createStore';
import AppMountNotifier from './src/components/AppMountNotifier'; import AppMountNotifier from './src/components/AppMountNotifier';
import layoutSelector from './utils/gatsby/layoutSelector';
import {
CertificationLayout,
DefaultLayout,
GuideLayout
} from './src/components/layouts';
import GuideNavMenu from './src/components/layouts/components/guide/NavMenu';
const store = createStore(); const store = createStore();
@ -26,33 +20,6 @@ wrapRootElement.propTypes = {
element: PropTypes.any element: PropTypes.any
}; };
export const wrapPageElement = ({ element, props }) => { export const wrapPageElement = layoutSelector;
const {
location: { pathname }
} = props;
if (pathname === '/') {
return <DefaultLayout landingPage={true}>{element}</DefaultLayout>;
}
if (/^\/certification(\/.*)*/.test(pathname)) {
return <CertificationLayout>{element}</CertificationLayout>;
}
if (/^\/guide(\/.*)*/.test(pathname)) {
return (
<DefaultLayout navigationMenu={<GuideNavMenu />}>
<GuideLayout>{element}</GuideLayout>
</DefaultLayout>
);
}
if (/^\/learn(\/.*)*/.test(pathname)) {
return <DefaultLayout showFooter={false}>{element}</DefaultLayout>;
}
return <DefaultLayout>{element}</DefaultLayout>;
};
wrapPageElement.propTypes = {
element: PropTypes.any,
location: PropTypes.objectOf({ pathname: PropTypes.string }),
props: PropTypes.any
};
export const disableCorePrefetching = () => true; export const disableCorePrefetching = () => true;

View File

@ -5,9 +5,8 @@ import { Provider } from 'react-redux';
import headComponents from './src/head'; import headComponents from './src/head';
import { createStore } from './src/redux/createStore'; import { createStore } from './src/redux/createStore';
import { wrapPageElement } from './gatsby-browser';
export { wrapPageElement }; import layoutSelector from './utils/gatsby/layoutSelector';
const store = createStore(); const store = createStore();
@ -19,6 +18,8 @@ wrapRootElement.propTypes = {
element: PropTypes.any element: PropTypes.any
}; };
export const wrapPageElement = layoutSelector;
export const onRenderBody = ({ setHeadComponents, setPostBodyComponents }) => { export const onRenderBody = ({ setHeadComponents, setPostBodyComponents }) => {
setHeadComponents([...headComponents]); setHeadComponents([...headComponents]);
setPostBodyComponents( setPostBodyComponents(

View File

@ -0,0 +1,47 @@
import React from 'react';
import PropTypes from 'prop-types';
import {
CertificationLayout,
DefaultLayout,
GuideLayout
} from '../../src/components/layouts';
// eslint-disable-next-line max-len
import GuideNavMenu from '../../src/components/layouts/components/guide/NavMenu';
export default function layoutSelector({ element, props }) {
const {
location: { pathname }
} = props;
if (pathname === '/') {
return (
<DefaultLayout landingPage={true} pathname={pathname}>
{element}
</DefaultLayout>
);
}
if (/^\/certification(\/.*)*/.test(pathname)) {
return <CertificationLayout>{element}</CertificationLayout>;
}
if (/^\/guide(\/.*)*/.test(pathname)) {
return (
<DefaultLayout navigationMenu={<GuideNavMenu />} pathname={pathname}>
<GuideLayout>{element}</GuideLayout>
</DefaultLayout>
);
}
if (/^\/learn(\/.*)*/.test(pathname)) {
return (
<DefaultLayout pathname={pathname} showFooter={false}>
{element}
</DefaultLayout>
);
}
return <DefaultLayout pathname={pathname}>{element}</DefaultLayout>;
}
layoutSelector.propTypes = {
element: PropTypes.any,
location: PropTypes.objectOf({ pathname: PropTypes.string }),
props: PropTypes.any
};