Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/app/components/shell/router-helpers/page-routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {assertDefined} from '~/helpers/data';
import {GeneralPageFromSlug} from '~/pages/general/general';
import {ImportedPage} from './page-loaders';
import {RouteAsPortalOrNot} from './portal-page-routes';
import Error404 from '~/pages/404/404';

type PageData = FlexPageData & {
meta: {slug: string};
Expand Down Expand Up @@ -53,13 +54,14 @@ export function DetailsRoutes() {
);
}

// eslint-disable-next-line complexity
export function OtherPageRoutes() {
const dir = assertDefined(useParams().dir);
const {'*': path} = useParams();
const {layoutParameters, setLayoutParameters} = useLayoutContext();

if (['books', 'textbooks'].includes(dir)) {
return (
return path === '' ? <Error404 /> : (
Copy link
Contributor Author

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 /books coming up as a blank page when it should 404.
Things like /books/Calculus will reroute to /details/books/Calculus.

<Routes>
<Route
path="/:title"
Expand Down
27 changes: 12 additions & 15 deletions src/app/contexts/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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({
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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};
Expand Down
22 changes: 16 additions & 6 deletions src/app/pages/flex-page/flex-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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({
Expand Down
8 changes: 8 additions & 0 deletions test/src/components/shell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ describe('shell', () => {

await waitFor(() => expect(setPortal).toHaveBeenCalledWith('landing-page'));
});
it('routes books routes to portal/books', async () => {
setPortalPrefix('portal');
BrowserRouter.mockImplementationOnce(({children}) => (
<MR initialEntries={['/books/slug']} >{children}</MR>
));
render(AppElement);
// Nothing can really be checked; code coverage in rex-portal
});
it('renders nothing when data is null', async () => {
setPortalPrefix('/landing-page');

Expand Down
2 changes: 1 addition & 1 deletion test/src/pages/details/common/featured-resources.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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';

Expand Down
22 changes: 21 additions & 1 deletion test/src/pages/flex-page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {render, screen} from '@testing-library/preact';
import {describe, it} from '@jest/globals';
import userEvent from '@testing-library/user-event';
import MemoryRouter from '~/../../test/helpers/future-memory-router';
import FlexPage from '~/pages/flex-page/flex-page';
import FlexPage, {LayoutUsingData} from '~/pages/flex-page/flex-page';
import {CTALinkFields} from '~/pages/flex-page/blocks/CTABlock';
import {ContentBlockConfig} from '~/pages/flex-page/blocks/ContentBlock';
import {HeroBlockConfig} from '~/pages/flex-page/blocks/HeroBlock';
import { LayoutContextProvider } from '~/contexts/layout';

const emptyTarget = {
type: '',
Expand Down Expand Up @@ -66,6 +67,25 @@ describe('flex-page', () => {
render(<Component />);
expect(screen.getAllByRole('img')).toHaveLength(1);
});
it('warns and renders with default when layout is not provided', () => {
const saveWarn = console.warn;

console.warn = jest.fn();
body = [heroBlock()];
render(
<ShellContextProvider>
<MemoryRouter initialEntries={['']}>
<LayoutContextProvider>
<LayoutUsingData data={{body, layout: []}} >
content
</LayoutUsingData>
</LayoutContextProvider>
</MemoryRouter>
</ShellContextProvider>
);
expect(console.warn).toHaveBeenCalledWith('No layout set for page');
console.warn = saveWarn;
});
it('renders heroBlock with top image alignment', () => {
const modBlock = heroBlock();

Expand Down