增加对不同帖子类型的工具函数

Change-Id: I725923ef606d0f2b434da3f5f4ab6927a5fe3e9a
diff --git a/src/views/upload/upload.tsx b/src/views/upload/upload.tsx
index 4a60330..1bf096a 100644
--- a/src/views/upload/upload.tsx
+++ b/src/views/upload/upload.tsx
@@ -1,16 +1,46 @@
 import instance from '@/utils/axios';
-import React, { useState } from 'react';
+import React, { useCallback, useEffect, useState } from 'react';
 import styles from './upload.module.css';
-import { Upload } from '@/api/upload';
-import { useNavigate } from 'react-router-dom'; // 用于跳转
+import {postTorrentUpload } from '@/api/torrent';
+import { useLocation, useNavigate } from 'react-router-dom'; // 用于跳转
+import { getTagsByMainTag } from '@/utils/common';
+import { Checkbox, Row, Col } from 'antd'; // 使用 antd 的 Checkbox 组件
+import { MainTag } from '@/types/common';
+import { Select } from 'antd';
+import { UploadOutlined } from '@ant-design/icons';
+import type { UploadProps } from 'antd';
+import { Button, message, Upload as UploadArea } from 'antd';
 
-const PostDetails = () => {
+
+const CreatePost = () => {
+  const location = useLocation();
   const [postTitle, setPostTitle] = useState('');
-  const [postType, setPostType] = useState('');
   const [postContent, setPostContent] = useState('');
   const [isChecked, setIsChecked] = useState(false);
-
+  const {type} = location.state || {type: ''}; // 获取路由传递的参数
+  const [tagIds, setTagIds] = useState<number[]>([]);
+  const [postType, setPostType] = useState<string>(type || ''); // 初始化 postType
   const navigate = useNavigate();
+  const [tags, setTags] = useState<Map<string, number>>(new Map());
+
+  const props: UploadProps = {
+    name: 'file',
+    action: process.env.REACT_APP_BASE_URL + postTorrentUpload, // 替换为实际的上传接口地址
+    headers: {
+      authorization: 'authorization-text',
+    },
+    onChange(info) {
+      if (info.file.status !== 'uploading') {
+        console.log(info.file, info.fileList);
+      }
+      if (info.file.status === 'done') {
+        message.success(`${info.file.name} file uploaded successfully`);
+      } else if (info.file.status === 'error') {
+        message.error(`${info.file.name} file upload failed.`);
+      }
+    },
+  };
+
 
   const handleSubmit = async () => {
     if (!postTitle.trim() || !postType || !postContent.trim()) {
@@ -54,6 +84,17 @@
     }
   };
 
+  useEffect(() => {
+    setTags(getTagsByMainTag(type)); // 获取对应类型的标签
+  },[]);
+
+  const handlePostTypeChange = useCallback((value:string) => {
+    setPostType(value);
+    setTags(getTagsByMainTag(value)); // 更新 tags
+    setTagIds([]); // 清空已选的标签 ID
+  }, [postType, setTags, setTagIds]);
+
+
   return (
     <div className={styles.container}>
       <div className={styles.formGroup}>
@@ -66,29 +107,42 @@
           className={styles.input}
         />
       </div>
+  
+      <div className={styles.formGroup}>
+        <label>种子类型:</label>
+        <Select
+          value={postType}
+          onChange={handlePostTypeChange}
+
+          options ={ MainTag.map((item) => ({
+            label: item,
+            value: item
+          }))} />
+      </div>
 
       <div className={styles.formGroup}>
         <label>类型选择:</label>
-        <select
-          value={postType}
-          onChange={(e) => setPostType(e.target.value)}
-          className={styles.select}
+        <Checkbox.Group
+          value={tagIds}
+          onChange={(checkedValues) => setTagIds(checkedValues as number[])}
         >
-          <option value="">下拉选择</option>
-          <option value="type1">类型一</option>
-          <option value="type2">类型二</option>
-        </select>
+          <Row gutter={[12, 12]}>
+            {[...tags.entries()].map(([name, id]) => (
+              <Col key={id} xs={12} sm={8} md={6} lg={4}>
+                <Checkbox value={id}>{name}</Checkbox>
+              </Col>
+            ))}
+          </Row>
+        </Checkbox.Group>
       </div>
 
       {/* 暂时移除上传文件表单 */}
-      {/* <div className={styles.formGroup}>
+      <div className={styles.formGroup}>
         <label>上传资源:</label>
-        <input
-          type="file"
-          onChange={(e) => setFile(e.target.files?.[0] || null)}
-          className={styles.upload}
-        />
-      </div> */}
+        <UploadArea {...props}>
+          <Button icon={<UploadOutlined />}>Upload</Button>
+        </UploadArea>
+      </div>
 
       <div className={styles.formGroup}>
         <label>内容介绍:</label>
@@ -113,11 +167,11 @@
         <span>我已知晓以上内容</span>
       </div>
 
-      <button onClick={handleSubmit} className={styles.submitBtn}>
+      <button onClick={handleSubmit} className={styles.submitBtn} disabled={!isChecked}>
         我已知晓
       </button>
     </div>
   );
 };
 
-export default PostDetails;
+export default CreatePost;