Skip to content

Commit

Permalink
DELETE, POST, PUT post/v2 API 마이그레이션 (#943)
Browse files Browse the repository at this point in the history
* refactor: DELETE /post/v2

* refactor: POST /post/v2/like

* refactor: PUT post/v2
  • Loading branch information
j-nary authored Nov 3, 2024
1 parent e0e3d89 commit 645ed00
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pages/post/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default function PostPage() {
const { mutate: togglePostLike } = useMutationPostLike(query.id as string);

const { mutate: mutateDeletePost } = useMutation({
mutationFn: () => DELETE('/post/v1/{postId}', { params: { path: { postId: post!.id } } }),
mutationFn: () => DELETE('/post/v2/{postId}', { params: { path: { postId: post!.id } } }),
onSuccess: () => router.replace(`/detail?id=${post?.meeting.id}`),
});

Expand Down
4 changes: 2 additions & 2 deletions src/api/post/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const useMutationUpdateLike = (take: number, meetingId?: number) => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (postId: number) => postLike(String(postId)),
mutationFn: (postId: number) => postLike(postId),
onMutate: async postId => {
await queryClient.cancelQueries(['getPosts', take, meetingId]);

Expand Down Expand Up @@ -77,7 +77,7 @@ export const useMutationPostLike = (queryId: string) => {

return useMutation({
mutationKey: ['getPost', queryId],
mutationFn: () => postLike(queryId),
mutationFn: () => postLike(+queryId),
onMutate: async () => {
const previousPost = queryClient.getQueryData(['getPost', queryId]) as postType;

Expand Down
15 changes: 8 additions & 7 deletions src/api/post/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ export const createPost = async (formData: FormCreateType) => {
};

export const editPost = async (postId: string, formData: FormEditType) => {
const { data } = await api.put<Data<Pick<PostResponse, 'id' | 'title' | 'contents' | 'updatedDate' | 'images'>>>(
`/post/v1/${postId}`,
formData
);
type editPostType =
paths['/post/v2/{postId}']['put']['responses']['200']['content']['application/json;charset=UTF-8'];
const { data } = await api.put<editPostType>(`/post/v2/${postId}`, formData);
return data;
};

Expand All @@ -42,9 +41,11 @@ export const getPost = async (postId: string) => {
return data;
};

export const postLike = async (queryId: string) => {
const { POST } = apiV2.get();
return await POST('/post/v1/{postId}/like', { params: { path: { postId: Number(queryId) } } });
export const postLike = async (postId: number) => {
type postListType =
paths['/post/v2/{postId}/like']['post']['responses']['201']['content']['application/json;charset=UTF-8'];
const { data } = await api.post<postListType>(`/post/v2/${postId}/like`);
return data;
};

export const deleteComment = async (commentId: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const PostDeleteModal = ({ isOpen, close, postId, meetingId }: PostDeleteModalPr
const { DELETE } = apiV2.get();

const { mutate: mutateDeletePost } = useMutation({
mutationFn: () => DELETE('/post/v1/{postId}', { params: { path: { postId: postId } } }),
mutationFn: () => DELETE('/post/v2/{postId}', { params: { path: { postId: postId } } }),
onSuccess: () => queryClient.invalidateQueries(['getPosts']),
//todo: 지금은 getPosts 로 시작하는 모든 query 가 invalidate 됨.
});
Expand Down

0 comments on commit 645ed00

Please sign in to comment.