-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Persist header and footer across ImportedPage content changes * Check for DefaultLayout before applying wrapper * Pull Layout to top level * Extract setLayoutParameters Set layout for general pages Avoid re-setting data to equal value * Optimize data reset * Use a layout context Further enapsulate LayoutContext; test coverage Ensure that normal pages get default layout Move layout up to the Routes level * Code coverage for HeroBlock * Code review * Use reducer for layoutParameters; derive layoutData * More code review items * CR again * Load null until layout name is right,.
- Loading branch information
1 parent
e2ff9f8
commit 8be052d
Showing
7 changed files
with
268 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react'; | ||
import buildContext from '~/components/jsx-helpers/build-context'; | ||
import loadable from 'react-loadable'; | ||
import LoadingPlaceholder from '~/components/loading-placeholder/loading-placeholder'; | ||
import deepEqual from 'deep-equal'; | ||
|
||
// Webpack wasn't able to make the dynamic strings work in the Context value. | ||
type LayoutName = 'default' | 'landing'; | ||
const loaders = { | ||
default: () => import('~/layouts/default/default'), | ||
landing: () => import('~/layouts/landing/landing') | ||
}; | ||
|
||
type LayoutParameters = { | ||
name: LayoutName; | ||
data?: any; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
} | ||
const defaultLayoutParameters: LayoutParameters = {name: 'default'}; | ||
|
||
function useContextValue() { | ||
const [layoutParameters, setLayoutParameters] = React.useReducer( | ||
(state: LayoutParameters, newState: LayoutParameters) => { | ||
if (newState === undefined) { | ||
return defaultLayoutParameters; | ||
} | ||
if (deepEqual(state, newState)) { | ||
return state; | ||
} | ||
return newState; | ||
}, | ||
defaultLayoutParameters | ||
); | ||
const LoadableLayout = React.useMemo( | ||
() => | ||
loadable({ | ||
loader: () => loaders[layoutParameters.name](), | ||
loading: LoadingPlaceholder | ||
}), | ||
[layoutParameters.name] | ||
); | ||
const Layout = React.useCallback( | ||
({children}: React.PropsWithChildren<object>) => ( | ||
<LoadableLayout data={layoutParameters.data}>{children}</LoadableLayout> | ||
), | ||
[LoadableLayout, layoutParameters.data] | ||
); | ||
|
||
return {Layout, setLayoutParameters, layoutParameters}; | ||
} | ||
|
||
const {useContext, ContextProvider} = buildContext({useContextValue}); | ||
|
||
export {useContext as default, ContextProvider as LayoutContextProvider}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
import {render, screen} from '@testing-library/preact'; | ||
import useLayoutContext, { LayoutContextProvider } from '~/contexts/layout'; | ||
|
||
function Component() { | ||
const {Layout, setLayoutParameters} = useLayoutContext(); | ||
|
||
React.useEffect( | ||
() => setLayoutParameters({name: 'landing', data: {title: 'title', layout: [ | ||
{ | ||
value: { | ||
navLinks: [{ | ||
text: 'Landing page link', | ||
target: { | ||
type: 'href', | ||
value: 'whatever' | ||
} | ||
}] | ||
} | ||
} | ||
]}}), | ||
[setLayoutParameters] | ||
); | ||
|
||
return ( | ||
<Layout> | ||
<div>No menus</div> | ||
</Layout> | ||
); | ||
} | ||
|
||
describe('layout-context', () => { | ||
it('renders a landing page', async () => { | ||
render( | ||
<LayoutContextProvider> | ||
<Component /> | ||
</LayoutContextProvider> | ||
); | ||
await screen.findByText('Landing page link'); | ||
}); | ||
}); |
Oops, something went wrong.