Merge "button state"
diff --git a/Merge/front/src/components/PostDetailJWLLL.jsx b/Merge/front/src/components/PostDetailJWLLL.jsx
index 417105a..9304b39 100644
--- a/Merge/front/src/components/PostDetailJWLLL.jsx
+++ b/Merge/front/src/components/PostDetailJWLLL.jsx
@@ -8,7 +8,7 @@
import MediaPreview from './MediaPreview'
import '../style/PostDetail.css'
import dayjs from 'dayjs'
-import { followUser, unfollowUser } from '../api/api_ljc'
+import { followUser, unfollowUser ,getUserFollowing} from '../api/api_ljc'
export default function PostDetail() {
const { id } = useParams()
@@ -67,24 +67,33 @@
}, [id])
// 检查当前用户是否已关注发帖人
- useEffect(() => {
- if (post && post.user_id) {
- // 这里假设有API postsAPI.getUserFollowing
- const checkFollow = async () => {
- try {
- const userInfo = getUserInfo()
- if (!userInfo?.id) return
- const res = await postsAPI.getUserFollowing(userInfo.id)
- if (Array.isArray(res)) {
- setIsFollowing(res.some(u => u.id === post.user_id))
- } else if (Array.isArray(res.following)) {
- setIsFollowing(res.following.some(u => u.id === post.user_id))
- }
- } catch {}
- }
- checkFollow()
- }
- }, [post])
+ const checkFollowStatus = useCallback(async (postUserId) => {
+ if (!postUserId) return;
+
+ const currentUserId = getCurrentUserId();
+ if (!currentUserId) return;
+
+ try {
+ const response = await getUserFollowing(currentUserId);
+ // 处理不同的响应结构
+ const followingList = Array.isArray(response) ? response : (response.data || []);
+
+ console.log(response)
+ // 检查目标用户是否在关注列表中
+ setIsFollowing(
+ followingList.some(user => user.id === postUserId)
+ );
+ } catch (error) {
+ console.error('获取关注状态失败:', error);
+ }
+}, []);
+
+// 当帖子数据加载后,检查关注状态
+useEffect(() => {
+ if (post && post.user_id) {
+ checkFollowStatus(post.user_id);
+ }
+}, [post, checkFollowStatus])
// 拉取发帖人信息
useEffect(() => {
@@ -319,24 +328,24 @@
</span>
{/* 关注按钮 */}
{post.user_id && (
- <button
- className={`follow-btn ${isFollowing ? 'following' : ''}`}
- onClick={handleFollowAction}
- disabled={followLoading}
- style={{
- marginLeft: '12px',
- padding: '4px 12px',
- borderRadius: '20px',
- border: '1px solid #ccc',
- background: isFollowing ? '#f0f0f0' : '#007bff',
- color: isFollowing ? '#333' : 'white',
- cursor: 'pointer',
- fontSize: '14px'
- }}
- >
- {followLoading ? '处理中...' : (isFollowing ? '已关注' : '关注')}
- </button>
- )}
+ <button
+ className={`follow-btn ${isFollowing ? 'following' : ''}`}
+ onClick={handleFollowAction}
+ disabled={followLoading}
+ style={{
+ marginLeft: '12px',
+ padding: '4px 12px',
+ borderRadius: '20px',
+ border: '1px solid #ccc',
+ background: isFollowing ? '#f0f0f0' : '#007bff',
+ color: isFollowing ? '#333' : 'white',
+ cursor: 'pointer',
+ fontSize: '14px'
+ }}
+ >
+ {followLoading ? '处理中...' : (isFollowing ? '已关注' : '关注')}
+ </button>
+ )}
</div>
</div>
<div className="post-stats">