blob: 3e94161e4a7af3aa54df398e4e7f92b764fafbce [file] [log] [blame]
22301008e25b4b02025-06-20 22:15:31 +08001import React, { useState } from 'react';
TRM-coding29174c22025-06-18 23:56:51 +08002import { followUser, unfollowUser } from '../api/api_ljc';
3
22301008e25b4b02025-06-20 22:15:31 +08004const FollowButton = ({ userId, isFollowing, onFollowChange, style }) => {
5 const [loading, setLoading] = useState(false);
TRM-coding29174c22025-06-18 23:56:51 +08006 const handleFollow = async () => {
22301008e25b4b02025-06-20 22:15:31 +08007 if (loading) return;
8 setLoading(true);
TRM-coding29174c22025-06-18 23:56:51 +08009 try {
22301008e25b4b02025-06-20 22:15:31 +080010 console.log('FollowButton clicked, isFollowing:', isFollowing, 'userId:', userId);
TRM-coding29174c22025-06-18 23:56:51 +080011 if (isFollowing) {
22301008e25b4b02025-06-20 22:15:31 +080012 const res = await unfollowUser(userId);
13 console.log('unfollowUser response:', res);
TRM-coding29174c22025-06-18 23:56:51 +080014 onFollowChange(false);
15 } else {
22301008e25b4b02025-06-20 22:15:31 +080016 const res = await followUser(userId);
17 console.log('followUser response:', res);
TRM-coding29174c22025-06-18 23:56:51 +080018 onFollowChange(true);
19 }
20 } catch (error) {
22301008e25b4b02025-06-20 22:15:31 +080021 alert('关注操作失败,请重试!');
TRM-coding29174c22025-06-18 23:56:51 +080022 console.error('关注操作失败:', error);
22301008e25b4b02025-06-20 22:15:31 +080023 } finally {
24 setLoading(false);
TRM-coding29174c22025-06-18 23:56:51 +080025 }
26 };
27
28 return (
22301008e25b4b02025-06-20 22:15:31 +080029 <button
TRM-coding29174c22025-06-18 23:56:51 +080030 onClick={handleFollow}
22301008e25b4b02025-06-20 22:15:31 +080031 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-coding29174c22025-06-18 23:56:51 +080046 >
22301008e25b4b02025-06-20 22:15:31 +080047 {loading ? '处理中...' : isFollowing ? '已关注' : '+ 关注'}
TRM-coding29174c22025-06-18 23:56:51 +080048 </button>
49 );
50};
51
52export default FollowButton;