import React, { useState, useEffect } from 'react'; import axios from 'axios'; import { useRouter } from 'next/router'; import { InputText } from 'primereact/inputtext'; import { InputTextarea } from 'primereact/inputtextarea'; import { InputNumber } from 'primereact/inputnumber'; import { InputSwitch } from 'primereact/inputswitch'; import GenericButton from '@/components/buttons/GenericButton'; import { useToast } from '@/hooks/useToast'; import { useSession } from 'next-auth/react'; import 'primeicons/primeicons.css'; import { Tooltip } from 'primereact/tooltip'; import 'primereact/resources/primereact.min.css'; // todo need to handle case where published video is being edited and not just draft const VideoForm = ({ draft = null }) => { const [title, setTitle] = useState(draft?.title || ''); const [summary, setSummary] = useState(draft?.summary || ''); const [price, setPrice] = useState(draft?.price || 0); const [isPaidResource, setIsPaidResource] = useState(draft?.price ? true : false); const [videoUrl, setVideoUrl] = useState(draft?.content || ''); const [coverImage, setCoverImage] = useState(draft?.image || ''); const [topics, setTopics] = useState(draft?.topics || ['']); const [additionalLinks, setAdditionalLinks] = useState(draft?.additionalLinks || ['']); const router = useRouter(); const { data: session, status } = useSession(); const [user, setUser] = useState(null); const { showToast } = useToast(); useEffect(() => { if (session) { setUser(session.user); } }, [session]); useEffect(() => { if (draft) { setTitle(draft.title); setSummary(draft.summary); setPrice(draft.price || 0); setIsPaidResource(draft.price ? true : false); setVideoUrl(draft.content); setCoverImage(draft.image); setTopics(draft.topics || ['']); setAdditionalLinks(draft.additionalLinks || ['']); } }, [draft]); const handleSubmit = async (e) => { e.preventDefault(); let embedCode = ''; // Check if it's a YouTube video if (videoUrl.includes('youtube.com') || videoUrl.includes('youtu.be')) { const videoId = videoUrl.split('v=')[1] || videoUrl.split('/').pop(); embedCode = `
`; } // Check if it's a Vimeo video else if (videoUrl.includes('vimeo.com')) { const videoId = videoUrl.split('/').pop(); embedCode = `
`; } else if (videoUrl.includes('.mp4') || videoUrl.includes('.mov') || videoUrl.includes('.avi') || videoUrl.includes('.wmv') || videoUrl.includes('.flv') || videoUrl.includes('.webm')) { const baseUrl = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3000"; const videoEmbed = `
`; embedCode = videoEmbed; } // Add more conditions here for other video services const userResponse = await axios.get(`/api/users/${user.pubkey}`); if (!userResponse.data) { showToast('error', 'Error', 'User not found', 'Please try again.'); return; } const payload = { title, summary, type: 'video', price: isPaidResource ? price : null, content: embedCode, image: coverImage, user: userResponse.data.id, topics: [...new Set([...topics.map(topic => topic.trim().toLowerCase()), 'video'])], additionalLinks: additionalLinks.filter(link => link.trim() !== ''), }; if (payload && payload.user) { const url = draft ? `/api/drafts/${draft.id}` : '/api/drafts'; const method = draft ? 'put' : 'post'; axios[method](url, payload) .then(response => { if (response.status === 200 || response.status === 201) { showToast('success', 'Success', draft ? 'Video updated successfully.' : 'Video saved as draft.'); if (response.data?.id) { router.push(`/draft/${response.data.id}`); } } }) .catch(error => { console.error(error); showToast('error', 'Error', 'Failed to save video. Please try again.'); }); } }; const handleTopicChange = (index, value) => { const updatedTopics = topics.map((topic, i) => i === index ? value : topic); setTopics(updatedTopics); }; const addTopic = (e) => { e.preventDefault(); setTopics([...topics, '']); // Add an empty string to the topics array }; const removeTopic = (e, index) => { e.preventDefault(); const updatedTopics = topics.filter((_, i) => i !== index); setTopics(updatedTopics); }; const handleLinkChange = (index, value) => { const updatedLinks = additionalLinks.map((link, i) => i === index ? value : link); setAdditionalLinks(updatedLinks); }; const addLink = (e) => { e.preventDefault(); setAdditionalLinks([...additionalLinks, '']); }; const removeLink = (e, index) => { e.preventDefault(); const updatedLinks = additionalLinks.filter((_, i) => i !== index); setAdditionalLinks(updatedLinks); }; return (
setTitle(e.target.value)} placeholder="Title" />
setSummary(e.target.value)} placeholder="Summary" rows={5} cols={30} />

Paid Video

setIsPaidResource(e.value)} /> {isPaidResource && (
setPrice(e.value)} placeholder="Price (sats)" />
)}
setVideoUrl(e.target.value)} placeholder="Video URL" />
setCoverImage(e.target.value)} placeholder="Cover Image URL" />
External Links {additionalLinks.map((link, index) => (
handleLinkChange(index, e.target.value)} placeholder="https://example.com" className="w-full mt-2" /> {index > 0 && ( removeLink(e, index)} /> )}
))}
{topics.map((topic, index) => (
handleTopicChange(index, e.target.value)} placeholder="Topic" className="w-full mt-2" /> {index > 0 && ( removeTopic(e, index)} /> )}
))}
); } export default VideoForm;