fix: bug
This commit is contained in:
@@ -18,6 +18,9 @@ import ErrorState from '@/components/ErrorState'
|
||||
import LoadingState from '@/components/LoadingState'
|
||||
import { useUserLikes } from '@/hooks'
|
||||
import { useTemplateSocialStore } from '@/stores/templateSocialStore'
|
||||
import { root } from '@repo/core'
|
||||
import { TemplateSocialController } from '@repo/sdk'
|
||||
import { handleError } from '@/hooks/use-error'
|
||||
|
||||
const NUM_COLUMNS = 2
|
||||
const HORIZONTAL_PADDING = 16
|
||||
@@ -37,7 +40,7 @@ export default function LikesScreen() {
|
||||
const [refreshing, setRefreshing] = useState(false)
|
||||
|
||||
// 使用 Store 中的点赞/收藏状态
|
||||
const { setLiked, setFavorited } = useTemplateSocialStore()
|
||||
const { setLiked, setFavorited, incrementLikeCount, decrementLikeCount } = useTemplateSocialStore()
|
||||
|
||||
// 获取用户点赞列表
|
||||
const {
|
||||
@@ -78,27 +81,56 @@ export default function LikesScreen() {
|
||||
})
|
||||
}, [router])
|
||||
|
||||
// 点赞/取消点赞处理
|
||||
const handleLike = useCallback((id: string) => {
|
||||
setLiked(id, true)
|
||||
console.log('Like template:', id)
|
||||
}, [setLiked])
|
||||
// 获取 social controller
|
||||
const getSocialController = useCallback(() => {
|
||||
return root.get(TemplateSocialController)
|
||||
}, [])
|
||||
|
||||
const handleUnlike = useCallback((id: string) => {
|
||||
// 点赞/取消点赞处理
|
||||
const handleLike = useCallback(async (id: string) => {
|
||||
setLiked(id, true)
|
||||
incrementLikeCount(id)
|
||||
try {
|
||||
const social = getSocialController()
|
||||
await handleError(() => social.like({ templateId: id }))
|
||||
} catch (e) {
|
||||
setLiked(id, false)
|
||||
decrementLikeCount(id)
|
||||
}
|
||||
}, [setLiked, incrementLikeCount, decrementLikeCount, getSocialController])
|
||||
|
||||
const handleUnlike = useCallback(async (id: string) => {
|
||||
setLiked(id, false)
|
||||
console.log('Unlike template:', id)
|
||||
}, [setLiked])
|
||||
decrementLikeCount(id)
|
||||
try {
|
||||
const social = getSocialController()
|
||||
await handleError(() => social.unlike({ templateId: id }))
|
||||
} catch (e) {
|
||||
setLiked(id, true)
|
||||
incrementLikeCount(id)
|
||||
}
|
||||
}, [setLiked, incrementLikeCount, decrementLikeCount, getSocialController])
|
||||
|
||||
// 收藏/取消收藏处理
|
||||
const handleFavorite = useCallback((id: string) => {
|
||||
const handleFavorite = useCallback(async (id: string) => {
|
||||
setFavorited(id, true)
|
||||
console.log('Favorite template:', id)
|
||||
}, [setFavorited])
|
||||
try {
|
||||
const social = getSocialController()
|
||||
await handleError(() => social.favorite({ templateId: id }))
|
||||
} catch (e) {
|
||||
setFavorited(id, false)
|
||||
}
|
||||
}, [setFavorited, getSocialController])
|
||||
|
||||
const handleUnfavorite = useCallback((id: string) => {
|
||||
const handleUnfavorite = useCallback(async (id: string) => {
|
||||
setFavorited(id, false)
|
||||
console.log('Unfavorite template:', id)
|
||||
}, [setFavorited])
|
||||
try {
|
||||
const social = getSocialController()
|
||||
await handleError(() => social.unfavorite({ templateId: id }))
|
||||
} catch (e) {
|
||||
setFavorited(id, true)
|
||||
}
|
||||
}, [setFavorited, getSocialController])
|
||||
|
||||
// 状态判断
|
||||
const showEmptyState = useMemo(() =>
|
||||
@@ -133,6 +165,7 @@ export default function LikesScreen() {
|
||||
onPress={handleTemplatePress}
|
||||
liked={item.template.isLiked}
|
||||
favorited={item.template.isFavorited}
|
||||
likeCount={item.template.likeCount}
|
||||
onLike={handleLike}
|
||||
onUnlike={handleUnlike}
|
||||
onFavorite={handleFavorite}
|
||||
|
||||
Reference in New Issue
Block a user