fix: 좋아요·저장·댓글이 서버에서 실패하면 화면을 되돌리고 알림
This commit is contained in:
1 parent
8a5d45c513
commit
ae6b06ac1f
2 files changed
+23
-7
No files matched your search
@@ -226,7 +226,7 @@ function FullCard({ post }: PostProps) {
|
||||
const comments = () => open({ type: 'comments', postId: post.post_id })
|
||||
const patch = (values: Partial<PostData>) => patchFeed(queryClient, post.post_id, values)
|
||||
|
||||
// 화면을 먼저 바꾸고, 실패하면 되돌린다
|
||||
// 화면을 먼저 바꾸고, 실패하면 되돌린다 (서버는 실패도 200 + result.success=false 로 준다)
|
||||
const setLike = (on: boolean) => {
|
||||
if (!loggedIn) return open({ type: 'login' })
|
||||
if (on === liked) return
|
||||
@@ -235,7 +235,10 @@ function FullCard({ post }: PostProps) {
|
||||
;(on ? like : unlike).mutate(
|
||||
{ path: { post_id: post.post_id } },
|
||||
{
|
||||
onSuccess: (res) => patch({ liked: res.liked ?? on, ...(res.like_count != null ? { like_count: res.like_count } : {}) }),
|
||||
onSuccess: (res) =>
|
||||
res.result?.success
|
||||
? patch({ liked: res.liked ?? on, ...(res.like_count != null ? { like_count: res.like_count } : {}) })
|
||||
: patch({ liked, like_count: likeCount }),
|
||||
onError: () => patch({ liked, like_count: likeCount }),
|
||||
},
|
||||
)
|
||||
@@ -248,7 +251,7 @@ function FullCard({ post }: PostProps) {
|
||||
;(on ? save : unsave).mutate(
|
||||
{ path: { post_id: post.post_id } },
|
||||
{
|
||||
onSuccess: (res) => patch({ saved: res.saved ?? on }),
|
||||
onSuccess: (res) => patch({ saved: res.result?.success ? (res.saved ?? on) : saved }),
|
||||
onError: () => patch({ saved }),
|
||||
},
|
||||
)
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in new issue
Block a user