收藏种子交互

Change-Id: Ifa884aedb39d0a7880171f2e7756124f366d3d0a
diff --git a/front/src/TorrentDetailPage.js b/front/src/TorrentDetailPage.js
index ab34d98..407306b 100644
--- a/front/src/TorrentDetailPage.js
+++ b/front/src/TorrentDetailPage.js
@@ -8,10 +8,11 @@
   const [detail, setDetail] = React.useState(null);

   const [loading, setLoading] = React.useState(true);

   const [error, setError] = React.useState(null);

-  // 假设你从某个地方获取了 userId(例如登录状态、localStorage 等)

-  const [userId] = React.useState('user1550e8400-e29b-41d4-a716-44665544000023'); // 替换为实际的用户 ID

-  const [isFavorite, setIsFavorite] = React.useState(false); // 收藏状态

+  const [isFavorite, setIsFavorite] = React.useState(false);

+  const match = document.cookie.match('(^|;)\\s*userId=([^;]+)');

+  const userId = match ? match[2] : null;

 

+  // 下载种子

   const handleClick = () => {

     // 构造下载 URL,包含 userId 和 torrentId 参数

     console.log(torrentId)

@@ -42,9 +43,32 @@
       });

   };

 

-  // 收藏按钮示例逻辑(仅前端切换)

+  // 收藏种子

   const handleFavorite = () => {

-    setIsFavorite(fav => !fav);

+    fetch(`${API_BASE_URL}/api/add-favorite`, {

+      method: "POST",

+      headers: {

+        "Content-Type": "application/json"

+      },

+      body: JSON.stringify({

+        userid: userId,

+        seedid: torrentId

+      })

+    })

+      .then(response => {

+        if (!response.ok) {

+          throw new Error('收藏失败');

+        }

+        return response.json();

+      })

+      .then(data => {

+        setIsFavorite(true);

+        alert("收藏成功!");

+      })

+      .catch(error => {

+        console.error('收藏错误:', error);

+        alert('收藏失败: ' + error.message);

+      });

   };

 

   React.useEffect(() => {