Skip to content

Refactor items show page #26

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

Merged
merged 2 commits into from
Jul 12, 2023
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
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 9 additions & 21 deletions src/client/pages/items/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<ItemsShowViewModel | null>(null);

const [error, setError] = useState<null | string>(null);
const [errorFrontmatterMessages, setErrorFrontmatterMessages] = useState<
Expand Down Expand Up @@ -94,25 +82,25 @@ export const ItemsShow = () => {
<Header
handleMobileOpen={handleMobileOpen}
isItemPublishable={
item.modified && item.errorMessages.length === 0
item.modified && item.error_messages.length === 0
}
itemPath={item.itemPath}
itemPath={item.item_path}
id={id}
basename={basename}
/>
<div css={contentsWrapperStyle}>
<div css={contentsContainerStyle}>
<ArticleInfo
secret={item.private}
secret={item.secret}
modified={item.modified}
organizationUrlName={item.organizationUrlName}
organizationUrlName={item.organization_url_name}
published={item.published}
errorMessages={item.errorMessages}
qiitaItemUrl={item.qiitaItemUrl}
errorMessages={item.error_messages}
qiitaItemUrl={item.qiita_item_url}
/>
<div css={articleWrapStyle}>
<Article
renderedBody={item.renderedBody}
renderedBody={item.rendered_body}
tags={item.tags}
title={item.title}
/>
Expand Down
13 changes: 13 additions & 0 deletions src/lib/view-models/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
33 changes: 14 additions & 19 deletions src/server/api/items.ts
Original file line number Diff line number Diff line change
@@ -1,16 +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";

Expand Down Expand Up @@ -95,23 +94,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) => {
Expand Down