Skip to content

Commit f2ee832

Browse files
committed
WatchLiveStreamComponent and update routing for livestreams
1 parent f420093 commit f2ee832

File tree

4 files changed

+77
-7
lines changed

4 files changed

+77
-7
lines changed

src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ function App() {
3636
<Route path="/search" element={<Search />} />
3737
<Route path="/livestream" element={<Livestream />} />
3838
<Route path="/watchlivestream" element={<WatchLiveStream />}/>
39+
<Route path="/watchlivestream/:streamKey" element={<WatchLiveStream />}/>
40+
3941
</Routes>
4042
</Router>
4143
);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import axios from "axios";
2+
import React, { useEffect, useState } from "react";
3+
import { useNavigate } from 'react-router-dom';
4+
5+
function formatTimestamp(timestamp) {
6+
const now = new Date();
7+
const diff = now.getTime() - timestamp.getTime();
8+
9+
// Tính toán số phút, số giờ, số ngày
10+
const minutes = Math.floor(diff / (1000 * 60));
11+
const hours = Math.floor(diff / (1000 * 60 * 60));
12+
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
13+
14+
// Định dạng lại thời gian
15+
if (minutes < 60) {
16+
return `${minutes} phút trước`;
17+
} else if (hours < 24) {
18+
return `${hours} giờ trước`;
19+
} else {
20+
return `${days} ngày trước`;
21+
}
22+
}
23+
24+
const WatchLiveStreamComponent = (props) =>
25+
{
26+
const [widthVideo, setWidthVideo] = useState(props?.wi)
27+
useEffect(()=>{
28+
setWidthVideo(widthVideo)
29+
}, [])
30+
const navigate = useNavigate()
31+
const handleClick = async ( videoId) => {
32+
33+
const bodyHis = {
34+
userId: localStorage.getItem('userToken'),
35+
thumbId: videoId
36+
}
37+
const formData = new FormData()
38+
formData.append('userId', localStorage.getItem('userToken'))
39+
formData.append('thumbId', videoId)
40+
// try {
41+
// await axios.post(`${process.env.REACT_APP_API_URL}/video/addHistory`, formData);
42+
// } catch (error) {
43+
// console.error('Đã xảy ra lỗi:', error);
44+
// }
45+
navigate(`/watchlivestream/${props?.streamKey}`)
46+
};
47+
const timestamp = new Date(props?.timestamp);
48+
const formattedTime = formatTimestamp(timestamp);
49+
return(
50+
<div className={`flex w-[405px]`} onClick={()=>handleClick(props?.videoId)}>
51+
<div className='p-[15px] hover:bg-[#dddddd] bg-white mx-4 my-4 mb-10 drop-shadow-lg rounded-[10px] cursor-pointer peer peer-focus:bg-[#f2f2f2]'>
52+
<img className=' w-[370px] h-[200px] rounded-[20px]' src={props?.img} alt='other'/>
53+
<div className='font-roboto mr-2 '>
54+
<p className='text-[18px] font-medium text-black mt-3 leading-6'>{props?.title}</p>
55+
<p className='text-[16px] mt-1'>{props?.username}</p>
56+
<div className='flex text-[16px] justify-between'>
57+
<p>{props?.view === undefined? '0':props?.view} views</p>
58+
<p>{formattedTime}</p>
59+
</div>
60+
</div>
61+
</div>
62+
63+
</div>
64+
)
65+
}
66+
export default WatchLiveStreamComponent

src/pages/Home.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { IoMdMenu } from "react-icons/io";
44
import VideoComponent from '../components/VideoCom/VideoComponent';
55
import Loading from '../components/Loading';
66
import ImageLiveStream from '../assets/images/livestream.png';
7+
import WatchLiveStreamComponent from '../components/VideoCom/WatchLiveStreamComponent';
78
const Home = () => {
89
const [videoIds, setVideoIds] = useState([]);
910
const [values, setValueIds] = useState([]);
@@ -136,15 +137,15 @@ const Home = () => {
136137
<div className='flex flex-wrap ml-2'>
137138
{livestreams.length > 0 ? (
138139
livestreams.map((stream, index) => (
139-
<VideoComponent
140+
<WatchLiveStreamComponent
140141
wi={405}
141142
key={stream.streamKey}
142143
img={ImageLiveStream}
143144
title={stream.titleLive}
144145
username={stream.userName}
145146
timestamp=""
146147
view=""
147-
videoId={stream.streamKey}
148+
streamKey={stream.streamKey}
148149
userid=""
149150
/>
150151
))

src/pages/WatchLiveStream.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import { CiShare1 } from "react-icons/ci";
55
import CopyButton from "../components/ButtonCustom/CopyButton";
66
import ReactPlayer from "react-player";
77
import { ToastContainer, toast } from "react-toastify";
8-
8+
import { useParams } from 'react-router-dom';
99
const WatchLiveStream = () => {
1010
const [isStreamLive, setIsStreamLive] = useState(false); // Trạng thái livestream
1111
const [chatMessages, setChatMessages] = useState([]); // Phải là một mảng
1212
const [message, setMessage] = useState(""); // Tin nhắn hiện tại
13-
const streamUrl = `http://192.168.120.213:13000/hls/2DEKS.m3u8`;
14-
const eventCode = "event123"; // Mã sự kiện mặc định
15-
13+
const { streamKey } = useParams();
14+
const streamUrl = `http://192.168.120.213:13000/hls/${streamKey}.m3u8`;
15+
const eventCode = streamKey; // Mã sự kiện mặc định
16+
console.log(streamKey + "asddas");
1617
// Kiểm tra trạng thái livestream
1718
const checkStreamStatus = async () => {
1819
try {
@@ -27,7 +28,7 @@ const WatchLiveStream = () => {
2728
// Lấy danh sách tin nhắn từ API
2829
const fetchChatMessages = async () => {
2930
try {
30-
const response = await fetch(`${process.env.REACT_APP_API_CHAT}/chat/event123`);
31+
const response = await fetch(`${process.env.REACT_APP_API_CHAT}/chat/${streamKey}`);
3132

3233
// Kiểm tra nếu không phải là JSON hợp lệ, có thể là HTML do lỗi
3334
const text = await response.text(); // Đọc phản hồi dưới dạng văn bản

0 commit comments

Comments
 (0)