"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 ( {pending ? "Creating..." : "Create Post"} ); } 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 const getPreviousInput = ( key: keyof NonNullable ) => state.previousInput?.[key] ?? ""; return ( // Remove encType="multipart/form-data" {/* Title */} Title {state.errors?.title && state.errors.title.map((error: string) => ( {error} ))} {/* Slug */} Slug (URL Path) 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) */} Content (Markdown supported) {state.errors?.content && state.errors.content.map((error: string) => ( {error} ))} {/* Excerpt (Optional) */} Excerpt (Optional short summary) {state.errors?.excerpt && state.errors.excerpt.map((error: string) => ( {error} ))} {/* Image Upload (File Input) */} Featured Image (Optional, Max 5MB) {state.errors?.image && state.errors.image.map((error: string) => ( {error} ))} {/* Tags Input (Optional) */} Tags (Optional, comma-separated) {state.errors?.tags && state.errors.tags.map((error: string) => ( {error} ))} {/* Published Checkbox */} Publish immediately {/* 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 */} ); }
{error}
Lowercase letters, numbers, and hyphens only (e.g., my-cool-post).
{state.message}