22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 1 | import React, { useState } from 'react'; |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 2 | import { followUser, unfollowUser } from '../api/api_ljc'; |
| 3 | |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 4 | const FollowButton = ({ userId, isFollowing, onFollowChange, style }) => { |
| 5 | const [loading, setLoading] = useState(false); |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 6 | const handleFollow = async () => { |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 7 | if (loading) return; |
| 8 | setLoading(true); |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 9 | try { |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 10 | console.log('FollowButton clicked, isFollowing:', isFollowing, 'userId:', userId); |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 11 | if (isFollowing) { |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 12 | const res = await unfollowUser(userId); |
| 13 | console.log('unfollowUser response:', res); |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 14 | onFollowChange(false); |
| 15 | } else { |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 16 | const res = await followUser(userId); |
| 17 | console.log('followUser response:', res); |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 18 | onFollowChange(true); |
| 19 | } |
| 20 | } catch (error) { |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 21 | alert('关注操作失败,请重试!'); |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 22 | console.error('关注操作失败:', error); |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 23 | } finally { |
| 24 | setLoading(false); |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 25 | } |
| 26 | }; |
| 27 | |
| 28 | return ( |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 29 | <button |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 30 | onClick={handleFollow} |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 31 | disabled={loading} |
| 32 | style={{ |
| 33 | border: 'none', |
| 34 | outline: 'none', |
| 35 | borderRadius: 20, |
| 36 | padding: '6px 22px', |
| 37 | fontSize: 15, |
| 38 | fontWeight: 600, |
| 39 | cursor: loading ? 'not-allowed' : 'pointer', |
| 40 | background: isFollowing ? '#f5f5f5' : 'linear-gradient(90deg,#ff4b2b,#ff416c)', |
| 41 | color: isFollowing ? '#888' : '#fff', |
| 42 | boxShadow: isFollowing ? 'none' : '0 2px 8px #ff416c22', |
| 43 | transition: 'all 0.2s', |
| 44 | ...style |
| 45 | }} |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 46 | > |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame^] | 47 | {loading ? '处理中...' : isFollowing ? '已关注' : '+ 关注'} |
TRM-coding | 29174c2 | 2025-06-18 23:56:51 +0800 | [diff] [blame] | 48 | </button> |
| 49 | ); |
| 50 | }; |
| 51 | |
| 52 | export default FollowButton; |