Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Jan 3, 2024
1 parent 8a7a0f0 commit 5a2ca00
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 159 deletions.
38 changes: 19 additions & 19 deletions src/app/articles/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@

import { headers } from 'next/headers'

// TODO: refactor
import { getArticles } from '../../api/articles';
import { Article, ArticleResponseWithCount } from '../../models/article';
import { getRequestContext } from '../../utils/requestContext';
import { Renderer } from './renderer';
import { runOrHandleErrorIf, throwIfError } from "../handler";

export default async function Page() {
const { props } = await get();
export default async function Page(req: any) {
return runOrHandleErrorIf(await run(req));
}

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

async function get() {
async function get(req: any) {
const response: Response = await getArticles(1, 10, getRequestContext(headers()));
throwIfError(response);

let articlesResponseWithCount: ArticleResponseWithCount = null;
let articles: Array<Article> = [];
if (response.status === 200) {
articlesResponseWithCount = await response.json() as ArticleResponseWithCount;
articles = articlesResponseWithCount.articles.map(article => {
return {
path: article.path,
title: article.title,
content: `${article.content} ...`,
publishedAt: article.publishedAt,
updatedAt: article.updatedAt
} as Article
});
}
const articlesResponseWithCount: ArticleResponseWithCount = await response.json() as ArticleResponseWithCount;
const articles: Array<Article> = articlesResponseWithCount.articles.map(article => {
return {
path: article.path,
title: article.title,
content: `${article.content} ...`,
publishedAt: article.publishedAt,
updatedAt: article.updatedAt
} as Article
});

return {
props: {
statusCode: response.status,
count: articlesResponseWithCount.count,
articles: articles
}
Expand Down
10 changes: 1 addition & 9 deletions src/app/articles/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@ import CoverWithNavigationComponent from '../../components/cover/withNavigation'
import ArticlesComponent from '../../components/articles';
import PaginationComponent from '../../components/pagination';
import { defaultRobotsMeta } from '../../../config';
import PlanePage from '../../components/planePage';

export const Renderer: React.FunctionComponent<{
statusCode,
count,
articles
}> = ({ statusCode, count, articles }) => {
if (statusCode !== 200) {
return <PlanePage
title={statusCode.toString()}
content="Something went to wrong..."
/>
}
}> = ({ count, articles }) => {
return (
<>
<HeadMetaComponent
Expand Down
38 changes: 19 additions & 19 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@ import { getArticles } from '../api/articles';
import { Article, ArticleResponseWithCount } from '../models/article';
import { getRequestContext } from '../utils/requestContext';
import { Renderer } from './renderer';
import { runOrHandleErrorIf, throwIfError } from "./handler";

export default async function Page() {
const { props } = await get();
export default async function Page(req: any) {
return runOrHandleErrorIf(await run(req));
}

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

// export async function get(ctx: any) {
async function get() {
async function get(req: any) {
const response: Response = await getArticles(1, 5, getRequestContext(headers()));
// ctx.res.statusCode = response.status;
throwIfError(response);

let articles: Array<Article> = [];
if (response.status === 200) {
let articlesResponseWithCount: ArticleResponseWithCount = await response.json() as ArticleResponseWithCount;
articles = articlesResponseWithCount.articles.map(article => {
return {
path: article.path,
title: article.title,
content: `${article.content} ...`,
publishedAt: article.publishedAt,
updatedAt: article.updatedAt
} as Article
});
}
const articlesResponseWithCount: ArticleResponseWithCount = await response.json() as ArticleResponseWithCount;
const articles: Array<Article> = articlesResponseWithCount.articles.map(article => {
return {
path: article.path,
title: article.title,
content: `${article.content} ...`,
publishedAt: article.publishedAt,
updatedAt: article.updatedAt
} as Article
});

return {
props: {
statusCode: response.status,
articles: articles
}
}
Expand Down
10 changes: 1 addition & 9 deletions src/app/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@ import RecentArticlesComponent from '../components/recentArticles';
import styles from '../styles/home.module.scss';
import containerStyles from '../styles/components/container.module.scss';
import { defaultRobotsMeta } from '../../config';
import PlanePage from '../components/planePage';

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

return (
<>
Expand Down
2 changes: 0 additions & 2 deletions src/app/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ async function get(ctx: any) {
async function execute(ctx, words: Array<string>) {
console.log(words);
const response = await search(getRequestContext(ctx.req), words);
console.log(response);
// ctx.res.statusCode = response.status;
if (response.status !== 200) {
return emptyResult;
}
Expand Down
3 changes: 1 addition & 2 deletions src/app/search/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import containerStyles from '../../styles/components/container.module.scss';
import SearchResultComponent from '../../components/searchResult';

export const Renderer: React.FunctionComponent<{
statusCode: number,
hits: number,
count:number,
contents: Array<SearchResponse>,
queryStrings: Array<string>
}> = ({ statusCode, hits, count, contents, queryStrings }) => {
}> = ({ hits, count, contents, queryStrings }) => {
let contentsWithCount: SearchResponseWithCount = {
count: 0,
contents: []
Expand Down
46 changes: 23 additions & 23 deletions src/app/series/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ import { SeriresWithArticlesResponse, SeriresWithArticles } from '../../../model
import { Article } from '../../../models/article';
import { getRequestContext } from '../../../utils/requestContext';
import { Renderer } from './renderer';
import { runOrHandleErrorIf, throwIfError } from "../../handler";

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

async function run(req: any): Promise<any> {
const { props } = await get(req);
return <Renderer {...props} />;
}
async function get(ctx: any) {
const seriesName = ctx.params.slug;
const response: Response = await getSeriesBySeriesName(seriesName, getRequestContext(ctx));
// ctx.res.statusCode = response.status;
throwIfError(response);

let seriresWithArticles: SeriresWithArticles = null;
if (response.status === 200) {
let seriresWithArticlesResponse: SeriresWithArticlesResponse = await response.json() as SeriresWithArticlesResponse;
seriresWithArticles = {
id: seriresWithArticlesResponse.id,
name: seriresWithArticlesResponse.name,
title: seriresWithArticlesResponse.title,
description: seriresWithArticlesResponse.description,
articles: seriresWithArticlesResponse.articles.map(article => {
return {
path: article.path,
title: article.title,
content: `${article.content} ...`,
publishedAt: article.publishedAt,
updatedAt: article.updatedAt
} as Article
})
};
const seriresWithArticlesResponse: SeriresWithArticlesResponse = await response.json() as SeriresWithArticlesResponse;
const seriresWithArticles: SeriresWithArticles = {
id: seriresWithArticlesResponse.id,
name: seriresWithArticlesResponse.name,
title: seriresWithArticlesResponse.title,
description: seriresWithArticlesResponse.description,
articles: seriresWithArticlesResponse.articles.map(article => {
return {
path: article.path,
title: article.title,
content: `${article.content} ...`,
publishedAt: article.publishedAt,
updatedAt: article.updatedAt
} as Article
})
}

return {
props: {
statusCode: response.status,
seriresWithArticles: seriresWithArticles
}
}
Expand Down
11 changes: 1 addition & 10 deletions src/app/series/[slug]/renderer.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
// TODO: refactor
import HeadMetaComponent from '../../../components/headmeta';
import CoverWithNavigationComponent from '../../../components/cover/withNavigation';
import SeriesWithArticlesComponent from '../../../components/seriesWithArticles';
import { defaultRobotsMeta } from '../../../../config';
import PlanePage from '../../../components/planePage';

export const Renderer: React.FunctionComponent<{
statusCode,
seriresWithArticles
}> = ({ statusCode, seriresWithArticles }) => {
if (statusCode !== 200) {
return <PlanePage
title={statusCode.toString()}
content="Something went to wrong..."
/>
}
}> = ({ seriresWithArticles }) => {
return (
<>
<HeadMetaComponent
Expand Down
30 changes: 16 additions & 14 deletions src/app/series/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,34 @@ import { getSeries } from '../../api/series';
import { Series, SeriesResponse } from '../../models/series';
import { getRequestContext } from '../../utils/requestContext';
import { Renderer } from './renderer';
import { runOrHandleErrorIf, throwIfError } from "../handler";

export default async function Page(req: any) {
return runOrHandleErrorIf(await run(req));
}

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


async function get(req: any) {
const response: Response = await getSeries(getRequestContext(req))
// ctx.res.statusCode = response.status;
throwIfError(response);

let series: Array<Series> = [];
if (response.status === 200) {
let seriesResponse: Array<SeriesResponse> = await response.json() as Array<SeriesResponse>;
series = seriesResponse.map(series => {
return {
id: series.id,
name: series.name,
title: series.title,
description: series.description
} as Series
});
}
const seriesResponse: Array<SeriesResponse> = await response.json() as Array<SeriesResponse>;
const series: Array<Series> = seriesResponse.map(series => {
return {
id: series.id,
name: series.name,
title: series.title,
description: series.description
} as Series
});

return {
props: {
statusCode: response.status,
series: series
}
}
Expand Down
11 changes: 1 addition & 10 deletions src/app/series/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@ import CoverWithNavigationComponent from '../../components/cover/withNavigation'
import SeriesComponent from '../../components/series';
import { Series } from '../../models/series';
import { defaultRobotsMeta } from '../../../config';
import PlanePage from '../../components/planePage';

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

}> = ({ series }) => {
return (
<>
<HeadMetaComponent
Expand Down
4 changes: 0 additions & 4 deletions src/app/sitemap.xml/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export async function GET(res: NextApiResponse) {
const response: Response = await getSitemap(getRequestContext(headers()));

if (response.status !== 200) {
/* NOTE:
res.statusCode = response.status;
res.send;
*/
return new Response('', {
status: 404 ,
headers: {
Expand Down
27 changes: 8 additions & 19 deletions src/app/tags/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
'use server';

import { notFound } from 'next/navigation';

import { getArticlesByTagName } from '../../../api/articles';
import { Article, ArticleResponseWithCount } from '../../../models/article';
import { getRequestContext } from '../../../utils/requestContext';
import { Renderer } from './renderer';
import { runOrHandleErrorIf, throwIfError } from "../../handler";

export default async function Page(req: any) {
try {
const { props } = await get(req);
return <Renderer {...props} />;
} catch(e) {
// @ts-ignore
if (e.cause === 404) {
return notFound();
}
// FIXME: I don't want to re-throw
// @ts-ignore
throw new Error(response.statusText, { cause: response.status });
}
return runOrHandleErrorIf(await run(req));
}

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

async function get(ctx: any) {
const tagName = decodeURI(ctx.params.slug[0]);
const currentPage = ctx.params.slug[1] ? ctx.params.slug[1] : 1;
const response: Response = await getArticlesByTagName(tagName, currentPage, 10, getRequestContext(ctx));

if (response.status !== 200) {
// TODO: use custom Error class
throw new Error(response.statusText, { cause: response.status });
}
throwIfError(response);

const articlesResponseWithCount: ArticleResponseWithCount = await response.json();
const articles: Array<Article> = articlesResponseWithCount.articles.map(article => {
Expand Down
Loading

0 comments on commit 5a2ca00

Please sign in to comment.