Ajouter une vidéo
{videoTitle || "Titre de la vidéo"}
{videoDescription || "Description de la vidéo"}
import Navbar from "../components/Navbar.jsx"; import { useEffect, useState } from "react"; import Tag from "../components/Tag.jsx"; import { getChannel, searchByUsername } from "../services/user.service.js"; import { uploadVideo, uploadThumbnail, uploadTags } from "../services/video.service.js"; import UserCard from "../components/UserCard.jsx"; import LoadingVideoModal from "../modals/LoadingVideoModal.jsx"; import { useNavigate } from "react-router-dom"; export default function AddVideo() { const storedUser = localStorage.getItem("user"); const user = storedUser ? JSON.parse(storedUser) : null; const token = localStorage.getItem("token"); const navigation = useNavigate(); const [videoTitle, setVideoTitle] = useState(""); const [videoDescription, setVideoDescription] = useState(""); const [videoTags, setVideoTags] = useState([]); const [visibility, setVisibility] = useState("private"); const [videoThumbnail, setVideoThumbnail] = useState(null); const [videoFile, setVideoFile] = useState(null); const [channel, setChannel] = useState(null); const [searchUser, setSearchUser] = useState(""); const [authorizedUsers, setAuthorizedUsers] = useState([]); const [searchResults, setSearchResults] = useState([]); const [alerts, setAlerts] = useState([]); const [loadingState, setLoadingState] = useState("none"); const [loadingMessage, setLoadingMessage] = useState(""); useEffect(() => { fetchChannel(); }, []) const fetchChannel = async () => { const fetchedChannel = await getChannel(user.id, token, addAlert); setChannel(fetchedChannel.channel); } const handleTagKeyDown = (e) => { if (e.key === 'Enter' && videoTags.length < 10) { e.preventDefault(); const newTag = e.target.value.trim(); if (newTag && !videoTags.includes(newTag)) { setVideoTags([...videoTags, newTag]); e.target.value = ''; } } } const handleTagRemove = (tagToRemove) => { setVideoTags(videoTags.filter(tag => tag !== tagToRemove)); }; // This function handles the submission of the video form const handleSubmit = async (e) => { e.preventDefault(); console.log(channel) setLoadingState("loading"); setLoadingMessage("Envoie de la vidéo..."); if (!videoTitle || !videoDescription || !videoThumbnail || !videoFile) { addAlert('error', 'Veuillez remplir tous les champs requis.'); return; } if (!channel || !channel.id) { addAlert('error', 'Chaîne non valide veuillez recharger la page.'); return; } if (videoTags.length > 10) { addAlert('error', 'Vous ne pouvez pas ajouter plus de 10 tags.'); return; } const formData = new FormData(); formData.append("title", videoTitle); formData.append("description", videoDescription); formData.append("channel", channel.id.toString()); formData.append("visibility", visibility); formData.append("authorizedUsers", JSON.stringify(authorizedUsers.map(user => user.id))); formData.append("file", videoFile); const request = await uploadVideo(formData, token, addAlert); setLoadingMessage("Envoie de la miniature..."); // If the video was successfully created, we can now upload the thumbnail const response = await request.json(); const videoId = response.id; const thumbnailFormData = new FormData(); thumbnailFormData.append("video", videoId); thumbnailFormData.append("file", videoThumbnail); thumbnailFormData.append("channel", channel.id.toString()); await uploadThumbnail(thumbnailFormData, token, addAlert); setLoadingMessage("Envoie des tags..."); // if the thumbnail was successfully uploaded, we can send the tags const body = { tags: videoTags, channel: channel.id.toString() }; await uploadTags(body, videoId, token, addAlert); // If everything is successful, redirect to the video management page navigation("/manage-channel/" + channel.id); addAlert('success', 'Vidéo ajoutée avec succès !'); }; const addAlert = (type, message) => { const newAlert = { type, message, id: Date.now() }; // Add unique ID setAlerts([...alerts, newAlert]); }; const onCloseAlert = (alertToRemove) => { setAlerts(alerts.filter(alert => alert !== alertToRemove)); }; const onUserSearch = (e) => { const searchUser = e.target.value; if (searchUser.trim() !== "") { // Call the API to search for users searchByUsername(searchUser, token, addAlert) .then((results) => { setSearchResults(results); }) .catch((error) => { addAlert('error', 'Erreur lors de la recherche d\'utilisateurs.'); }); } else { setSearchResults([]); } } const onAuthorizedUserAdd = (user) => { // Verify if user is not already authorized if (authorizedUsers.find((u) => u.id === user.id)) { addAlert('error', 'Utilisateur déjà autorisé.'); setSearchUser("") setSearchResults([]); return; } setAuthorizedUsers([...authorizedUsers, user]); setSearchResults([]); setSearchUser("") }; const onAuthorizedUserRemove = (user) => { setAuthorizedUsers(authorizedUsers.filter((u) => u.id !== user.id)); }; return (
{videoDescription || "Description de la vidéo"}