el.xwx.moe/components/ModalContent/NewCollectionModal.tsx

116 lines
3.5 KiB
TypeScript
Raw Normal View History

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";
2023-11-29 08:41:24 -06:00
type Props = {
onClose: Function;
};
2023-12-01 16:42:45 -06:00
export default function NewCollectionModal({ onClose }: Props) {
2023-11-29 08:41:24 -06:00
const initial = {
name: "",
description: "",
color: "#0ea5e9",
};
const [collection, setCollection] = useState<Partial<Collection>>(initial);
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 () => {
if (submitLoader) return;
if (!collection) return null;
2023-11-29 08:41:24 -06:00
setSubmitLoader(true);
2023-11-29 08:41:24 -06:00
const load = toast.loading("Creating...");
2023-11-29 08:41:24 -06:00
let response = await addCollection(collection as any);
toast.dismiss(load);
2023-11-29 08:41:24 -06:00
if (response.ok) {
toast.success("Created!");
onClose();
} else toast.error(response.data as string);
2023-11-29 08:41:24 -06:00
setSubmitLoader(false);
2023-11-29 08:41:24 -06:00
};
return (
2023-12-01 16:42:45 -06:00
<Modal toggleModal={onClose}>
<p className="text-xl font-thin">Create a New Collection</p>
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">
<p className="mb-2">Name</p>
<div className="flex flex-col gap-3">
<TextInput
className="bg-base-200"
value={collection.name}
placeholder="e.g. Example Collection"
onChange={(e) =>
setCollection({ ...collection, name: e.target.value })
}
/>
<div>
<p className="w-full mb-2">Color</p>
<div className="color-picker flex justify-between">
<div className="flex flex-col gap-2 items-center w-32">
<i className={"bi-folder2 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
>
Reset
</div>
2023-11-29 08:41:24 -06:00
</div>
2023-12-01 16:42:45 -06:00
<HexColorPicker
color={collection.color}
onChange={(e) => setCollection({ ...collection, color: e })}
/>
2023-11-29 08:41:24 -06:00
</div>
</div>
</div>
</div>
2023-12-01 16:42:45 -06:00
<div className="w-full">
<p className="mb-2">Description</p>
<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"
placeholder="The purpose of this Collection..."
value={collection.description}
onChange={(e) =>
setCollection({
...collection,
description: e.target.value,
})
}
/>
</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}
>
2023-12-01 16:42:45 -06:00
Create Collection
</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
);
}