"use client"; import IconTrashLines from "@/components/icon/icon-trash-lines"; import dynamic from "next/dynamic"; import React, { useMemo, useState } from "react"; import "react-quill/dist/quill.snow.css"; const ReactQuill = dynamic(() => import("react-quill"), { ssr: false, }); const formats = [ "header", "bold", "italic", "underline", "strike", "blockquote", "code-block", "list", "bullet", "align", "link", "image", "video", ]; const PostForm = () => { const [formData, setFormData] = useState({ title: "", slug: "", coverImage: null as File | null, description: "", }); const modules = useMemo( () => ({ toolbar: { container: [ [{ header: [1, 2, 3, false] }], ["bold", "italic", "underline", "strike"], [{ list: "ordered" }, { list: "bullet" }], ["blockquote", "code-block"], [{ align: [] }], ["link", "image", "video"], ["clean"], ], handlers: { image: function (this: any) { const input = document.createElement("input"); input.setAttribute("type", "file"); input.setAttribute("accept", "image/*"); input.click(); input.onchange = async () => { const file = input.files?.[0]; if (file) { const reader = new FileReader(); reader.onload = () => { const quill = this.quill; const range = quill.getSelection(true); quill.insertEmbed( range.index, "image", reader.result ); }; reader.readAsDataURL(file); } }; }, }, }, }), [] ); const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value, })); }; const handleImageChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0] || null; setFormData((prev) => ({ ...prev, coverImage: file, })); }; const handleRemoveImage = () => { setFormData((prev) => ({ ...prev, coverImage: null, })); }; const handleDescriptionChange = (value: string) => { setFormData((prev) => ({ ...prev, description: value, })); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const data = new FormData(); data.append("title", formData.title); data.append("slug", formData.slug); if (formData.coverImage) { data.append("coverImage", formData.coverImage); } data.append("description", formData.description); console.log("FormData prepared:", { title: formData.title, slug: formData.slug, coverImage: formData.coverImage?.name, description: formData.description, }); }; return (

Create Blog

{formData.coverImage && (

Preview:

Selected
)}
); }; export default PostForm;