feat(ProcessPostUseCase, HomeScreen, PostDetailScreen, PostService): implement share count synchronization after post sharing
- Added applyShareCountUpdate method in ProcessPostUseCase to synchronize share counts across post lists and details. - Updated sharePost method in PostService to return the latest shares_count from the server. - Modified share handling in HomeScreen and PostDetailScreen to utilize the new share count synchronization logic after a successful share operation. - Enhanced error handling for share operations to improve user feedback.
This commit is contained in:
@@ -333,6 +333,35 @@ class ProcessPostUseCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分享计数上报成功后,同步各列表与详情中的分享数
|
||||||
|
*/
|
||||||
|
applyShareCountUpdate(postId: string, sharesCount: number): void {
|
||||||
|
this.postsState.forEach((state, key) => {
|
||||||
|
const index = state.posts.findIndex((p) => p.id === postId);
|
||||||
|
if (index !== -1) {
|
||||||
|
const newPosts = [...state.posts];
|
||||||
|
const cur = newPosts[index];
|
||||||
|
newPosts[index] = {
|
||||||
|
...cur,
|
||||||
|
sharesCount,
|
||||||
|
shares_count: sharesCount,
|
||||||
|
};
|
||||||
|
this.updatePostsState(key, { posts: newPosts });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const detail = this.getPostDetailState(postId);
|
||||||
|
if (detail.post) {
|
||||||
|
this.updatePostDetailState(postId, {
|
||||||
|
post: {
|
||||||
|
...detail.post,
|
||||||
|
sharesCount,
|
||||||
|
shares_count: sharesCount,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== 帖子列表操作 ====================
|
// ==================== 帖子列表操作 ====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -336,7 +336,10 @@ export const HomeScreen: React.FC = () => {
|
|||||||
const handleShare = async (post: Post) => {
|
const handleShare = async (post: Post) => {
|
||||||
if (!post?.id) return;
|
if (!post?.id) return;
|
||||||
try {
|
try {
|
||||||
await postService.sharePost(post.id);
|
const res = await postService.sharePost(post.id, { channel: 'copy_link' });
|
||||||
|
if (res.ok && res.shares_count != null) {
|
||||||
|
processPostUseCase.applyShareCountUpdate(post.id, res.shares_count);
|
||||||
|
}
|
||||||
} catch (shareError) {
|
} catch (shareError) {
|
||||||
console.error('上报分享次数失败:', shareError);
|
console.error('上报分享次数失败:', shareError);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -514,7 +514,15 @@ export const PostDetailScreen: React.FC = () => {
|
|||||||
const handleShare = useCallback(async () => {
|
const handleShare = useCallback(async () => {
|
||||||
if (!post?.id) return;
|
if (!post?.id) return;
|
||||||
try {
|
try {
|
||||||
await postService.sharePost(post.id);
|
const res = await postService.sharePost(post.id, { channel: 'copy_link' });
|
||||||
|
if (res.ok && res.shares_count != null) {
|
||||||
|
setPost((prev) =>
|
||||||
|
prev
|
||||||
|
? { ...prev, shares_count: res.shares_count!, sharesCount: res.shares_count! }
|
||||||
|
: null
|
||||||
|
);
|
||||||
|
processPostUseCase.applyShareCountUpdate(post.id, res.shares_count);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('上报分享次数失败:', error);
|
console.error('上报分享次数失败:', error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -250,14 +250,24 @@ class PostService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 分享帖子
|
/** 上报分享成功;返回服务端最新 shares_count 供界面同步 */
|
||||||
async sharePost(postId: string): Promise<boolean> {
|
async sharePost(
|
||||||
|
postId: string,
|
||||||
|
body?: { channel?: string }
|
||||||
|
): Promise<{ ok: boolean; shares_count?: number }> {
|
||||||
try {
|
try {
|
||||||
const response = await api.post(`/posts/${postId}/share`);
|
const response = await api.post<{ success: boolean; shares_count: number }>(
|
||||||
return response.code === 0;
|
`/posts/${postId}/share`,
|
||||||
|
body && body.channel ? { channel: body.channel } : undefined
|
||||||
|
);
|
||||||
|
if (response.code !== 0) {
|
||||||
|
return { ok: false };
|
||||||
|
}
|
||||||
|
const n = response.data?.shares_count;
|
||||||
|
return { ok: true, shares_count: typeof n === 'number' ? n : undefined };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('分享帖子失败:', error);
|
console.error('分享帖子失败:', error);
|
||||||
return false;
|
return { ok: false };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user