-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: (#55) 글 작성 및 수정 관련 API, 커스텀 쿼리 훅 구현
- Loading branch information
1 parent
3bfe1f3
commit ae7fb58
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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); | ||
}; |
This file contains 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,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 }; | ||
}; |
This file contains 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,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 }; | ||
}; |