chore: main 병합 — 로그인 없이 댓글·좋아요, 구글 로그인만, 동네 이야기·카드뉴스 등 (차단 표 user_block 하나로 합침, 익명 댓글도 신고 가능)
This commit is contained in:
commit
dba34675c8
408 files changed
+36377
-725
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import { useState } from 'react'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { likePostMutation, unlikePostMutation } from '@nalssigak/shared/api/client/@tanstack/react-query.gen'
|
||||
import type { PostReaction, ResFeed } from '@nalssigak/shared/api/client/types.gen'
|
||||
|
||||
export type PinnedKey = 'hero_post' | 'gak_post' | 'foliage_post' | 'events_post' | 'season_post'
|
||||
|
||||
/** 히어로·오늘의 각·단풍 지도·띠처럼 포스트 틀이 아닌 카드의 좋아요. 화면을 먼저 바꾸고 실패하면 되돌린다 */
|
||||
export function usePinnedLike(key: PinnedKey, r?: PostReaction | null) {
|
||||
const queryClient = useQueryClient()
|
||||
const like = useMutation(likePostMutation())
|
||||
const unlike = useMutation(unlikePostMutation())
|
||||
const [failed, setFailed] = useState(false)
|
||||
const liked = !!r?.liked
|
||||
const count = r?.like_count ?? 0
|
||||
const patch = (values: Partial<PostReaction>) =>
|
||||
queryClient.setQueriesData<ResFeed>({ predicate: (q) => (q.queryKey[0] as { _id?: string } | undefined)?._id === 'getFeed' }, (feed) =>
|
||||
feed?.[key] && feed[key].post_id === r?.post_id ? { ...feed, [key]: { ...feed[key], ...values } } : feed,
|
||||
)
|
||||
const toggle = () => {
|
||||
if (!r || like.isPending || unlike.isPending) return
|
||||
const on = !liked
|
||||
setFailed(false)
|
||||
patch({ liked: on, like_count: count + (on ? 1 : -1) })
|
||||
const revert = () => {
|
||||
patch({ liked, like_count: count })
|
||||
setFailed(true)
|
||||
}
|
||||
;(on ? like : unlike).mutate(
|
||||
{ path: { post_id: r.post_id } },
|
||||
{
|
||||
onSuccess: (res) => (res.result?.success ? patch({ liked: res.liked ?? on, ...(res.like_count != null ? { like_count: res.like_count } : {}) }) : revert()),
|
||||
onError: revert,
|
||||
},
|
||||
)
|
||||
}
|
||||
return { liked, count, failed, toggle }
|
||||
}
|
||||
Reference in new issue
Block a user