Krishya | 3dc6b35 | 2025-06-07 19:02:25 +0800 | [diff] [blame] | 1 | // import React, { useState, useEffect } from 'react'; |
| 2 | // import axios from 'axios'; |
| 3 | // import './SeedRating.css'; |
| 4 | // import { useUser } from '../../../context/UserContext'; |
| 5 | |
| 6 | // const SeedRating = ({ seedId }) => { |
| 7 | // const { user } = useUser(); |
| 8 | // const [score, setScore] = useState(0); |
| 9 | // const [submitted, setSubmitted] = useState(false); |
| 10 | |
| 11 | // const handleRating = async (newScore) => { |
| 12 | // if (!user || !user.userId) { |
| 13 | // alert('请先登录再评分'); |
| 14 | // return; |
| 15 | // } |
| 16 | |
| 17 | // try { |
| 18 | // const res = await axios.post('/echo/ratings', null, { |
| 19 | // params: { |
| 20 | // userId: user.userId, |
| 21 | // seedId, |
| 22 | // score: newScore, |
| 23 | // }, |
| 24 | // }); |
| 25 | |
| 26 | // if (res.data && res.data.status === 'success') { |
| 27 | // setScore(newScore); |
| 28 | // setSubmitted(true); |
| 29 | // alert('评分成功'); |
| 30 | // } else { |
| 31 | // alert('评分失败,请稍后再试'); |
| 32 | // } |
| 33 | // } catch (error) { |
| 34 | // console.error('评分出错:', error); |
| 35 | // alert('评分请求失败'); |
| 36 | // } |
| 37 | // }; |
| 38 | |
| 39 | |
| 40 | |
| 41 | // return ( |
| 42 | // <div className="seed-rating"> |
| 43 | // <span>评分:</span> |
| 44 | // {[1, 2, 3, 4, 5].map((star) => ( |
| 45 | // <span |
| 46 | // key={star} |
| 47 | // className={`star ${star <= score ? 'active' : ''}`} |
| 48 | // // onClick={() => !submitted && handleRating(star)} |
| 49 | // onClick={() => handleRating(star)} |
| 50 | |
| 51 | // > |
| 52 | // ★ |
| 53 | // </span> |
| 54 | // ))} |
| 55 | // {submitted && <span className="thank-you">感谢您的评分!</span>} |
| 56 | // </div> |
| 57 | // ); |
| 58 | // }; |
| 59 | |
| 60 | // export default SeedRating; |
| 61 | |
| 62 | import React, { useState, useEffect } from 'react'; |
| 63 | import axios from 'axios'; |
| 64 | import './SeedRating.css'; |
| 65 | import { useUser } from '../../../context/UserContext'; |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 66 | import AuthButton from '../../../components/AuthButton'; |
Krishya | 3dc6b35 | 2025-06-07 19:02:25 +0800 | [diff] [blame] | 67 | |
| 68 | const SeedRating = ({ seedId }) => { |
| 69 | const { user } = useUser(); |
| 70 | const [score, setScore] = useState(0); // 最终提交的分数 |
| 71 | const [hoverScore, setHoverScore] = useState(0); // 鼠标悬停显示 |
| 72 | const [submitted, setSubmitted] = useState(false); |
| 73 | const [loading, setLoading] = useState(false); // 防止重复点击 |
| 74 | |
| 75 | const handleRating = async (newScore) => { |
| 76 | if (!user || !user.userId) { |
| 77 | alert('请先登录再评分'); |
| 78 | return; |
| 79 | } |
| 80 | if (submitted || loading) return; |
| 81 | |
| 82 | setLoading(true); |
| 83 | try { |
| 84 | const res = await axios.post('/echo/ratings', null, { |
| 85 | params: { |
| 86 | userId: user.userId, |
| 87 | seedId, |
| 88 | score: newScore, |
| 89 | }, |
| 90 | }); |
| 91 | |
| 92 | if (res.data?.status === 'success') { |
| 93 | setScore(newScore); |
| 94 | setSubmitted(true); |
| 95 | alert('评分成功'); |
| 96 | } else { |
| 97 | alert('评分失败,请稍后再试'); |
| 98 | } |
| 99 | } catch (error) { |
| 100 | console.error('评分出错:', error); |
| 101 | alert('评分请求失败'); |
| 102 | } finally { |
| 103 | setLoading(false); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | return ( |
| 108 | <div className="seed-rating"> |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 109 | <span style={{color: '#333'}}>评分:</span> |
Krishya | 3dc6b35 | 2025-06-07 19:02:25 +0800 | [diff] [blame] | 110 | {[1, 2, 3, 4, 5].map((star) => ( |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 111 | <AuthButton |
| 112 | key={star} |
| 113 | roles={["cookie", "chocolate", "ice-cream"]} |
| 114 | style={{ |
| 115 | backgroundColor: 'inherit', |
| 116 | margin: 0, |
| 117 | // color: '#ccc', |
| 118 | padding: 0, |
| 119 | outline: 'none', |
| 120 | border: 'none', |
| 121 | cursor: 'pointer' |
| 122 | }} |
| 123 | className={`star ${(star <= (hoverScore || score) ? 'active' : '')} ${hoverScore >= star ? 'hover' : ''}`} |
| 124 | onMouseEnter={() => !submitted && setHoverScore(star)} |
| 125 | onMouseLeave={() => !submitted && setHoverScore(0)} |
| 126 | onClick={() => handleRating(star)} |
| 127 | > |
| 128 | ★ |
| 129 | </AuthButton> |
| 130 | |
Krishya | 3dc6b35 | 2025-06-07 19:02:25 +0800 | [diff] [blame] | 131 | ))} |
| 132 | {submitted && <span className="thank-you">感谢您的评分!</span>} |
| 133 | </div> |
| 134 | ); |
| 135 | }; |
| 136 | |
| 137 | export default SeedRating; |