Skip to content
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

Private content pages #310

Merged
merged 2 commits into from
Jan 9, 2024
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
100 changes: 79 additions & 21 deletions components/navigation/NavigationContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,45 @@ function NavigationContent({
key={contentPageGroup.uid}
header={contentPageGroup.title}
>
{contentPageGroup.content_pages.map(({ title }) => (
<MobileNavigationItem
key={title}
onClick={() => router.push(`/foundation/${title}`)}
active={title === activePage}
>
{title.split('-').join(' ')}
</MobileNavigationItem>
))}
{contentPageGroup.content_pages.map(({ title, is_private }) => {
const shouldRenderAsLocked = is_private && !session;
return (
<MobileNavigationItem
key={title}
onClick={() =>
router.push(
`/${contentPageGroup.title}/${
is_private ? 'private/' : ''
}${title}`,
)
}
active={title === activePage}
>
<div>
<div style={{ display: 'flex', alignItems: 'center' }}>
{title.split('-').join(' ')}
{shouldRenderAsLocked && (
<LockIconContainer>
<Icon glyph="Lock" />
</LockIconContainer>
)}
</div>
<div style={{ display: 'block' }}>
{shouldRenderAsLocked && (
<Description
style={{
textTransform: 'none',
color: palette.gray.base,
}}
>
Log in to view this page
</Description>
)}
</div>
</div>
</MobileNavigationItem>
);
})}
</MobileNavigationGroup>
))}
<MobileNavigationGroup
Expand Down Expand Up @@ -111,19 +141,47 @@ function NavigationContent({
{contentPageGroup.content_pages &&
contentPageGroup.content_pages.map(contentPage => {
const contentPageKebabCaseName = kebabCase(contentPage.title);
const shouldRenderAsLocked =
contentPage.is_private && !session;

return (
<SideNavItem
key={contentPage.title}
as={NextLinkWrapper}
href={`/${kebabCase(
contentPageGroup.title,
)}/${contentPageKebabCaseName}`}
active={contentPageKebabCaseName === activePage}
>
{contentPage.title}
</SideNavItem>
);
if (shouldRenderAsLocked) {
return (
<Tooltip
key={`${contentPageKebabCaseName}-page-tooltip`}
align="right"
trigger={
<SideNavItem
key={contentPageKebabCaseName}
as={NextLinkWrapper}
active={contentPageKebabCaseName === activePage}
href={`/${kebabCase(contentPageGroup.title)}/${
contentPage.is_private ? 'private/' : ''
}${contentPageKebabCaseName}`}
>
{contentPage.title}
<LockIconContainer>
<Icon glyph="Lock" />
</LockIconContainer>
</SideNavItem>
}
>
Log in to view this page
</Tooltip>
);
} else {
return (
<SideNavItem
key={contentPageKebabCaseName}
href={`/${kebabCase(contentPageGroup.title)}/${
contentPage.is_private ? 'private/' : ''
}${contentPageKebabCaseName}`}
as={NextLinkWrapper}
active={contentPageKebabCaseName === activePage}
>
{contentPage.title}
</SideNavItem>
);
}
})}
</SideNavGroup>
))}
Expand Down
41 changes: 41 additions & 0 deletions pages/[contentPageGroup]/private/[contentPageTitle].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ReactElement } from 'react';
import ContentPageLayout from 'layouts/ContentPageLayout';
import startCase from 'lodash/startCase';
import { unstable_getServerSession } from 'next-auth';
import { authOptions } from 'pages/api/auth/[...nextauth]';
import { getContentPage } from 'utils/ContentStack/getContentstackResources';

import ContentstackRichText from 'components/ContentstackRichText';
import Unauthorized from 'components/Unauthorized';

const ContentPage = ({ contentPage }) => {
if (contentPage !== null)
return <ContentstackRichText content={contentPage?.content} />;

return <Unauthorized />;
};

ContentPage.getLayout = function getLayout(page: ReactElement) {
return (
<ContentPageLayout
contentPageTitle={page.props.contentPageTitle ?? 'Protected Page'}
>
{page}
</ContentPageLayout>
);
};

export async function getServerSideProps({ params, req, res }) {
const session = await unstable_getServerSession(req, res, authOptions);
const contentPage = session
? await getContentPage(startCase(params.contentPageTitle))
: null;
return {
props: {
contentPage,
contentPageTitle: contentPage ? contentPage.title : null,
},
};
}

export default ContentPage;
19 changes: 16 additions & 3 deletions utils/ContentStack/getContentstackResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,16 @@ export async function getContentPageGroups(): Promise<Array<ContentPageGroup>> {
const pageGroups: Array<ContentPageGroup> = (
await query
.includeReference('content_pages')
.only(['content_pages', 'uid', 'title', 'url', 'iconname'])
.only([
'content_pages',
'uid',
'title',
'url',
'iconname',
'private',
'rendering_order',
])
.ascending('rendering_order')
.toJSON()
.find()
)[0].map(({ content_pages, ...meta }: ContentPageGroup) => {
Expand All @@ -116,11 +125,15 @@ export async function getContentPageGroups(): Promise<Array<ContentPageGroup>> {
content_pages: content_pages
// TODO: strip fields in initial query
// Strip any additional fields
.map(({ uid, title, url }) => ({ uid, title, url }))
.map(({ uid, title, url, is_private }: ContentPage) => ({
uid,
title,
url,
is_private,
}))
.sort((a, b) => a.title.localeCompare(b.title)),
};
});

return pageGroups;
} catch (error) {
console.error('No Content Page Groups found', error);
Expand Down
3 changes: 2 additions & 1 deletion utils/ContentStack/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface ContentPageGroup extends Object {
title: string;
url: string;
iconname: string;
content_pages: Array<ContentPageMeta>;
content_pages: Array<ContentPage>;
}

/**
Expand All @@ -20,6 +20,7 @@ export interface ContentPageMeta {
*/
export interface ContentPage extends ContentPageMeta {
content: unknown;
is_private?: boolean;
}

/**
Expand Down
Loading