Skip to content

Commit 67e181e

Browse files
authored
Merge pull request #26 from increments/refactor-items-show-page
Refactor items show page
2 parents 1d9f7ab + f32f0b0 commit 67e181e

File tree

4 files changed

+36
-41
lines changed

4 files changed

+36
-41
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ module.exports = {
3939
"src/commands/pull.ts",
4040
"src/lib/file-system-repo.test.ts",
4141
"src/lib/file-system-repo.ts",
42-
"src/server/api/items.ts",
4342
"src/server/api/readme.ts",
4443
"src/server/app.ts",
4544
"src/server/lib/get-current-user.ts",

src/client/pages/items/show.tsx

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { css } from "@emotion/react";
22
import { useState } from "react";
33
import { useParams, useSearchParams } from "react-router-dom";
44
import { apiItemsShowPath } from "../../../lib/qiita-cli-url";
5+
import type { ItemsShowViewModel } from "../../../lib/view-models/items";
56
import { Article } from "../../components/Article";
67
import { ArticleInfo } from "../../components/ArticleInfo";
78
import { Header } from "../../components/Header";
@@ -25,20 +26,7 @@ export const ItemsShow = () => {
2526
const [searchParams] = useSearchParams();
2627
const basename = searchParams.get("basename");
2728

28-
const [item, setItem] = useState<{
29-
body: string;
30-
renderedBody: string;
31-
private: boolean;
32-
tags: string[];
33-
title: string;
34-
itemPath: string;
35-
qiitaItemUrl: string | null;
36-
itemsShowPath: string;
37-
modified: boolean;
38-
published: boolean;
39-
organizationUrlName: string | null;
40-
errorMessages: string[];
41-
} | null>(null);
29+
const [item, setItem] = useState<ItemsShowViewModel | null>(null);
4230

4331
const [error, setError] = useState<null | string>(null);
4432
const [errorFrontmatterMessages, setErrorFrontmatterMessages] = useState<
@@ -94,25 +82,25 @@ export const ItemsShow = () => {
9482
<Header
9583
handleMobileOpen={handleMobileOpen}
9684
isItemPublishable={
97-
item.modified && item.errorMessages.length === 0
85+
item.modified && item.error_messages.length === 0
9886
}
99-
itemPath={item.itemPath}
87+
itemPath={item.item_path}
10088
id={id}
10189
basename={basename}
10290
/>
10391
<div css={contentsWrapperStyle}>
10492
<div css={contentsContainerStyle}>
10593
<ArticleInfo
106-
secret={item.private}
94+
secret={item.secret}
10795
modified={item.modified}
108-
organizationUrlName={item.organizationUrlName}
96+
organizationUrlName={item.organization_url_name}
10997
published={item.published}
110-
errorMessages={item.errorMessages}
111-
qiitaItemUrl={item.qiitaItemUrl}
98+
errorMessages={item.error_messages}
99+
qiitaItemUrl={item.qiita_item_url}
112100
/>
113101
<div css={articleWrapStyle}>
114102
<Article
115-
renderedBody={item.renderedBody}
103+
renderedBody={item.rendered_body}
116104
tags={item.tags}
117105
title={item.title}
118106
/>

src/lib/view-models/items.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,16 @@ export type ItemsIndexViewModel = {
1212
draft: ItemViewModel[];
1313
public: ItemViewModel[];
1414
};
15+
16+
export type ItemsShowViewModel = {
17+
error_messages: string[];
18+
item_path: string;
19+
modified: boolean;
20+
organization_url_name: string | null;
21+
published: boolean;
22+
qiita_item_url: string | null;
23+
rendered_body: string;
24+
secret: boolean;
25+
tags: string[];
26+
title: string;
27+
};

src/server/api/items.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import type Express from "express";
22
import { Router } from "express";
3-
import { config } from "../../lib/config";
3+
import { checkFrontmatterType } from "../../lib/check-frontmatter-type";
44
import { getFileSystemRepo } from "../../lib/get-file-system-repo";
55
import { getQiitaApiInstance } from "../../lib/get-qiita-api-instance";
6-
import { itemsShowPath } from "../../lib/qiita-cli-url";
76
import { validateItem } from "../../lib/validators/item-validator";
87
import type {
98
ItemViewModel,
109
ItemsIndexViewModel,
10+
ItemsShowViewModel,
1111
} from "../../lib/view-models/items";
12-
import { Item, QiitaApi } from "../../qiita-api";
13-
import { checkFrontmatterType } from "../../lib/check-frontmatter-type";
12+
import { Item } from "../../qiita-api";
1413
import { getCurrentUser } from "../lib/get-current-user";
1514
import { itemUrl } from "../lib/qiita-url";
1615

@@ -95,23 +94,19 @@ const itemsShow = async (req: Express.Request, res: Express.Response) => {
9594
// validate
9695
const errorMessages = validateItem(item);
9796

98-
res.json({
99-
title: item.title,
100-
tags: item.tags,
101-
private: item.secret,
102-
body: item.rawBody,
103-
organizationUrlName: item.organizationUrlName,
104-
renderedBody,
105-
itemPath,
106-
qiitaItemUrl,
107-
itemsShowPath: itemsShowPath(
108-
itemId,
109-
basename ? { basename: basename } : undefined
110-
),
97+
const result: ItemsShowViewModel = {
98+
error_messages: errorMessages,
99+
item_path: itemPath,
111100
modified,
101+
organization_url_name: item.organizationUrlName,
102+
secret: item.secret,
112103
published,
113-
errorMessages,
114-
});
104+
qiita_item_url: qiitaItemUrl,
105+
rendered_body: renderedBody,
106+
tags: item.tags,
107+
title: item.title,
108+
};
109+
res.json(result);
115110
};
116111

117112
const itemsCreate = async (req: Express.Request, res: Express.Response) => {

0 commit comments

Comments
 (0)