`import React, { useState, useEffect } from "react"; import { toast, Toaster } from "react-hot-toast"; import Markdown from "markdown-to-jsx"; import { FileUploaderRegular } from '@uploadcare/react-uploader'; import '@uploadcare/react-uploader/core.css'; const Submit = () => { const [isLoading, setIsLoading] = useState(false); const [newPost, setNewPost] = useState({ title: "", content: "", coterie: "" }); const [imageFiles, setImageFiles] = useState([]); // Store image objects with UUIDs and URLs const [coteries, setCoteries] = useState([]); useEffect(() => { const fetchCoteries = async () => { try { const userId = localStorage.getItem("user_id"); const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/user/${userId}/coteries`); const data = await response.json(); if (response.ok) { setCoteries(data); } else { toast.error("Failed to fetch coteries", { icon: "❌" }); } } catch (error) { toast.error("Error fetching coteries", { icon: "❌" }); } }; fetchCoteries(); }, []); const handleInputChange = (e) => { const { name, value } = e.target; setNewPost((prev) => ({ ...prev, [name]: value })); }; const handleImageUpload = (fileInfo) => { if (fileInfo.uuid) { const imageUrl = `https://ucarecdn.com/${fileInfo.uuid}/-/preview/750x1000/`; setImageFiles((prev) => [...prev, { uuid: fileInfo.uuid, cdnUrl: imageUrl }]); toast.success("Image uploaded successfully"); } else { toast.error("Image upload failed: No UUID received"); } }; const handleSubmit = async (e) => { e.preventDefault(); if (!newPost.title || !newPost.content) { toast.error("Title and content are required", { icon: "❌", style: { borderRadius: "5px", background: "#333", color: "#ceaaaa", }, }); return; } setIsLoading(true); try { let uploadedImageUrls = ""; if (imageFiles.length > 0) { uploadedImageUrls = imageFiles.map(file => file.cdnUrl).join(","); // Join CDN URLs setImageFiles([]); // Clear file list after submission } const userId = localStorage.getItem("user_id"); let url = `${process.env.NEXT_PUBLIC_API_URL}/post/add?title=${encodeURIComponent(newPost.title)}&content=${encodeURIComponent(newPost.content)}&userId=${encodeURIComponent(userId)}&image=${encodeURIComponent(uploadedImageUrls)}`; if (newPost.coterie) { url += `&coterie=${encodeURIComponent(newPost.coterie)}`; } const response = await fetch(url, { method: "POST", credentials: "include", }); if (response.ok) { setNewPost({ title: "", content: "", coterie: "" }); toast.success("Post created successfully", { icon: "😊", style: { borderRadius: "5px", background: "var(--primary-color)", color: "#ffffff", }, }); } else { toast.error("Failed to create post", { icon: "❌", style: { borderRadius: "5px", background: "var(--primary-color)", color: "#ceaaaa", }, }); } } catch (error) { toast.error("Error creating post", { icon: "❌", style: { borderRadius: "5px", background: "var(--primary-color)", color: "#ceaaaa", }, }); } finally { setIsLoading(false); } }; return (