"use client"; // Import useActionState from react instead of useFormState from react-dom import React, { useActionState } from "react"; import { useFormStatus } from "react-dom"; // Keep this for useFormStatus import { createPost, CreatePostState } from "@/actions/posts"; import Button from "@/components/ui/Button"; function SubmitButton() { const { pending } = useFormStatus(); return ( ); } export default function CreatePostForm() { const initialState: CreatePostState = { message: null, errors: {} }; // Use useActionState from React const [state, dispatch] = useActionState(createPost, initialState); // Helper to get default value or empty string, ensuring string return type const getPreviousInput = ( key: keyof NonNullable ): string => { const value = state.previousInput?.[key]; // Ensure the return value is always a string for text/textarea defaultValue if (typeof value === "string") { return value; } // Return empty string for non-string types (like boolean) or null/undefined return ""; }; return ( // Remove encType="multipart/form-data"
{/* Title */}
{state.errors?.title && state.errors.title.map((error: string) => (

{error}

))}
{/* Slug */}

Lowercase letters, numbers, and hyphens only (e.g., my-cool-post).

{state.errors?.slug && state.errors.slug.map((error: string) => (

{error}

))}
{/* Content (Textarea - consider a Markdown editor later) */}
{state.errors?.content && state.errors.content.map((error: string) => (

{error}

))}
{/* Excerpt (Optional) */}
{state.errors?.excerpt && state.errors.excerpt.map((error: string) => (

{error}

))}
{/* Image Upload (File Input) */}
{state.errors?.image && state.errors.image.map((error: string) => (

{error}

))}
{/* Tags Input (Optional) */}
{state.errors?.tags && state.errors.tags.map((error: string) => (

{error}

))}
{/* Published Checkbox */}
{/* General Form Error */}
{state.errors?._form && state.errors._form.map((error: string) => (

{error}

))} {state.message && !state.errors?._form && ( // Display general message if no specific form error

{state.message}

)}
{/* Submit Button */} ); }