import React, { useEffect, useState } from "react"; import TextInput from "@/components/TextInput"; import { Collection } from "@prisma/client"; import Modal from "../Modal"; import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; import { useTranslation } from "next-i18next"; import { useCreateCollection } from "@/hooks/store/collections"; import toast from "react-hot-toast"; import IconPicker from "../IconPicker"; import { IconWeight } from "@phosphor-icons/react"; type Props = { onClose: Function; parent?: CollectionIncludingMembersAndLinkCount; }; export default function NewCollectionModal({ onClose, parent }: Props) { const { t } = useTranslation(); const initial = { parentId: parent?.id, name: "", description: "", color: "#0ea5e9", } as Partial; const [collection, setCollection] = useState>(initial); useEffect(() => { setCollection(initial); }, []); const [submitLoader, setSubmitLoader] = useState(false); const createCollection = useCreateCollection(); const submit = async () => { if (submitLoader) return; if (!collection) return null; setSubmitLoader(true); const load = toast.loading(t("creating")); await createCollection.mutateAsync(collection, { onSettled: (data, error) => { setSubmitLoader(false); toast.dismiss(load); if (error) { toast.error(error.message); } else { onClose(); toast.success(t("created")); } }, }); }; return ( {parent?.id ? ( <>

{t("new_sub_collection")}

{t("for_collection", { name: parent.name })}

) : (

{t("create_new_collection")}

)}
setCollection({ ...collection, color }) } weight={(collection.iconWeight || "regular") as IconWeight} setWeight={(iconWeight: string) => setCollection({ ...collection, iconWeight }) } iconName={collection.icon as string} setIconName={(icon: string) => setCollection({ ...collection, icon }) } reset={() => setCollection({ ...collection, color: "#0ea5e9", icon: "", iconWeight: "", }) } />

{t("name")}

setCollection({ ...collection, name: e.target.value }) } />

{t("description")}