Skip to content

Commit

Permalink
feat: (#55) 글 작성 및 수정 관련 API, 커스텀 쿼리 훅 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
inyeong-kang committed Jul 17, 2023
1 parent 3bfe1f3 commit ae7fb58
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions frontend/src/api/jero/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PostRequest } from '@type/post';

import { putFetch, postFetch } from '@utils/fetch';

export const createPost = async (newPost: PostRequest) => {
return await postFetch('/posts', newPost);
};

export const editPost = async (postId: number, updatedPost: PostRequest) => {
return await putFetch(`/posts/${postId}`, updatedPost);
};
20 changes: 20 additions & 0 deletions frontend/src/hooks/query/post/useCreatePost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { PostRequest } from '@type/post';

import { createPost } from '@api/jero/post';

export const useCreatePost = () => {
const queryClient = useQueryClient();
const { mutate } = useMutation((post: PostRequest) => createPost(post), {
onSuccess: () => {
queryClient.invalidateQueries(['posts']);
},
onError: () => {
// mutate가 실패하면, 함수를 실행합니다.
window.console.log('createPost error');
},
});

return { mutate };
};
19 changes: 19 additions & 0 deletions frontend/src/hooks/query/post/useEditPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { PostRequest } from '@type/post';

import { editPost } from '@api/jero/post';

export const useEditPost = (postId: number) => {
const queryClient = useQueryClient();
const { mutate } = useMutation((updatedPost: PostRequest) => editPost(postId, updatedPost), {
onSuccess: () => {
queryClient.invalidateQueries(['posts', postId]);
},
onError: () => {
window.console.log('editPost error');
},
});

return { mutate };
};

0 comments on commit ae7fb58

Please sign in to comment.