fix: 좋아요·저장·댓글이 서버에서 실패하면 화면을 되돌리고 알림

This commit is contained in:
minaChoi committed 2026-10-06 10:30:33 +09:00
1 parent 8a5d45c513
commit ae6b06ac1f
2 files changed
+23 -7

No files matched your search

+17 -4
View File
@@ -422,6 +422,7 @@ function CommentRow({ c, postId }: { c: CommentData; postId: number }) {
const like = useMutation(likeCommentMutation())
const unlike = useMutation(unlikeCommentMutation())
const remove = useMutation(deleteCommentMutation())
const [removeFailed, setRemoveFailed] = useState(false)
const toggle = () => {
if (!loggedIn) return open({ type: 'login' })
@@ -432,6 +433,11 @@ function CommentRow({ c, postId }: { c: CommentData; postId: number }) {
{ path: { comment_id: c.comment_id } },
{
onSuccess: (res) => {
if (!res.result?.success) {
setLiked(!next)
setCount(c.like_count)
return
}
setLiked(!!res.liked)
if (res.like_count != null) setCount(res.like_count)
},
@@ -447,10 +453,13 @@ function CommentRow({ c, postId }: { c: CommentData; postId: number }) {
remove.mutate(
{ path: { comment_id: c.comment_id } },
{
onSuccess: () => {
onSuccess: (res) => {
setRemoveFailed(!res.result?.success)
if (!res.result?.success) return
queryClient.invalidateQueries({ queryKey: listCommentsQueryKey({ path: { post_id: postId } }) })
queryClient.invalidateQueries({ queryKey: getFeedQueryKey({ query: { region: region.code } }) })
},
onError: () => setRemoveFailed(true),
},
)
@@ -473,7 +482,7 @@ function CommentRow({ c, postId }: { c: CommentData; postId: number }) {
</>
)}
</small>
{remove.isError && <small className="block text-xs font-bold text-(--heart)">지우지 못했어요. 다시 해 주세요.</small>}
{removeFailed && <small className="block text-xs font-bold text-(--heart)">지우지 못했어요. 다시 해 주세요.</small>}
</div>
<button type="button" onClick={toggle} aria-pressed={liked} aria-label="댓글 좋아요" className="cursor-pointer pt-1">
<HeartIcon className={cx('size-4 stroke-2', liked ? 'fill-(--heart) stroke-(--heart)' : 'fill-none stroke-(--ink3)')} />
@@ -489,6 +498,7 @@ function CommentsSheet({ postId }: { postId: number }) {
const queryClient = useQueryClient()
const list = useQuery(listCommentsOptions({ path: { post_id: postId } }))
const add = useMutation(addCommentMutation())
const [addFailed, setAddFailed] = useState(false)
const [text, setText] = useState('')
const inputRef = useRef<HTMLInputElement>(null)
const comments = list.data?.comments ?? []
@@ -501,11 +511,14 @@ function CommentsSheet({ postId }: { postId: number }) {
add.mutate(
{ path: { post_id: postId }, body: { body } },
{
onSuccess: () => {
onSuccess: (res) => {
setAddFailed(!res.result?.success)
if (!res.result?.success) return
setText('')
queryClient.invalidateQueries({ queryKey: listCommentsQueryKey({ path: { post_id: postId } }) })
queryClient.invalidateQueries({ queryKey: getFeedQueryKey({ query: { region: region.code } }) })
},
onError: () => setAddFailed(true),
},
)
}
@@ -539,7 +552,7 @@ function CommentsSheet({ postId }: { postId: number }) {
</button>
))}
</div>
{add.isError && (
{addFailed && (
<p className="px-4 pb-1 text-center text-xs font-bold text-(--heart)" role="alert">
댓글을 올리지 못했어요. 다시 해 주세요.
</p>