-
Notifications
You must be signed in to change notification settings - Fork 5
CORE-1221: Fix layoutParameters issue #2773
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -35,29 +35,26 @@ function useContextValue() { | |
| }, | ||
| [layoutParameters] | ||
| ); | ||
| const LoadableLayout = React.useMemo( | ||
| () => | ||
| loadable({ | ||
| loader: () => | ||
| loaders[ | ||
| layoutParameters.name as Exclude<LayoutName, null> | ||
| ](), | ||
| loading: LoadingPlaceholder | ||
| }), | ||
| [layoutParameters.name] | ||
| ); | ||
| const Layout = React.useCallback( | ||
| ({children}: React.PropsWithChildren<object>) => { | ||
| const layoutParameterName = layoutParameters.name; | ||
|
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 eliminates the need for the cast. In theory, layoutParameters.name could change before the function call. |
||
|
|
||
| if (layoutParameterName === null) { | ||
| return <div>{children}</div>; | ||
| } | ||
| const LoadableLayout = loadable({ | ||
|
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. Defining this separately was causing the bug that is the main thing being addressed by this PR. |
||
| loader: () => loaders[layoutParameterName](), | ||
| loading: LoadingPlaceholder | ||
| }); | ||
|
|
||
| // Avoids initial flash default -> landing | ||
| return layoutParameters.name === null ? ( | ||
| <div>{children}</div> | ||
| ) : ( | ||
| return ( | ||
| <LoadableLayout data={layoutParameters.data}> | ||
| {children} | ||
| </LoadableLayout> | ||
| ); | ||
| }, | ||
| [LoadableLayout, layoutParameters.data, layoutParameters.name] | ||
| [layoutParameters.data, layoutParameters.name] | ||
| ); | ||
|
|
||
| return {Layout, setLayoutParameters: updateIfNotEqual, layoutParameters}; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,22 +6,32 @@ import './flex-page.scss'; | |
|
|
||
| export type FlexPageData = { | ||
| meta?: {type: string}; | ||
| layout: [{type: LayoutName}]; | ||
| layout: [{type: LayoutName}?]; | ||
| body: ContentBlockConfig[]; | ||
| }; | ||
|
|
||
| export const isFlexPage = (data?: {meta?: FlexPageData['meta']}) => ( | ||
| export const isFlexPage = (data?: {meta?: FlexPageData['meta']}) => | ||
| typeof data?.meta?.type === 'string' && | ||
| ['pages.FlexPage', 'pages.RootPage'].includes(data.meta.type) | ||
| ); | ||
| ['pages.FlexPage', 'pages.RootPage'].includes(data.meta.type); | ||
|
|
||
| function FlexPageBody({data}: {data: FlexPageData}) { | ||
| return <ContentBlocks data={data.body} />; | ||
| } | ||
|
|
||
| export function LayoutUsingData({data, children}: {data: FlexPageData, children: React.ReactNode}) { | ||
| function warnAndUseDefault() { | ||
| console.warn('No layout set for page'); | ||
|
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 makes the issue more obvious. When no layout is defined for the page in the CMS, the page wouldn't render. Now it will render with the default layout, and there will also be a warning. |
||
| return 'default' as LayoutName; | ||
| } | ||
|
|
||
| export function LayoutUsingData({ | ||
| data, | ||
| children | ||
| }: { | ||
| data: FlexPageData; | ||
| children: React.ReactNode; | ||
| }) { | ||
| const {layoutParameters, setLayoutParameters} = useLayoutContext(); | ||
| const layoutName = data.layout[0]?.type; | ||
| const layoutName = data.layout[0]?.type || warnAndUseDefault(); | ||
|
|
||
| if (layoutParameters.name !== layoutName) { | ||
| setLayoutParameters({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import React from 'react'; | |
| import {render, screen} from '@testing-library/preact'; | ||
| import FeaturedResourcesSection from '~/pages/details/common/featured-resources/featured-resources'; | ||
| import ShellContextProvider from '../../../../helpers/shell-context'; | ||
| import {MemoryRouter} from 'react-router-dom'; | ||
| import MemoryRouter from '~/../../test/helpers/future-memory-router'; | ||
|
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 is just to quiet a bunch of warnings in testing. |
||
| import userEvent from '@testing-library/user-event'; | ||
| import {ResourceModel} from '~/pages/details/common/resource-box/resource-boxes'; | ||
|
|
||
|
|
||
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.
Earlier changes to the router had
/bookscoming up as a blank page when it should 404.Things like
/books/Calculuswill reroute to/details/books/Calculus.