Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Jan 2, 2024
1 parent 7ea5a8d commit 6d77b11
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 97 deletions.
3 changes: 2 additions & 1 deletion src/api/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { isIgnoreRequest } from '../utils/filterRequests';
import { getRequestContext } from '../utils/requestContext';
import { generateRequestHeaderObject } from './utils/header';

// TODO:
// export async function findByPath(req: NextApiRequest, path: string): Promise<Response> {
export async function findByPath(path: string): Promise<Response> {
export async function findByPath(path: string): Promise<Response> {

if (!path || (path && isIgnoreRequest(path))) {
return new Response(null, { "status" : 404 });
Expand Down
96 changes: 0 additions & 96 deletions src/app/articles/[...slug].tsx

This file was deleted.

53 changes: 53 additions & 0 deletions src/app/articles/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ContentResponse, Content } from '../../../models/content';
import { isIgnoreRequest } from '../../../utils/filterRequests';
import { findByPath } from '../../../api/content';
import { asInsight } from '../../../utils/converters';
import Renderer from './renderer';

export default async function Page(req: any) {
const { props } = await get(req);
return <Renderer {...props} />;
}

export async function get(req: any) {
const path = "/articles/" + req.params.slug.join("/");

// avoid send request of images
// TODO
if (isIgnoreRequest(path)) {
return {
props: {
statusCode: 404
}
}
}

// const response: Response = await findByPath(ctx.req, path);
const response: Response = await findByPath(path);
// ctx.res.statusCode = response.status;

let content: Content = null;
if (response.status === 200) {
const contentResponse: ContentResponse = await response.json() as ContentResponse;
content = {
id: contentResponse.id,
title: contentResponse.title,
robotsAttributes: contentResponse.robotsAttributes,
externalResources: contentResponse.externalResources,
tags: contentResponse.tags,
description: contentResponse.description,
content: contentResponse.content,
length: contentResponse.length,
authorName: contentResponse.authorName,
publishedAt: contentResponse.publishedAt,
updatedAt: contentResponse.updatedAt
} as Content
}
return {
props: {
statusCode: response.status,
content: content,
insight: asInsight(response)
}
}
}
56 changes: 56 additions & 0 deletions src/app/articles/[...slug]/renderer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import ContentComponent from '../../../components/content';
import CoverWithNavigationComponent from '../../../components/cover/withNavigation';
import HeadMetaComponent from '../../../components/headmeta';
import MainBottomCodesComponent from '../../../components/mainBottomCodes';
import { Content } from '../../../models/content';
import { Insight } from '../../../models/insight';
import { ScriptCode } from '../../../models/script';
import { getScriptCodes } from '../../../utils/scriptTags';
import { externalResources as externalResourcesConfig } from '../../../../config';
import PlanePage from '../../../components/planePage';

const Renderer: React.FunctionComponent<{
statusCode: number,
content: Content,
insight: Insight
}> = ({ statusCode, content, insight }) => {
if (statusCode !== 200) {
return <PlanePage
title={statusCode.toString()}
content="Something went to wrong..."
/>
}

let externalResourceCodes: Array<ScriptCode> = [];
const hasExternalResources = (content.externalResources && externalResourcesConfig);
if (hasExternalResources) {
externalResourceCodes = getScriptCodes(content.externalResources, externalResourcesConfig)
}
return (
<>
<HeadMetaComponent
robotsMeta={content.robotsAttributes}
externalResources={content.externalResources}
content={content}
/>
<CoverWithNavigationComponent
contentCover={{
title: content.title,
tags: content.tags,
publishedAt: content.publishedAt,
}}
/>
<main>
<ContentComponent
content={content}
insight={insight}
/>
<MainBottomCodesComponent
scriptCodes={externalResourceCodes}
/>
</main>
</>
)
}

export default Renderer;

0 comments on commit 6d77b11

Please sign in to comment.