|
|
|
@ -4,6 +4,7 @@ import {useAuth} from "../contexts/AuthContext.jsx"; |
|
|
|
import {useEffect, useState} from "react"; |
|
|
|
import LinearGraph from "../components/LinearGraph.jsx"; |
|
|
|
import Comment from "../components/Comment.jsx"; |
|
|
|
import Tag from "../components/Tag.jsx"; |
|
|
|
|
|
|
|
|
|
|
|
export default function ManageVideo() { |
|
|
|
@ -17,9 +18,12 @@ export default function ManageVideo() { |
|
|
|
const [viewsPerDay, setViewsPerDay] = useState([]); |
|
|
|
const [videoTitle, setVideoTitle] = useState(null); |
|
|
|
const [description, setDescription] = useState(null); |
|
|
|
const [visibility, setVisibility] = useState("private"); |
|
|
|
const [editMode, setEditMode] = useState(false); |
|
|
|
const [thumbnailPreview, setThumbnailPreview] = useState(null); |
|
|
|
const [videoFile, setVideoFile] = useState(null); |
|
|
|
|
|
|
|
const nonEditModeClasses = "text-2xl font-semibold text-white p-2 focus:text-white focus:outline-none w-full font-montserrat resizable-none"; |
|
|
|
const nonEditModeClasses = "text-md font-normal text-white p-2 focus:text-white focus:outline-none w-full font-montserrat resizable-none"; |
|
|
|
const editModeClasses = nonEditModeClasses + " glassmorphism"; |
|
|
|
|
|
|
|
const nonEditModeClassesTextArea = "text-md font-normal text-white p-2 focus:text-white focus:outline-none w-full font-montserrat resizable-none w-full" |
|
|
|
@ -38,6 +42,9 @@ export default function ManageVideo() { |
|
|
|
} |
|
|
|
const data = await request.json(); |
|
|
|
setVideo(data); |
|
|
|
setVisibility(data.visibility) |
|
|
|
setVideoTitle(data.title); |
|
|
|
setDescription(data.description); |
|
|
|
} |
|
|
|
const fetchLikesPerDay = async () => { |
|
|
|
const request = await fetch(`/api/videos/${id}/likes/day`, { |
|
|
|
@ -62,6 +69,142 @@ export default function ManageVideo() { |
|
|
|
} |
|
|
|
}, [user, id, token]); |
|
|
|
|
|
|
|
const onSubmit = async (e) => { |
|
|
|
e.preventDefault(); |
|
|
|
if (!editMode) return; |
|
|
|
|
|
|
|
const request = await fetch(`/api/videos/${id}`, { |
|
|
|
method: 'PUT', |
|
|
|
headers: { |
|
|
|
'Content-Type': 'application/json', |
|
|
|
'Authorization': `Bearer ${token}` |
|
|
|
}, |
|
|
|
body: JSON.stringify({ |
|
|
|
title: videoTitle, |
|
|
|
description: description, |
|
|
|
visibility: visibility, |
|
|
|
channel: video.channel |
|
|
|
}) |
|
|
|
}); |
|
|
|
|
|
|
|
if (!request.ok) { |
|
|
|
console.error("Failed to update video"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
const form = new FormData(); |
|
|
|
if (videoFile) { |
|
|
|
form.append('file', videoFile); |
|
|
|
form.append('video', id); |
|
|
|
form.append('channel', video.channel); |
|
|
|
|
|
|
|
const videoRequest = await fetch(`/api/videos/${id}/video`, { |
|
|
|
method: 'PUT', |
|
|
|
headers: { |
|
|
|
'Authorization': `Bearer ${token}` |
|
|
|
}, |
|
|
|
body: form |
|
|
|
}); |
|
|
|
|
|
|
|
if (!videoRequest.ok) { |
|
|
|
console.error("Failed to update video file"); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const data = await request.json(); |
|
|
|
setVideo(data); |
|
|
|
setEditMode(false); |
|
|
|
} |
|
|
|
|
|
|
|
const handleThumbnailChange = async (e) => { |
|
|
|
const file = e.target.files[0]; |
|
|
|
if (file) { |
|
|
|
const reader = new FileReader(); |
|
|
|
reader.onload = (e) => { |
|
|
|
setThumbnailPreview(e.target.result); |
|
|
|
}; |
|
|
|
reader.readAsDataURL(file); |
|
|
|
} |
|
|
|
|
|
|
|
const formData = new FormData(); |
|
|
|
formData.append('file', file); |
|
|
|
formData.append('video', id); |
|
|
|
formData.append('channel', video.channel); |
|
|
|
|
|
|
|
console.log(formData); |
|
|
|
|
|
|
|
const request = await fetch(`/api/videos/thumbnail`, { |
|
|
|
"method": 'POST', |
|
|
|
"headers": { |
|
|
|
"Authorization": `Bearer ${token}` |
|
|
|
}, |
|
|
|
body: formData |
|
|
|
}) |
|
|
|
if (!request.ok) { |
|
|
|
console.error("Failed to upload thumbnail"); |
|
|
|
return; |
|
|
|
} |
|
|
|
const data = await request.json(); |
|
|
|
console.log(data); |
|
|
|
}; |
|
|
|
|
|
|
|
const onAddTag = async (e) => { |
|
|
|
if (e.key !== 'Enter' || e.target.value.trim() === "") return; |
|
|
|
|
|
|
|
const newTag = e.target.value.trim(); |
|
|
|
e.target.value = ""; |
|
|
|
|
|
|
|
const request = await fetch(`/api/videos/${id}/tags`, { |
|
|
|
method: 'PUT', |
|
|
|
headers: { |
|
|
|
'Content-Type': 'application/json', |
|
|
|
'Authorization': `Bearer ${token}` |
|
|
|
}, |
|
|
|
body: JSON.stringify({ |
|
|
|
tags: [...video.tags, newTag], |
|
|
|
channel: video.channel |
|
|
|
}) |
|
|
|
}); |
|
|
|
if (!request.ok) { |
|
|
|
console.error("Failed to add tag"); |
|
|
|
return; |
|
|
|
} |
|
|
|
const data = await request.json(); |
|
|
|
console.log(data); |
|
|
|
setVideo({ |
|
|
|
...video, |
|
|
|
tags: [...video.tags, newTag] |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
const onSuppressTag = async (tag) => { |
|
|
|
|
|
|
|
const request = await fetch(`/api/videos/${id}/tags`, { |
|
|
|
method: 'PUT', |
|
|
|
headers: { |
|
|
|
'Content-Type': 'application/json', |
|
|
|
'Authorization': `Bearer ${token}` |
|
|
|
}, |
|
|
|
body: JSON.stringify({ |
|
|
|
tags: video.tags.filter(t => t !== tag), |
|
|
|
channel: video.channel |
|
|
|
}) |
|
|
|
}); |
|
|
|
if (!request.ok) { |
|
|
|
console.error("Failed to suppress tag"); |
|
|
|
return; |
|
|
|
} |
|
|
|
const data = await request.json(); |
|
|
|
console.log(data); |
|
|
|
const newTags = video.tags.filter(t => t !== tag); |
|
|
|
setVideo({ |
|
|
|
...video, |
|
|
|
tags: newTags |
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return ( |
|
|
|
<div className="min-w-screen min-h-screen bg-linear-to-br from-left-gradient to-right-gradient"> |
|
|
|
|
|
|
|
@ -90,8 +233,8 @@ export default function ManageVideo() { |
|
|
|
{ /* LEFT SIDE */ } |
|
|
|
<div className="flex-1" > |
|
|
|
{ /* THUMBNAIL */ } |
|
|
|
<div className="glassmorphism flex justify-center items-center py-5 relative overflow-hidden "> |
|
|
|
<img src={video ? video.thumbnail : ""} alt="" className=" rounded-sm"/> |
|
|
|
<label htmlFor="thumbnail" className="glassmorphism flex justify-center items-center py-5 relative overflow-hidden "> |
|
|
|
<img src={thumbnailPreview || (video ? video.thumbnail : "")} alt="" className=" rounded-sm"/> |
|
|
|
|
|
|
|
<div className="absolute top-0 left-0 bg-[#00000080] w-full h-full flex justify-center items-center opacity-0 hover:opacity-100 transition" > |
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" className="fill-white" viewBox="0 0 24 24"> |
|
|
|
@ -99,7 +242,8 @@ export default function ManageVideo() { |
|
|
|
</svg> |
|
|
|
</div> |
|
|
|
|
|
|
|
</div> |
|
|
|
</label> |
|
|
|
<input type="file" name="thumbnail" id="thumbnail" className="opacity-0 w-0 h-0 hidden" onChange={handleThumbnailChange} accept="image/*"/> |
|
|
|
|
|
|
|
{ /* VIDEO INFOS */ } |
|
|
|
|
|
|
|
@ -111,8 +255,8 @@ export default function ManageVideo() { |
|
|
|
type="text" |
|
|
|
id="name" |
|
|
|
value={videoTitle || videoTitle === "" ? videoTitle : video ? video.title : "Chargement"} |
|
|
|
className={(editMode ? editModeClasses : nonEditModeClasses)} |
|
|
|
onChange={(e) => setChannelName(e.target.value)} |
|
|
|
onChange={(e) => setVideoTitle(e.target.value)} |
|
|
|
className={(editMode ? editModeClasses : nonEditModeClasses) + " text-xl"} |
|
|
|
placeholder="Nom d'utilisateur" |
|
|
|
disabled={!editMode} |
|
|
|
/> |
|
|
|
@ -130,12 +274,61 @@ export default function ManageVideo() { |
|
|
|
disabled={!editMode} |
|
|
|
></textarea> |
|
|
|
|
|
|
|
<label htmlFor="visibility" className="text-2xl text-white mb-1 block font-montserrat"> |
|
|
|
Visibilité |
|
|
|
</label> |
|
|
|
<select |
|
|
|
className={(editMode ? editModeClasses : nonEditModeClasses)} |
|
|
|
value={visibility} |
|
|
|
name="visibility" |
|
|
|
onChange={(e) => setVisibility(e.target.value)} |
|
|
|
disabled={!editMode} |
|
|
|
> |
|
|
|
<option value="public">Publique</option> |
|
|
|
<option value="private">Privée</option> |
|
|
|
</select> |
|
|
|
|
|
|
|
<label htmlFor="video" className={`flex gap-2 glassmorphism p-2 items-center mt-4 cursor-pointer ${editMode ? "block" : "hidden"}`}> |
|
|
|
<video src={video ? video.file : ""} className="w-1/8 aspect-video rounded-sm" ></video> |
|
|
|
<p className="text-2xl text-white mb-1 block font-montserrat">Fichier vidéo</p> |
|
|
|
</label> |
|
|
|
<input |
|
|
|
type="file" |
|
|
|
name="video" |
|
|
|
id="video" |
|
|
|
className="hidden" |
|
|
|
accept="video/*" |
|
|
|
onChange={(e) => setVideoFile(e.target.files[0])} |
|
|
|
/> |
|
|
|
|
|
|
|
{ |
|
|
|
editMode ? ( |
|
|
|
<div className="mt-4"> |
|
|
|
<button |
|
|
|
type="button" |
|
|
|
className="bg-primary p-3 rounded-sm text-white font-montserrat text-2xl font-black cursor-pointer" |
|
|
|
onClick={(e) => {onSubmit(e)}} |
|
|
|
> |
|
|
|
Enregistrer |
|
|
|
</button> |
|
|
|
<button |
|
|
|
type="button" |
|
|
|
className="bg-red-500 p-3 rounded-sm text-white font-montserrat text-2xl font-black cursor-pointer ml-3" |
|
|
|
onClick={() => setEditMode(!editMode)} |
|
|
|
> |
|
|
|
Annuler |
|
|
|
</button> |
|
|
|
</div> |
|
|
|
) : ( |
|
|
|
<button |
|
|
|
type='button' |
|
|
|
type="button" |
|
|
|
className="bg-primary p-3 rounded-sm text-white font-montserrat text-2xl font-black cursor-pointer mt-4" |
|
|
|
onClick={() => setEditMode(!editMode)} |
|
|
|
> |
|
|
|
Modifier |
|
|
|
</button> |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
</form> |
|
|
|
|
|
|
|
@ -172,6 +365,47 @@ export default function ManageVideo() { |
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
{ /* TAGS */ } |
|
|
|
<div className="glassmorphism p-4 mt-4"> |
|
|
|
<h2 className="text-2xl font-bold text-white mb-4">Tags</h2> |
|
|
|
<div className="flex flex-wrap gap-2"> |
|
|
|
{ video && video.tags && video.tags.length > 0 ? ( |
|
|
|
video.tags.map((tag) => ( |
|
|
|
<Tag tag={tag} key={tag} onSuppress={() => onSuppressTag(tag)} /> |
|
|
|
)) |
|
|
|
) : ( |
|
|
|
<p className="text-gray-500">Aucun tag</p> |
|
|
|
)} |
|
|
|
</div> |
|
|
|
<input |
|
|
|
type="text" |
|
|
|
className="glassmorphism text-md font-normal text-white p-2 focus:text-white focus:outline-none w-full font-montserrat resizable-none mt-4" |
|
|
|
placeholder="Ajouter un tag" |
|
|
|
onKeyPress={(e) => onAddTag(e)} |
|
|
|
/> |
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
{ /* LINK */ } |
|
|
|
|
|
|
|
<div className="glassmorphism p-4 mt-4"> |
|
|
|
|
|
|
|
<h2 className="text-2xl font-bold text-white mb-4">Lien de la vidéo</h2> |
|
|
|
<p className="text-md font-normal text-white"> |
|
|
|
<a href={`/video/${id}`} target="_blank" rel="noopener noreferrer" className="text-blue-500 hover:underline"> |
|
|
|
{window.location.origin}/video/{id} |
|
|
|
</a> |
|
|
|
<button |
|
|
|
className="ml-2 bg-primary text-white p-2 rounded-sm cursor-pointer" |
|
|
|
onClick={() => { |
|
|
|
navigator.clipboard.writeText(`${window.location.origin}/video/${id}`); |
|
|
|
}} |
|
|
|
> |
|
|
|
Copier |
|
|
|
</button> |
|
|
|
</p> |
|
|
|
|
|
|
|
</div> |
|
|
|
</div> |
|
|
|
|
|
|
|
</div> |
|
|
|
|