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

@@ -608,14 +608,40 @@ const communityDataSlice = createSlice({
state.error[stateKey] = action.payload;
logger.error('CommunityData', `${stateKey} 加载失败`, new Error(action.payload));
})
// toggleEventFollow
// ===== toggleEventFollow(乐观更新)=====
// pending: 立即切换状态
.addCase(toggleEventFollow.pending, (state, action) => {
const eventId = action.meta.arg;
const current = state.eventFollowStatus[eventId];
// 乐观切换:如果当前已关注则变为未关注,反之亦然
state.eventFollowStatus[eventId] = {
isFollowing: !(current?.isFollowing),
followerCount: current?.followerCount ?? 0
};
logger.debug('CommunityData', 'toggleEventFollow pending (乐观更新)', {
eventId,
newIsFollowing: !(current?.isFollowing)
});
})
// rejected: 回滚状态
.addCase(toggleEventFollow.rejected, (state, action) => {
const eventId = action.meta.arg;
const current = state.eventFollowStatus[eventId];
// 回滚:恢复到之前的状态(再次切换回去)
state.eventFollowStatus[eventId] = {
isFollowing: !(current?.isFollowing),
followerCount: current?.followerCount ?? 0
};
logger.error('CommunityData', 'toggleEventFollow rejected (已回滚)', {
eventId,
error: action.payload
});
})
// fulfilled: 使用 API 返回的准确数据覆盖
.addCase(toggleEventFollow.fulfilled, (state, action) => {
const { eventId, isFollowing, followerCount } = action.payload;
state.eventFollowStatus[eventId] = { isFollowing, followerCount };
logger.debug('CommunityData', 'toggleEventFollow fulfilled', { eventId, isFollowing, followerCount });
})
.addCase(toggleEventFollow.rejected, (_state, action) => {
logger.error('CommunityData', 'toggleEventFollow rejected', action.payload);
});
}
});