2023-11-29 08:41:24 -06:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import TextInput from "@/components/TextInput";
|
|
|
|
import useCollectionStore from "@/store/collections";
|
2023-12-17 16:28:42 -06:00
|
|
|
import toast from "react-hot-toast";
|
2023-11-29 08:41:24 -06:00
|
|
|
import { HexColorPicker } from "react-colorful";
|
|
|
|
import { Collection } from "@prisma/client";
|
2023-12-01 16:42:45 -06:00
|
|
|
import Modal from "../Modal";
|
2024-02-06 15:46:05 -06:00
|
|
|
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
|
2024-02-22 02:24:10 -06:00
|
|
|
import useAccountStore from "@/store/account";
|
|
|
|
import { useSession } from "next-auth/react";
|
2024-06-09 08:27:16 -05:00
|
|
|
import { useTranslation } from "next-i18next";
|
2023-11-29 08:41:24 -06:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onClose: Function;
|
2024-02-06 15:46:05 -06:00
|
|
|
parent?: CollectionIncludingMembersAndLinkCount;
|
2023-11-29 08:41:24 -06:00
|
|
|
};
|
|
|
|
|
2024-02-06 15:46:05 -06:00
|
|
|
export default function NewCollectionModal({ onClose, parent }: Props) {
|
2024-06-09 08:27:16 -05:00
|
|
|
const { t } = useTranslation();
|
2023-11-29 08:41:24 -06:00
|
|
|
const initial = {
|
2024-02-06 15:46:05 -06:00
|
|
|
parentId: parent?.id,
|
2023-11-29 08:41:24 -06:00
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
color: "#0ea5e9",
|
2024-02-06 15:46:05 -06:00
|
|
|
} as Partial<Collection>;
|
2023-11-29 08:41:24 -06:00
|
|
|
|
|
|
|
const [collection, setCollection] = useState<Partial<Collection>>(initial);
|
2024-02-22 02:24:10 -06:00
|
|
|
const { setAccount } = useAccountStore();
|
|
|
|
const { data } = useSession();
|
2023-11-29 08:41:24 -06:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setCollection(initial);
|
2023-12-01 16:42:45 -06:00
|
|
|
}, []);
|
2023-11-29 08:41:24 -06:00
|
|
|
|
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
const { addCollection } = useCollectionStore();
|
|
|
|
|
|
|
|
const submit = async () => {
|
2023-12-17 18:28:52 -06:00
|
|
|
if (submitLoader) return;
|
|
|
|
if (!collection) return null;
|
2023-11-29 08:41:24 -06:00
|
|
|
|
2023-12-17 18:28:52 -06:00
|
|
|
setSubmitLoader(true);
|
2023-11-29 08:41:24 -06:00
|
|
|
|
2024-06-09 08:27:16 -05:00
|
|
|
const load = toast.loading(t("creating"));
|
2023-11-29 08:41:24 -06:00
|
|
|
|
2023-12-17 18:28:52 -06:00
|
|
|
let response = await addCollection(collection as any);
|
|
|
|
toast.dismiss(load);
|
2023-11-29 08:41:24 -06:00
|
|
|
|
2023-12-17 18:28:52 -06:00
|
|
|
if (response.ok) {
|
2024-06-09 08:27:16 -05:00
|
|
|
toast.success(t("created"));
|
2024-02-22 02:24:10 -06:00
|
|
|
if (response.data) {
|
|
|
|
setAccount(data?.user.id as number);
|
|
|
|
onClose();
|
|
|
|
}
|
2023-12-17 18:28:52 -06:00
|
|
|
} else toast.error(response.data as string);
|
2023-11-29 08:41:24 -06:00
|
|
|
|
2023-12-17 18:28:52 -06:00
|
|
|
setSubmitLoader(false);
|
2023-11-29 08:41:24 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-12-01 16:42:45 -06:00
|
|
|
<Modal toggleModal={onClose}>
|
2024-02-06 15:46:05 -06:00
|
|
|
{parent?.id ? (
|
|
|
|
<>
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="text-xl font-thin">{t("new_sub_collection")}</p>
|
|
|
|
<p className="capitalize text-sm">
|
|
|
|
{t("for_collection", { name: parent.name })}
|
|
|
|
</p>
|
2024-02-06 15:46:05 -06:00
|
|
|
</>
|
|
|
|
) : (
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="text-xl font-thin">{t("create_new_collection")}</p>
|
2024-02-06 15:46:05 -06:00
|
|
|
)}
|
2023-12-03 22:52:32 -06:00
|
|
|
|
2023-12-05 14:17:36 -06:00
|
|
|
<div className="divider mb-3 mt-1"></div>
|
2023-12-01 16:42:45 -06:00
|
|
|
|
|
|
|
<div className="flex flex-col gap-3">
|
|
|
|
<div className="flex flex-col sm:flex-row gap-3">
|
|
|
|
<div className="w-full">
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("name")}</p>
|
|
|
|
<div className="flex flex-col gap-2">
|
2023-12-01 16:42:45 -06:00
|
|
|
<TextInput
|
|
|
|
className="bg-base-200"
|
|
|
|
value={collection.name}
|
2024-06-09 08:27:16 -05:00
|
|
|
placeholder={t("collection_name_placeholder")}
|
2023-12-01 16:42:45 -06:00
|
|
|
onChange={(e) =>
|
|
|
|
setCollection({ ...collection, name: e.target.value })
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<div>
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="w-full mb-2">{t("color")}</p>
|
|
|
|
<div className="color-picker flex justify-between items-center">
|
|
|
|
<HexColorPicker
|
|
|
|
color={collection.color}
|
|
|
|
onChange={(color) =>
|
|
|
|
setCollection({ ...collection, color })
|
|
|
|
}
|
|
|
|
/>
|
2023-12-01 16:42:45 -06:00
|
|
|
<div className="flex flex-col gap-2 items-center w-32">
|
2023-12-17 22:32:33 -06:00
|
|
|
<i
|
|
|
|
className={"bi-folder-fill text-5xl"}
|
|
|
|
style={{ color: collection.color }}
|
|
|
|
></i>
|
2023-12-01 16:42:45 -06:00
|
|
|
<div
|
|
|
|
className="btn btn-ghost btn-xs"
|
|
|
|
onClick={() =>
|
|
|
|
setCollection({ ...collection, color: "#0ea5e9" })
|
2023-11-29 08:41:24 -06:00
|
|
|
}
|
2023-12-01 16:42:45 -06:00
|
|
|
>
|
2024-06-09 08:27:16 -05:00
|
|
|
{t("reset")}
|
2023-12-01 16:42:45 -06:00
|
|
|
</div>
|
2023-11-29 08:41:24 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2023-12-01 16:42:45 -06:00
|
|
|
<div className="w-full">
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("description")}</p>
|
2023-12-01 16:42:45 -06:00
|
|
|
<textarea
|
|
|
|
className="w-full h-[13rem] resize-none border rounded-md duration-100 bg-base-200 p-2 outline-none border-neutral-content focus:border-primary"
|
2024-06-09 08:27:16 -05:00
|
|
|
placeholder={t("collection_description_placeholder")}
|
2023-12-01 16:42:45 -06:00
|
|
|
value={collection.description}
|
|
|
|
onChange={(e) =>
|
2024-06-09 08:27:16 -05:00
|
|
|
setCollection({ ...collection, description: e.target.value })
|
2023-12-01 16:42:45 -06:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-11-29 08:41:24 -06:00
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
|
2023-12-04 09:24:45 -06:00
|
|
|
<button
|
2023-12-07 11:29:45 -06:00
|
|
|
className="btn btn-accent dark:border-violet-400 text-white w-fit ml-auto"
|
2023-12-04 09:24:45 -06:00
|
|
|
onClick={submit}
|
|
|
|
>
|
2024-06-09 08:27:16 -05:00
|
|
|
{t("create_collection_button")}
|
2023-12-01 16:42:45 -06:00
|
|
|
</button>
|
2023-11-29 08:41:24 -06:00
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
</Modal>
|
2023-11-29 08:41:24 -06:00
|
|
|
);
|
|
|
|
}
|