From ebeba9f7a0a547678f214ba059d860b6c9968d74 Mon Sep 17 00:00:00 2001 From: ohakutsu Date: Wed, 12 Jul 2023 11:57:45 +0900 Subject: [PATCH 1/2] Add items show view model --- src/client/pages/items/show.tsx | 30 +++++++++--------------------- src/lib/view-models/items.ts | 13 +++++++++++++ src/server/api/items.ts | 27 ++++++++++++--------------- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/client/pages/items/show.tsx b/src/client/pages/items/show.tsx index b5c1ed7..96afdfd 100644 --- a/src/client/pages/items/show.tsx +++ b/src/client/pages/items/show.tsx @@ -2,6 +2,7 @@ import { css } from "@emotion/react"; import { useState } from "react"; import { useParams, useSearchParams } from "react-router-dom"; import { apiItemsShowPath } from "../../../lib/qiita-cli-url"; +import type { ItemsShowViewModel } from "../../../lib/view-models/items"; import { Article } from "../../components/Article"; import { ArticleInfo } from "../../components/ArticleInfo"; import { Header } from "../../components/Header"; @@ -25,20 +26,7 @@ export const ItemsShow = () => { const [searchParams] = useSearchParams(); const basename = searchParams.get("basename"); - const [item, setItem] = useState<{ - body: string; - renderedBody: string; - private: boolean; - tags: string[]; - title: string; - itemPath: string; - qiitaItemUrl: string | null; - itemsShowPath: string; - modified: boolean; - published: boolean; - organizationUrlName: string | null; - errorMessages: string[]; - } | null>(null); + const [item, setItem] = useState(null); const [error, setError] = useState(null); const [errorFrontmatterMessages, setErrorFrontmatterMessages] = useState< @@ -94,25 +82,25 @@ export const ItemsShow = () => {
diff --git a/src/lib/view-models/items.ts b/src/lib/view-models/items.ts index 13df8da..ad84eb4 100644 --- a/src/lib/view-models/items.ts +++ b/src/lib/view-models/items.ts @@ -12,3 +12,16 @@ export type ItemsIndexViewModel = { draft: ItemViewModel[]; public: ItemViewModel[]; }; + +export type ItemsShowViewModel = { + error_messages: string[]; + item_path: string; + modified: boolean; + organization_url_name: string | null; + published: boolean; + qiita_item_url: string | null; + rendered_body: string; + secret: boolean; + tags: string[]; + title: string; +}; diff --git a/src/server/api/items.ts b/src/server/api/items.ts index 4a10590..fd6b3b2 100644 --- a/src/server/api/items.ts +++ b/src/server/api/items.ts @@ -8,6 +8,7 @@ import { validateItem } from "../../lib/validators/item-validator"; import type { ItemViewModel, ItemsIndexViewModel, + ItemsShowViewModel, } from "../../lib/view-models/items"; import { Item, QiitaApi } from "../../qiita-api"; import { checkFrontmatterType } from "../../lib/check-frontmatter-type"; @@ -95,23 +96,19 @@ const itemsShow = async (req: Express.Request, res: Express.Response) => { // validate const errorMessages = validateItem(item); - res.json({ - title: item.title, - tags: item.tags, - private: item.secret, - body: item.rawBody, - organizationUrlName: item.organizationUrlName, - renderedBody, - itemPath, - qiitaItemUrl, - itemsShowPath: itemsShowPath( - itemId, - basename ? { basename: basename } : undefined - ), + const result: ItemsShowViewModel = { + error_messages: errorMessages, + item_path: itemPath, modified, + organization_url_name: item.organizationUrlName, + secret: item.secret, published, - errorMessages, - }); + qiita_item_url: qiitaItemUrl, + rendered_body: renderedBody, + tags: item.tags, + title: item.title, + }; + res.json(result); }; const itemsCreate = async (req: Express.Request, res: Express.Response) => { From f32f0b0bf854e3ee08a9f54039f0af4166530fb7 Mon Sep 17 00:00:00 2001 From: ohakutsu Date: Wed, 12 Jul 2023 12:00:33 +0900 Subject: [PATCH 2/2] Remove unused variables from src/server/api/items.ts --- .eslintrc.js | 1 - src/server/api/items.ts | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 6b0f05e..f25bb32 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -39,7 +39,6 @@ module.exports = { "src/commands/pull.ts", "src/lib/file-system-repo.test.ts", "src/lib/file-system-repo.ts", - "src/server/api/items.ts", "src/server/api/readme.ts", "src/server/app.ts", "src/server/lib/get-current-user.ts", diff --git a/src/server/api/items.ts b/src/server/api/items.ts index fd6b3b2..cd1a049 100644 --- a/src/server/api/items.ts +++ b/src/server/api/items.ts @@ -1,17 +1,15 @@ import type Express from "express"; import { Router } from "express"; -import { config } from "../../lib/config"; +import { checkFrontmatterType } from "../../lib/check-frontmatter-type"; import { getFileSystemRepo } from "../../lib/get-file-system-repo"; import { getQiitaApiInstance } from "../../lib/get-qiita-api-instance"; -import { itemsShowPath } from "../../lib/qiita-cli-url"; import { validateItem } from "../../lib/validators/item-validator"; import type { ItemViewModel, ItemsIndexViewModel, ItemsShowViewModel, } from "../../lib/view-models/items"; -import { Item, QiitaApi } from "../../qiita-api"; -import { checkFrontmatterType } from "../../lib/check-frontmatter-type"; +import { Item } from "../../qiita-api"; import { getCurrentUser } from "../lib/get-current-user"; import { itemUrl } from "../lib/qiita-url";