fix: sync store state when like/favorite actions complete
- Update templateSocialStore when like/unlike succeeds - Update templateSocialStore when favorite/unfavorite succeeds - Sync likeCount and favoriteCount from API response - Ensure state consistency across video page and detail page Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
30625bc820
commit
e6416ee604
|
|
@ -11,6 +11,7 @@ import { useCallback, useState } from 'react'
|
||||||
import { type ApiError } from '@/lib/types'
|
import { type ApiError } from '@/lib/types'
|
||||||
|
|
||||||
import { handleError } from './use-error'
|
import { handleError } from './use-error'
|
||||||
|
import { templateSocialStore } from '@/stores/templateSocialStore'
|
||||||
|
|
||||||
export const useTemplateFavorite = (
|
export const useTemplateFavorite = (
|
||||||
templateId?: string,
|
templateId?: string,
|
||||||
|
|
@ -44,6 +45,13 @@ export const useTemplateFavorite = (
|
||||||
}
|
}
|
||||||
|
|
||||||
setFavorited(true)
|
setFavorited(true)
|
||||||
|
|
||||||
|
// 同步更新 store
|
||||||
|
templateSocialStore.setFavorited(templateId, true)
|
||||||
|
if (data?.favoriteCount !== undefined) {
|
||||||
|
templateSocialStore.setFavoriteCount(templateId, data.favoriteCount)
|
||||||
|
}
|
||||||
|
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
return { data, error: null }
|
return { data, error: null }
|
||||||
}, [templateId])
|
}, [templateId])
|
||||||
|
|
@ -72,6 +80,13 @@ export const useTemplateFavorite = (
|
||||||
}
|
}
|
||||||
|
|
||||||
setFavorited(false)
|
setFavorited(false)
|
||||||
|
|
||||||
|
// 同步更新 store
|
||||||
|
templateSocialStore.setFavorited(templateId, false)
|
||||||
|
if (data?.favoriteCount !== undefined) {
|
||||||
|
templateSocialStore.setFavoriteCount(templateId, data.favoriteCount)
|
||||||
|
}
|
||||||
|
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
return { data, error: null }
|
return { data, error: null }
|
||||||
}, [templateId])
|
}, [templateId])
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { useCallback, useState } from 'react'
|
||||||
|
|
||||||
import { type ApiError } from '@/lib/types'
|
import { type ApiError } from '@/lib/types'
|
||||||
import { handleError } from './use-error'
|
import { handleError } from './use-error'
|
||||||
|
import { templateSocialStore } from '@/stores/templateSocialStore'
|
||||||
|
|
||||||
export const useTemplateLike = (templateId?: string) => {
|
export const useTemplateLike = (templateId?: string) => {
|
||||||
const [liked, setLiked] = useState<boolean>(false)
|
const [liked, setLiked] = useState<boolean>(false)
|
||||||
|
|
@ -31,6 +32,13 @@ export const useTemplateLike = (templateId?: string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
setLiked(true)
|
setLiked(true)
|
||||||
|
|
||||||
|
// 同步更新 store
|
||||||
|
templateSocialStore.setLiked(templateId, true)
|
||||||
|
if (data?.likeCount !== undefined) {
|
||||||
|
templateSocialStore.setLikeCount(templateId, data.likeCount)
|
||||||
|
}
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
}, [templateId])
|
}, [templateId])
|
||||||
|
|
||||||
|
|
@ -55,6 +63,13 @@ export const useTemplateLike = (templateId?: string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
setLiked(false)
|
setLiked(false)
|
||||||
|
|
||||||
|
// 同步更新 store
|
||||||
|
templateSocialStore.setLiked(templateId, false)
|
||||||
|
if (data?.likeCount !== undefined) {
|
||||||
|
templateSocialStore.setLikeCount(templateId, data.likeCount)
|
||||||
|
}
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
}, [templateId])
|
}, [templateId])
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue