-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathVideoLikeBox.jsx
More file actions
57 lines (51 loc) · 1.76 KB
/
VideoLikeBox.jsx
File metadata and controls
57 lines (51 loc) · 1.76 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
// src/components/VideoLikeBox.jsx
import React, { useEffect, useState } from 'react';
import axios from 'axios';
import { BaseUrl } from '@/ipconfig';
export default function VideoLikeBox({ videoId, refreshKey }) {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
const token = localStorage.getItem('token');
const fetchLikes = async () => {
if (!videoId || videoId === ':videoId') return;
setLoading(true);
try {
const res = await axios.get(`${BaseUrl}/video-like/${videoId}/users`, {
headers: { Authorization: `Bearer ${token}` },
});
const usersList = Array.isArray(res.data.data)
? res.data.data
: res.data?.users || [];
setUsers(usersList);
} catch (err) {
console.error('Lỗi khi lấy danh sách like:', err);
setError('Không thể lấy danh sách người like.');
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchLikes();
}, [videoId, refreshKey]);
return (
<div className="mt-2 px-3 py-2 bg-gray-50 rounded border border-blue-100">
<p className="text-sm font-semibold text-gray-700">Người đã like:</p>
{loading ? (
<span className="text-xs italic text-gray-400">Đang tải...</span>
) : error ? (
<span className="text-xs text-red-500">{error}</span>
) : users.length === 0 ? (
<span className="text-xs italic text-gray-400">Chưa có ai like</span>
) : (
<ul className="list-disc pl-4 text-sm text-gray-800">
{users.map((user, idx) => (
<li key={idx}>
{user.fullName || user.username || 'Ẩn danh'}
</li>
))}
</ul>
)}
</div>
);
}