FEAT: Add functionality to update ongoing stream on frontend
This commit is contained in:
@@ -6,7 +6,7 @@ import ListItem from "../components/Layout/ListItem";
|
|||||||
import { X as XIcon, Eye as ShowIcon, EyeOff as HideIcon } from "lucide-react";
|
import { X as XIcon, Eye as ShowIcon, EyeOff as HideIcon } from "lucide-react";
|
||||||
import { useAuth } from "../context/AuthContext";
|
import { useAuth } from "../context/AuthContext";
|
||||||
import { debounce } from "lodash";
|
import { debounce } from "lodash";
|
||||||
import VideoPlayer from "../components/Video/VideoPlayer";
|
import VideoPlayer from "../components/Stream/VideoPlayer";
|
||||||
|
|
||||||
interface StreamData {
|
interface StreamData {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -66,6 +66,10 @@ const StreamDashboardPage: React.FC = () => {
|
|||||||
}, [categories, thumbnailPreview.isCustom]);
|
}, [categories, thumbnailPreview.isCustom]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
checkStreamStatus();
|
||||||
|
fetchCategories();
|
||||||
|
}, [username]);
|
||||||
|
|
||||||
const checkStreamStatus = async () => {
|
const checkStreamStatus = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/user/${username}/status`);
|
const response = await fetch(`/api/user/${username}/status`);
|
||||||
@@ -102,14 +106,13 @@ const StreamDashboardPage: React.FC = () => {
|
|||||||
}m ago`
|
}m ago`
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
// Just need the stream key if not streaming
|
||||||
const response = await fetch(`/api/user/${username}/stream_key`);
|
const response = await fetch(`/api/user/${username}/stream_key`);
|
||||||
const keyData = await response.json();
|
const keyData = await response.json();
|
||||||
setStreamData((prev) => ({
|
setStreamData((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
stream_key: keyData.stream_key,
|
stream_key: keyData.stream_key,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
console.log("Stream key:", keyData.stream_key);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error checking stream status:", error);
|
console.error("Error checking stream status:", error);
|
||||||
@@ -127,10 +130,6 @@ const StreamDashboardPage: React.FC = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
checkStreamStatus();
|
|
||||||
fetchCategories();
|
|
||||||
}, [username]);
|
|
||||||
|
|
||||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
setStreamData((prev) => ({ ...prev, [name]: value }));
|
setStreamData((prev) => ({ ...prev, [name]: value }));
|
||||||
@@ -201,11 +200,11 @@ const StreamDashboardPage: React.FC = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleStartStream = async () => {
|
const handlePublishStream = async () => {
|
||||||
console.log("Starting stream with data:", streamData);
|
console.log("Starting stream with data:", streamData);
|
||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("key", streamData.stream_key);
|
formData.append("data", JSON.stringify(streamData));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/publish_stream", {
|
const response = await fetch("/api/publish_stream", {
|
||||||
@@ -228,6 +227,30 @@ const StreamDashboardPage: React.FC = () => {
|
|||||||
|
|
||||||
const handleUpdateStream = async () => {
|
const handleUpdateStream = async () => {
|
||||||
console.log("Updating stream with data:", streamData);
|
console.log("Updating stream with data:", streamData);
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append("key", streamData.stream_key);
|
||||||
|
formData.append("title", streamData.title);
|
||||||
|
formData.append("category_name", streamData.category_name);
|
||||||
|
if (thumbnail) {
|
||||||
|
formData.append("thumbnail", thumbnail);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/update_stream", {
|
||||||
|
method: "POST",
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
console.log("Stream updated successfully");
|
||||||
|
window.location.reload();
|
||||||
|
} else {
|
||||||
|
console.error("Failed to update stream");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating stream:", error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleEndStream = async () => {
|
const handleEndStream = async () => {
|
||||||
@@ -392,7 +415,7 @@ const StreamDashboardPage: React.FC = () => {
|
|||||||
<div className="flex gap-8">
|
<div className="flex gap-8">
|
||||||
<Button
|
<Button
|
||||||
onClick={
|
onClick={
|
||||||
isStreaming ? handleUpdateStream : handleStartStream
|
isStreaming ? handleUpdateStream : handlePublishStream
|
||||||
}
|
}
|
||||||
disabled={!isFormValid()}
|
disabled={!isFormValid()}
|
||||||
extraClasses="text-2xl px-8 py-4 disabled:opacity-50 disabled:cursor-not-allowed"
|
extraClasses="text-2xl px-8 py-4 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
|||||||
Reference in New Issue
Block a user