'use client'; import CreateEditor from '@/app/admin/(dashboard)/_com/createEditor'; import stateGallery from '@/app/admin/(dashboard)/_state/desa/gallery'; import colors from '@/con/colors'; import { ActionIcon, Box, Button, Group, Paper, Stack, Text, TextInput, Title, Loader } from '@mantine/core'; import { IconArrowBack, IconX } from '@tabler/icons-react'; import { useRouter } from 'next/navigation'; import { useState } from 'react'; import { toast } from 'react-toastify'; import { useProxy } from 'valtio/utils'; import { convertYoutubeUrlToEmbed } from '../../lib/youtube-utils'; function CreateVideo() { const videoState = useProxy(stateGallery.video); const router = useRouter(); const [link, setLink] = useState(''); const embedLink = convertYoutubeUrlToEmbed(link); const [isSubmitting, setIsSubmitting] = useState(false); // Helper function to check if HTML content is empty const isHtmlEmpty = (html: string) => { // Remove all HTML tags and check if there's any text content const textContent = html.replace(/<[^>]*>/g, '').trim(); return textContent === ''; }; // Check if form is valid const isFormValid = () => { return ( videoState.create.form.name?.trim() !== '' && link.trim() !== '' && !isHtmlEmpty(videoState.create.form.deskripsi) && embedLink !== null // Make sure the embed link is valid ); }; const resetForm = () => { videoState.create.form = { name: '', deskripsi: '', linkVideo: '', }; setLink(''); }; const handleSubmit = async () => { if (!videoState.create.form.name?.trim()) { toast.error('Judul wajib diisi'); return; } if (!link.trim()) { toast.error('Link YouTube wajib diisi'); return; } if (isHtmlEmpty(videoState.create.form.deskripsi)) { toast.error('Deskripsi wajib diisi'); return; } if (!embedLink) { toast.error('Link YouTube tidak valid. Pastikan formatnya benar.'); return; } try { setIsSubmitting(true); if (!embedLink) { toast.error('Link YouTube tidak valid. Pastikan formatnya benar.'); return; } videoState.create.form.linkVideo = embedLink; await videoState.create.create(); resetForm(); router.push('/admin/desa/gallery/video'); } catch (error) { console.error("Error creating video:", error); toast.error("Terjadi kesalahan saat menambahkan video"); } finally { setIsSubmitting(false); } }; return ( {/* Header Back Button + Title */} Tambah Video {/* Card Form */} {/* Judul */} { videoState.create.form.name = e.currentTarget.value; }} required /> {/* Link YouTube */} setLink(e.currentTarget.value)} required /> {/* Preview Video */} {embedLink && (