'use client'; import { useState } from 'react'; import { ArrowLeft, Save, Eye } from 'lucide-react'; import { Button } from '@/components/ui/button'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { DynamicFieldRenderer } from '../DynamicFieldRenderer'; interface ContentType { id: string; name: string; apiIdentifier: string; description?: string | null; fields: Field[]; } interface Field { id: string; label: string; apiIdentifier: string; type: string; isRequired: boolean; } interface CreateContentEntryPageProps { contentType: ContentType; } export function CreateContentEntryPage({ contentType }: CreateContentEntryPageProps) { const [formData, setFormData] = useState>({}); const [status, setStatus] = useState('draft'); const [isSaving, setIsSaving] = useState(false); const router = useRouter(); const handleFieldChange = (fieldId: string, value: any) => { setFormData(prev => ({ ...prev, [fieldId]: value })); }; const handleSave = async (saveStatus: string = status) => { setIsSaving(true); try { const response = await fetch(`/api/content-types/${contentType.apiIdentifier}/entries`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ data: formData, status: saveStatus }), }); if (response.ok) { const result = await response.json(); router.push(`/admin/dashboard/content-types/${contentType.apiIdentifier}/content`); } else { throw new Error('Error al guardar la entrada'); } } catch (error) { console.error('Error saving entry:', error); alert('Error al guardar la entrada'); } finally { setIsSaving(false); } }; const validateForm = () => { const requiredFields = contentType.fields.filter(field => field.isRequired); return requiredFields.every(field => { const value = formData[field.apiIdentifier]; return value !== undefined && value !== null && value !== ''; }); }; const isFormValid = validateForm(); return (
{/* Header */}

Crear {contentType.name}

Completa los campos para crear una nueva entrada

{/* Actions */}
{/* Form */}
{contentType.fields.map(field => (
handleFieldChange(field.apiIdentifier, value)} /> {field.isRequired && !formData[field.apiIdentifier] && (

Este campo es obligatorio

)}
))}
); }