import ChunkedUploaderInput from '@/components/chunked-uploader-input'; import LoadingButton from '@/components/loading-button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { useLang } from '@/hooks/use-lang'; import { onHandleChange } from '@/lib/inertia'; import { generatePropertyFields } from '@/lib/page'; import { useForm } from '@inertiajs/react'; import { useEffect, useState } from 'react'; import { useSectionEditor } from './context'; import Fields from './fields'; const EditForm = () => { const { input, button, frontend } = useLang(); const { section, setOpen, isSubmit, setIsSubmit } = useSectionEditor(); const [isFileSelected, setIsFileSelected] = useState(false); const [isFileUploaded, setIsFileUploaded] = useState(false); const [thumbnailPreview, setThumbnailPreview] = useState(section.thumbnail || null); const [backgroundPreview, setBackgroundPreview] = useState(section.background_image || null); const { data, setData, post, reset, processing, errors } = useForm({ title: section.title, sub_title: section.sub_title, description: section.description || '', thumbnail: null, video_url: section.video_url, background_image: null, background_color: section.background_color, properties: section.properties, active: section.active, sort: section.sort, }); // Remove the array empty elements first // data.properties = { ...data.properties, array: removeEmptyArrayItems(data.properties.array) }; const properties = generatePropertyFields(data.properties); const [propertyFields, setPropertyFields] = useState(properties); useEffect(() => { setPropertyFields(generatePropertyFields(section.properties)); }, [section]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (isFileSelected) { setIsSubmit(true); return; } submitForm(); }; const submitForm = () => { post(route('page.section.update', section.id), { onSuccess: () => { reset(); setOpen(false); setIsSubmit(false); }, }); }; useEffect(() => { if (data.video_url && isFileUploaded) { submitForm(); reset('video_url'); setIsFileUploaded(false); } }, [data.video_url]); const handlePropertyChange = (name: string, value: any) => { setData((data) => ({ ...data, properties: { ...data.properties, [name]: value, }, })); }; useEffect(() => { if (!open) { reset(); } }, [open]); return (
{/* Basic section fields */} {section.flags.title && (
onHandleChange(e, setData)} placeholder={input.title_placeholder} /> {errors.title &&

{errors.title}

}
)} {section.flags.sub_title && (
onHandleChange(e, setData)} placeholder={input.title_placeholder} /> {errors.sub_title &&

{errors.sub_title}

}
)} {section.flags.description && (