Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame^] | 1 | // import React, { useEffect, useState } from 'react'; |
| 2 | // import axios from 'axios'; |
| 3 | // import Header from '../../components/Header'; |
| 4 | // import UserNav from './UserNav'; |
| 5 | // import { useUser } from '../../context/UserContext'; |
| 6 | // import { useLocation } from 'wouter'; |
| 7 | // import './UserProfile.css'; // ✅ 复用个人资料通用样式 |
| 8 | |
| 9 | // const UserCollect = () => { |
| 10 | // const { user, loading: userLoading } = useUser(); |
| 11 | // const [collections, setCollections] = useState([]); |
| 12 | // const [loading, setLoading] = useState(true); |
| 13 | // const [error, setError] = useState(null); |
| 14 | // const [, navigate] = useLocation(); |
| 15 | |
| 16 | // useEffect(() => { |
| 17 | // if (userLoading) return; |
| 18 | |
| 19 | // if (!user || !user.userId) { |
| 20 | // setError('未登录或用户信息获取失败'); |
| 21 | // setLoading(false); |
| 22 | // return; |
| 23 | // } |
| 24 | |
| 25 | // const fetchCollections = async () => { |
| 26 | // try { |
| 27 | // const response = await axios.get(`/echo/forum/posts/${user.userId}/getAllcollections`); |
| 28 | // setCollections(response.data || []); |
| 29 | // setError(null); |
| 30 | // } catch (error) { |
| 31 | // console.error('获取收藏失败:', error); |
| 32 | // setError('获取收藏失败,请稍后再试'); |
| 33 | // } finally { |
| 34 | // setLoading(false); |
| 35 | // } |
| 36 | // }; |
| 37 | |
| 38 | // fetchCollections(); |
| 39 | // }, [user, userLoading]); |
| 40 | |
| 41 | // if (loading || userLoading) { |
| 42 | // return <p className="loading">加载中...</p>; |
| 43 | // } |
| 44 | |
| 45 | // return ( |
| 46 | // <div className="user-profile-container"> |
| 47 | // <Header /> |
| 48 | // <div className="user-center"> |
| 49 | // <div className="user-nav-container"> |
| 50 | // <UserNav activeKey="collect" /> |
| 51 | // </div> |
| 52 | |
| 53 | // <div className="common-card right-content"> |
| 54 | // <div className="profile-header"> |
| 55 | // <h1>📌 我的收藏</h1> |
| 56 | // </div> |
| 57 | |
| 58 | // {error ? ( |
| 59 | // <p className="error-text">{error}</p> |
| 60 | // ) : collections.length === 0 ? ( |
| 61 | // <p>暂无收藏内容。</p> |
| 62 | // ) : ( |
| 63 | // <div className="collection-list"> |
| 64 | // {collections.map((post) => ( |
| 65 | // <div |
| 66 | // key={post.postNo} |
| 67 | // className="collection-item" |
| 68 | // onClick={() => navigate(`/forum/post/${post.postNo}`)} |
| 69 | // > |
| 70 | // <h3>{post.title || '无标题'}</h3> |
| 71 | // <p className="content-preview"> |
| 72 | // {post.postContent?.slice(0, 100) || '暂无内容'}... |
| 73 | // </p> |
| 74 | // <p className="meta"> |
| 75 | // 作者:{post.username || '未知'} | 发布时间未知 |
| 76 | // </p> |
| 77 | // <hr /> |
| 78 | // </div> |
| 79 | // ))} |
| 80 | // </div> |
| 81 | // )} |
| 82 | // </div> |
| 83 | // </div> |
| 84 | // </div> |
| 85 | // ); |
| 86 | // }; |
| 87 | |
| 88 | // export default UserCollect; |
| 89 | |
22301009 | 6401163 | 2025-06-04 21:57:22 +0800 | [diff] [blame] | 90 | import React, { useEffect, useState } from 'react'; |
| 91 | import axios from 'axios'; |
22301009 | 6401163 | 2025-06-04 21:57:22 +0800 | [diff] [blame] | 92 | import { useUser } from '../../context/UserContext'; |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame^] | 93 | import { useLocation } from 'wouter'; |
22301009 | 6401163 | 2025-06-04 21:57:22 +0800 | [diff] [blame] | 94 | import './UserCollect.css'; |
| 95 | |
| 96 | const UserCollect = () => { |
| 97 | const { user, loading: userLoading } = useUser(); |
| 98 | const [collections, setCollections] = useState([]); |
| 99 | const [loading, setLoading] = useState(true); |
| 100 | const [error, setError] = useState(null); |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame^] | 101 | const [, navigate] = useLocation(); |
22301009 | 6401163 | 2025-06-04 21:57:22 +0800 | [diff] [blame] | 102 | |
| 103 | useEffect(() => { |
| 104 | if (userLoading) return; |
| 105 | |
| 106 | if (!user || !user.userId) { |
| 107 | setError('未登录或用户信息获取失败'); |
| 108 | setLoading(false); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | const fetchCollections = async () => { |
| 113 | try { |
| 114 | const response = await axios.get(`/echo/forum/posts/${user.userId}/getAllcollections`); |
| 115 | setCollections(response.data || []); |
| 116 | setError(null); |
| 117 | } catch (error) { |
| 118 | console.error('获取收藏失败:', error); |
| 119 | setError('获取收藏失败,请稍后再试'); |
| 120 | } finally { |
| 121 | setLoading(false); |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | fetchCollections(); |
| 126 | }, [user, userLoading]); |
| 127 | |
| 128 | return ( |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame^] | 129 | <div className="common-card right-content"> |
| 130 | <div className="profile-header"> |
| 131 | <h2>📌 我的收藏</h2> |
22301009 | 6401163 | 2025-06-04 21:57:22 +0800 | [diff] [blame] | 132 | </div> |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame^] | 133 | |
| 134 | {userLoading || loading ? ( |
| 135 | <p className="loading-text">加载中...</p> |
| 136 | ) : error ? ( |
| 137 | <p className="error-text">{error}</p> |
| 138 | ) : collections.length === 0 ? ( |
| 139 | <p>暂无收藏内容。</p> |
| 140 | ) : ( |
| 141 | <div className="collection-list"> |
| 142 | {collections.map((post) => ( |
| 143 | <div |
| 144 | key={post.postNo} |
| 145 | className="collection-item" |
| 146 | onClick={() => navigate(`/forum/post/${post.postNo}`)} |
| 147 | > |
| 148 | <h3>{post.title || '无标题'}</h3> |
| 149 | <p className="preview">{post.postContent?.slice(0, 100) || '暂无内容'}...</p> |
| 150 | <p className="meta">作者:{post.username || '未知'} | 发布时间未知</p> |
| 151 | <hr /> |
| 152 | </div> |
| 153 | ))} |
| 154 | </div> |
| 155 | )} |
22301009 | 6401163 | 2025-06-04 21:57:22 +0800 | [diff] [blame] | 156 | </div> |
| 157 | ); |
| 158 | }; |
| 159 | |
| 160 | export default UserCollect; |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame^] | 161 | |