feat: 事件关注功能优化 - Redux 乐观更新 + Mock 数据状态同步

1. communityDataSlice 添加事件关注乐观更新
   - pending: 立即切换 isFollowing 状态
   - rejected: 回滚到之前状态
   - fulfilled: 使用 API 返回的准确数据覆盖

2. Mock 数据添加内存状态管理
   - 新增 followedEventsSet 和 followedEventsMap 存储
   - toggleEventFollowStatus: 切换关注状态
   - isEventFollowed: 检查是否已关注
   - getFollowedEvents: 获取关注事件列表

3. Mock handlers 使用内存状态
   - follow handler: 使用 toggleEventFollowStatus
   - following handler: 使用 getFollowedEvents 动态返回
   - 事件详情: 返回正确的 is_following 状态

修复: 关注事件后导航栏"自选事件"列表不同步更新的问题

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
zdl
2025-12-09 16:34:36 +08:00
parent 726d808f5c
commit 023684b8b7
4 changed files with 138 additions and 19 deletions

View File

@@ -128,6 +128,13 @@ export const mockRealtimeQuotes = [
// ==================== 关注事件数据 ====================
// 事件关注内存存储Set 存储已关注的事件 ID
export const followedEventsSet = new Set();
// 关注事件完整数据存储Map: eventId -> eventData
export const followedEventsMap = new Map();
// 初始关注事件列表(用于初始化)
export const mockFollowingEvents = [
{
id: 101,
@@ -231,6 +238,74 @@ export const mockFollowingEvents = [
}
];
// 初始化:将 mockFollowingEvents 的数据加入内存存储
mockFollowingEvents.forEach(event => {
followedEventsSet.add(event.id);
followedEventsMap.set(event.id, event);
});
/**
* 切换事件关注状态
* @param {number} eventId - 事件 ID
* @param {Object} eventData - 事件数据(关注时需要)
* @returns {{ isFollowing: boolean, followerCount: number }}
*/
export function toggleEventFollowStatus(eventId, eventData = null) {
const wasFollowing = followedEventsSet.has(eventId);
if (wasFollowing) {
// 取消关注
followedEventsSet.delete(eventId);
followedEventsMap.delete(eventId);
} else {
// 添加关注
followedEventsSet.add(eventId);
if (eventData) {
followedEventsMap.set(eventId, {
...eventData,
followed_at: new Date().toISOString()
});
} else {
// 如果没有提供事件数据,创建基础数据
followedEventsMap.set(eventId, {
id: eventId,
title: `事件 ${eventId}`,
tags: [],
followed_at: new Date().toISOString()
});
}
}
const isFollowing = !wasFollowing;
const followerCount = isFollowing ? Math.floor(Math.random() * 500) + 100 : Math.floor(Math.random() * 500) + 50;
console.log('[Mock Data] 切换事件关注状态:', {
eventId,
wasFollowing,
isFollowing,
followedEventsCount: followedEventsSet.size
});
return { isFollowing, followerCount };
}
/**
* 检查事件是否已关注
* @param {number} eventId - 事件 ID
* @returns {boolean}
*/
export function isEventFollowed(eventId) {
return followedEventsSet.has(eventId);
}
/**
* 获取所有已关注的事件列表
* @returns {Array}
*/
export function getFollowedEvents() {
return Array.from(followedEventsMap.values());
}
// ==================== 评论数据 ====================
export const mockEventComments = [