-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathLikeButton.jsx
More file actions
69 lines (61 loc) · 1.89 KB
/
LikeButton.jsx
File metadata and controls
69 lines (61 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// src/components/LikeButton.jsx
import React, { useState } from 'react';
import axios from 'axios';
import { BaseUrl } from '@/ipconfig';
import { useNavigate } from 'react-router-dom';
export default function LikeButton({ videoId }) {
const [liked, setLiked] = useState(false);
const [loading, setLoading] = useState(false);
const token = localStorage.getItem('token');
const navigate = useNavigate();
const handleLikeToggle = async () => {
if (!videoId || !token) return;
setLoading(true);
try {
if (liked) {
// ✅ GỌI API UNLIKE CHUẨN BE
await axios.post(
`${BaseUrl}/video-like/${videoId}/unlike`,
{},
{ headers: { Authorization: `Bearer ${token}` } }
);
} else {
// ✅ GỌI API LIKE CHUẨN BE
await axios.post(
`${BaseUrl}/video-like/${videoId}/like`,
{},
{ headers: { Authorization: `Bearer ${token}` } }
);
}
// Toggle trạng thái
setLiked(!liked);
} catch (error) {
console.error('Lỗi khi Like/Unlike video:', error);
} finally {
setLoading(false);
}
};
const handleViewLikes = () => {
// Điều hướng đến trang hiển thị danh sách users đã Like
navigate(`/dashboard/video-like/${videoId}`);
};
return (
<div className="flex gap-2">
<button
onClick={handleLikeToggle}
disabled={loading}
className={`px-3 py-1 rounded text-white text-sm font-semibold shadow ${
liked ? 'bg-red-500 hover:bg-red-600' : 'bg-gray-600 hover:bg-gray-700'
}`}
>
{loading ? '...' : liked ? 'Bỏ Like' : 'Like'}
</button>
<button
onClick={handleViewLikes}
className="px-3 py-1 rounded text-blue-700 bg-blue-100 hover:bg-blue-200 text-sm font-semibold shadow"
>
👥 Xem Like
</button>
</div>
);
}