import { useState } from "react"; import { useTranslation } from "../utils/i18n"; import { MdOutlineImage, MdOutlineInfo } from "react-icons/md"; import showToast from "../components/toast"; import { network } from "../network/network"; import { useNavigate } from "react-router"; export default function CreateCollectionPage() { const [title, setTitle] = useState(""); const [article, setArticle] = useState(""); const [isLoading, setLoading] = useState(false); const [isUploadingimage, setUploadingImage] = useState(false); const { t } = useTranslation(); const navigate = useNavigate(); const handleAddImage = () => { if (isUploadingimage) { return; } const input = document.createElement("input"); input.type = "file"; input.accept = "image/*"; input.multiple = true; input.onchange = async (e) => { const files = (e.target as HTMLInputElement).files; if (files) { for (let i = 0; i < files.length; i++) { if (files[i].size > 8 * 1024 * 1024) { showToast({ message: t("Image size exceeds 5MB limit"), type: "error", }); return; } } setUploadingImage(true); const imageIds: number[] = []; for (let i = 0; i < files.length; i++) { const file = files[i]; const res = await network.uploadImage(file); if (res.success) { imageIds.push(res.data!); } else { showToast({ message: res.message, type: "error" }); setUploadingImage(false); return; } } if (imageIds.length > 0) { setArticle((prev) => { return ( prev + "\n" + imageIds.map((id) => `![Image](/api/image/${id})`).join(" ") ); }); } setUploadingImage(false); } }; input.click(); }; const createCollection = async () => { if (isLoading) { return; } if (title.trim() === "" || article.trim() === "") { showToast({ message: t("Title and description cannot be empty"), type: "error", }); return; } setLoading(true); const res = await network.createCollection(title, article); if (res.success) { showToast({ message: t("Collection created successfully"), type: "success", }); navigate(`/collection/${res.data?.id}`, { replace: true }); } else { showToast({ message: res.message, type: "error" }); setLoading(false); } }; return (

{t("Create Collection")}

setTitle(e.target.value)} className="input mt-1 w-full" />