-
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.
[ Refactor ] 산책로 포스트 페이지 (좋아요 기능 개선) (#348)
* 📝docs: LikeResponse 타입 추가 - fetchPostLikedToggle 함수에 대한 반환 타입 지정 * ♻️refactor: Post 페이지 좋아요 기능 분리 및 성능 개선 - debounce 함수 적용으로 최종 상태에 따른 API 호출 (마지막 한 번만 호출함) - 만약 현재 좋아요 상태와 동일한 경우 API 호출되지 않도록 조건문 추가 - 낙관적 업데이트 되도록 수정 * 🚜change: 주석 제거
- Loading branch information
1 parent
ed7e7ef
commit 60ba1eb
Showing
6 changed files
with
96 additions
and
39 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
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 @@ | ||
export { default as useLikeMutation } from "./useLikeMutation/useLikeMutation"; |
80 changes: 80 additions & 0 deletions
80
src/app/post/[id]/components/PostMemo/hooks/useLikeMutation/useLikeMutation.ts
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,80 @@ | ||
import { useCallback, useState } from "react"; | ||
|
||
import { useUI } from "@/components/uiContext/UiContext"; | ||
import { fetchPostLikedToggle } from "@/lib/api/Post/client"; | ||
import { POST_KEY } from "@/lib/api/queryKeys"; | ||
import useAuthStore from "@/lib/stores/useAuthStore"; | ||
import checkErrorCode from "@/lib/utils/checkErrorCode"; | ||
import { LikesResponse } from "@/types/Response"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
|
||
import { debounce } from "lodash"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
interface UseLikeMutationProps { | ||
id: number; | ||
isLiked: boolean; | ||
likeCount: number; | ||
} | ||
|
||
const useLikeMutation = ({ id, isLiked, likeCount }: UseLikeMutationProps) => { | ||
const router = useRouter(); | ||
const [isLike, setIsLike] = useState(isLiked); | ||
const [likes, setLikes] = useState(likeCount); | ||
const [isCurrentLiked, setIsCurrentLiked] = useState(isLiked); | ||
|
||
const { setModalView, openModal } = useUI(); | ||
const { isLogIn } = useAuthStore(); | ||
|
||
const likeMutation = useMutation({ | ||
mutationKey: [POST_KEY.LIKED_STATUS], | ||
mutationFn: fetchPostLikedToggle, | ||
onSuccess: ({ isLike }: LikesResponse) => { | ||
setIsCurrentLiked(isLike); | ||
router.refresh(); | ||
}, | ||
onError: ({ message }) => { | ||
setModalView("ANIMATION_ALERT_VIEW"); | ||
openModal({ | ||
message: checkErrorCode({ | ||
errorCode: message, | ||
defaultMessage: "해당 요청에 문제가 발생하였습니다.<br>잠시 후 다시 시도해주세요!", | ||
}), | ||
}); | ||
}, | ||
}); | ||
|
||
const handleLikeMutation = useCallback( | ||
debounce((postId, isLike) => { | ||
if (isCurrentLiked === isLike) { | ||
return; | ||
} | ||
|
||
likeMutation.mutate({ postId: String(postId), data: { isLike: isLike } }); | ||
}, 1000), | ||
[isCurrentLiked], | ||
); | ||
|
||
const handleClickLike = () => { | ||
if (!isLogIn) { | ||
setModalView("ACCESS_LOGIN_VIEW"); | ||
openModal(); | ||
return; | ||
} | ||
|
||
setLikes((likes) => (isLike ? likes - 1 : likes + 1)); | ||
setIsLike((prevIsLike) => { | ||
const newIsLike = !prevIsLike; | ||
handleLikeMutation(id, newIsLike); | ||
return newIsLike; | ||
}); | ||
}; | ||
|
||
return { | ||
isLike, | ||
likes, | ||
handleClickLike, | ||
}; | ||
}; | ||
|
||
export default useLikeMutation; |
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
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,3 @@ | ||
export interface LikesResponse { | ||
isLike: boolean; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./User"; | ||
export * from "./Masil"; | ||
export * from "./mate"; | ||
export * from "./Like"; |