- 나는 글쓰기
-
{/*todo (5-2) 제목 / 태그 셀렉 / 내용 입력란 추가*/}
-
{/*todo (5-3) 나가기, 저장하기 버튼 추가*/}
+
+
+
+ {tagList.map(tag => (
+
+ ))}
+
+
+
+
+
+ 나가기
+
+ 저장하기
+
);
};
diff --git a/src/queries/useCreatePost.ts b/src/queries/useCreatePost.ts
new file mode 100644
index 0000000..c2d4128
--- /dev/null
+++ b/src/queries/useCreatePost.ts
@@ -0,0 +1,15 @@
+import { createPost } from '../api';
+import { useMutation } from '@tanstack/react-query';
+
+const useCreatePost = () => {
+ const mutation = async ({ title, contents, tag }: { title: string; contents: string; tag: string }) => {
+ await createPost(title, contents, tag);
+ };
+
+ return useMutation({
+ mutationKey: ['createPost'],
+ mutationFn: mutation,
+ });
+};
+
+export default useCreatePost;
diff --git a/src/queries/useDeletePostById.ts b/src/queries/useDeletePostById.ts
new file mode 100644
index 0000000..89c563b
--- /dev/null
+++ b/src/queries/useDeletePostById.ts
@@ -0,0 +1,24 @@
+import { useNavigate } from 'react-router-dom';
+import { deletePostById } from '../api';
+import { useMutation } from '@tanstack/react-query';
+
+const useDeletePostById = () => {
+ const navigate = useNavigate();
+
+ const mutate = async ({ postId }: { postId: string }) => {
+ const { data } = await deletePostById(postId);
+ return data;
+ };
+
+ return useMutation({
+ mutationFn: mutate,
+ onSuccess: () => {
+ navigate('/');
+ },
+ onError: () => {
+ alert('게시글 삭제에 실패했습니다. 잠시 후 다시 시도해주세요.');
+ },
+ });
+};
+
+export default useDeletePostById;
diff --git a/src/queries/useGetPostById.ts b/src/queries/useGetPostById.ts
new file mode 100644
index 0000000..6da40b5
--- /dev/null
+++ b/src/queries/useGetPostById.ts
@@ -0,0 +1,17 @@
+import { getPostById} from "../api";
+import { useQuery } from '@tanstack/react-query';
+
+const useGetPostById = (postId: string) => {
+ const fetcher = async () => {
+ const { data } = await getPostById(postId);
+ return data;
+ };
+
+ return useQuery({
+ queryKey: ['getPostById', postId],
+ queryFn: fetcher,
+ enabled: !!postId,
+ });
+};
+
+export default useGetPostById;
diff --git a/src/queries/useGetPostList.ts b/src/queries/useGetPostList.ts
new file mode 100644
index 0000000..2d67aaf
--- /dev/null
+++ b/src/queries/useGetPostList.ts
@@ -0,0 +1,16 @@
+import { getPostList } from '../api';
+import { useQuery } from '@tanstack/react-query';
+
+const useGetPostList = () => {
+ const fetcher = async () => {
+ const { data } = await getPostList();
+ return data;
+ };
+
+ return useQuery({
+ queryKey: ['getPostList'],
+ queryFn: fetcher,
+ });
+};
+
+export default useGetPostList;
diff --git a/src/queries/useUpdatePostById.ts b/src/queries/useUpdatePostById.ts
new file mode 100644
index 0000000..028a1b5
--- /dev/null
+++ b/src/queries/useUpdatePostById.ts
@@ -0,0 +1,16 @@
+import { updatePostById } from '../api';
+import { useMutation } from '@tanstack/react-query';
+import { TAG } from '../api/types.ts';
+
+const useUpdatePostById = () => {
+ const mutation = async ({ postId, title, contents, tag }: { postId: string; title: string; contents: string; tag: TAG }) => {
+ await updatePostById(postId, title, contents, tag);
+ };
+
+ return useMutation({
+ mutationKey: ['updatePost'],
+ mutationFn: mutation,
+ });
+};
+
+export default useUpdatePostById;
diff --git a/vite.config.ts b/vite.config.ts
index 627a319..f49dabb 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -3,5 +3,6 @@ import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/
export default defineConfig({
+ base: '/CNU_Blog/',
plugins: [react()],
});