-
Notifications
You must be signed in to change notification settings - Fork 0
Faeture/article update #39
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
69297d7
chore: 디렉토리 구조에 맞게 코멘트 파일 이동
4f9f572
feat: 수정 삭제 버튼 컴포넌트를 client 컴포넌트로 선언
8be1e3b
chore: 업데이트 화면 구성을 위해 create-post 페이지 복붙
2e69602
fix: 수정 버튼에 적절한 route 함수 연결
2ff56bb
feat: 수정 버튼에 API 연결
4808f03
chore: 린트 에러 수정 (추후에 설정 확인할 것)
17e84c1
fix: 누락된 optional interface 수정
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
gitmon-client/src/app/[id]/[repo]/[slug]/update/UpdatePost.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| 'use client' | ||
|
|
||
| import { useState } from 'react' | ||
|
|
||
| import { useMutation } from '@tanstack/react-query' | ||
| import { useParams, useRouter } from 'next/navigation' | ||
| import { toast } from 'sonner' | ||
|
|
||
| import { PostForm } from '@components/Post' | ||
| import matter from 'gray-matter' | ||
| import { titleToSlug } from '@lib/utils' | ||
| import { Post, PostMeta } from '@lib/types' | ||
|
|
||
| interface UpdatePostProps { | ||
| token: string | null | ||
| post: Omit<Post, 'id'> | ||
| } | ||
|
|
||
| export default function UpdatePost({ token, post }: UpdatePostProps) { | ||
| const router = useRouter() | ||
| const { slug } = useParams() | ||
| const [user, setUser] = useState<{ id: string; repo: string }>() | ||
|
|
||
| console.log(slug) | ||
|
|
||
| const { mutate } = useMutation({ | ||
| // 해당 부분을 업데이트치는 API 호출로 변경해야 함 | ||
| mutationFn: async ({ title, blob }: { title: string; blob: Blob }) => { | ||
| const body = new FormData() | ||
| const fileName = titleToSlug(title) | ||
| body.append('title', fileName) | ||
| body.append('content', blob, fileName) | ||
| body.append('id', slug as string) | ||
|
|
||
| const response = await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/api/v1/posting`, { | ||
| method: 'PUT', | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| }, | ||
| body, | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| toast('게시글 저장에 실패했습니다.') | ||
| throw new Error('게시글 저장 실패') | ||
| } | ||
| return response | ||
| }, | ||
| onSuccess: async (res, {}) => { | ||
| const { data } = await res.json() | ||
|
|
||
| toast('게시글이 저장되었습니다.') | ||
| router.push(`/${user?.id}/${user?.repo}/${data.id}`) | ||
| }, | ||
| }) | ||
|
|
||
| const handleSavePost = async ({ title, content }: { title: string; content: string }) => { | ||
| const res = await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/api/v1/member`, { | ||
| method: 'GET', | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }) | ||
|
|
||
| if (!res.ok) { | ||
| toast('사용자 정보를 가져오는 데 실패했습니다.') | ||
| return | ||
| } | ||
|
|
||
| const { data } = await res.json() | ||
| setUser({ id: data.githubUsername, repo: data.repoName }) | ||
|
|
||
| if (!data.githubUsername || !data.repoName) { | ||
| toast('레포지토리 정보가 없습니다. 먼저 레포지토리를 설정해주세요.') | ||
| return | ||
| } | ||
|
|
||
| if (!title) { | ||
| toast('게시글 제목을 입력해주세요.') | ||
| return | ||
| } | ||
|
|
||
| const metadata: PostMeta = { | ||
| title: title.trim(), | ||
| slug: titleToSlug(title), | ||
| repo: data.repoName, | ||
| excerpt: content.slice(0, 100), | ||
| coverImage: '', | ||
| createdAt: new Date().toISOString(), | ||
| updatedAt: new Date().toISOString(), | ||
| pinned: false, | ||
| tags: [], | ||
| author: data.githubUsername, | ||
| } | ||
|
|
||
| const markdown = matter.stringify(content, metadata) | ||
| const blob = new Blob([markdown], { type: 'text/markdown' }) | ||
|
|
||
| mutate({ title, blob }) | ||
| } | ||
|
|
||
| return ( | ||
| <div className="p-4"> | ||
| <PostForm onPostSaved={handleSavePost} post={post} /> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import Link from 'next/link' | ||
| import { cookies } from 'next/headers' | ||
|
|
||
| import UpdatePost from './UpdatePost' | ||
| import { replaceId } from '@lib/utils' | ||
| import { fetchPost } from '@lib/github' | ||
|
|
||
| export default async function Page({ | ||
| params, | ||
| }: { | ||
| params: Promise<{ slug: string; id: string; repo: string }> | ||
| }) { | ||
| const { id, repo, slug } = await params | ||
| const token = (await cookies()).get('github_token')?.value ?? null | ||
|
|
||
| const res = await fetch( | ||
| `${process.env.NEXT_PUBLIC_API_DOMAIN}/api/v1/posting/github/${replaceId(id)}/${slug}`, | ||
| { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${token}`, | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| const { data } = await res.json() | ||
|
|
||
| if (!data?.githubDownloadUrl) { | ||
| return ( | ||
| <div> | ||
| 해당 포스트를 찾을 수 없습니다. <br /> | ||
| <Link href={`/@${replaceId(id)}/${repo}`} className="text-blue-500 hover:underline"> | ||
| 다른 포스트 보기 | ||
| </Link> | ||
| </div> | ||
| ) | ||
| } | ||
| const post = await fetchPost(data?.githubDownloadUrl) | ||
|
|
||
| return <UpdatePost token={token} post={post} /> | ||
| } |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| 'use client' | ||
|
|
||
| import { Button } from '@components/ui' | ||
| import { useRouter } from 'next/navigation' | ||
| import React from 'react' | ||
|
|
||
| interface PostButtonsProps { | ||
| id: string | ||
| repo: string | ||
| slug: string | ||
| } | ||
|
|
||
| export const PostButtons = ({ id, repo, slug }: PostButtonsProps) => { | ||
| const router = useRouter() | ||
|
|
||
| const handleUpdate = () => { | ||
| router.push(`/@${id}/${repo}/${slug}/update`) | ||
| } | ||
|
|
||
| const handleDelete = () => { | ||
| const confirm = window.confirm('정말 삭제하시겠습니까?') | ||
| if (confirm) { | ||
| console.log('delete') | ||
| } | ||
| } | ||
| return ( | ||
| <div className="flex items-center gap-4"> | ||
| <Button onClick={handleUpdate} variant="outline"> | ||
| 수정 | ||
| </Button> | ||
| <Button onClick={handleDelete} variant="outline"> | ||
| 삭제 | ||
| </Button> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요거 생성페이지와 동일 코드 반복이 좀 많아서,
저번에 말씀드렸던 방식으로 처리하면 좋을 것 같습니다.
그것은 바로
create-post/1처럼 게시글 id가 있을 경우 편집 페이지로 가는 방식There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이거 그러면 url은 update-post로 유지하고 같은 컴포넌트를 바라보게 하는게 더 좋을라나요?