el.xwx.moe/components/Modal/Collection/CollectionInfo.tsx

124 lines
4.0 KiB
TypeScript
Raw Normal View History

import { Dispatch, SetStateAction, useState } from "react";
2023-06-02 06:59:52 -05:00
import {
faFolder,
faPenToSquare,
faPlus,
} from "@fortawesome/free-solid-svg-icons";
2023-05-28 00:55:49 -05:00
import useCollectionStore from "@/store/collections";
2023-06-16 07:55:21 -05:00
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
2023-05-31 13:33:01 -05:00
import SubmitButton from "@/components/SubmitButton";
2023-06-02 06:59:52 -05:00
import { HexColorPicker } from "react-colorful";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { toast } from "react-hot-toast";
2023-08-17 15:05:44 -05:00
import TextInput from "@/components/TextInput";
2023-05-28 00:55:49 -05:00
type Props = {
toggleCollectionModal: Function;
2023-06-16 07:55:21 -05:00
setCollection: Dispatch<
SetStateAction<CollectionIncludingMembersAndLinkCount>
>;
collection: CollectionIncludingMembersAndLinkCount;
2023-11-15 22:31:13 -06:00
method: "CREATE" | "UPDATE" | "VIEW_TEAM";
2023-05-28 00:55:49 -05:00
};
export default function CollectionInfo({
toggleCollectionModal,
setCollection,
collection,
2023-05-28 00:55:49 -05:00
method,
}: Props) {
const [submitLoader, setSubmitLoader] = useState(false);
2023-05-28 00:55:49 -05:00
const { updateCollection, addCollection } = useCollectionStore();
const submit = async () => {
if (!collection) return null;
setSubmitLoader(true);
const load = toast.loading(
method === "UPDATE" ? "Applying..." : "Creating..."
);
let response;
2023-05-28 00:55:49 -05:00
if (method === "CREATE") response = await addCollection(collection);
else response = await updateCollection(collection);
toast.dismiss(load);
if (response.ok) {
toast.success(
`Collection ${method === "UPDATE" ? "Saved!" : "Created!"}`
);
toggleCollectionModal();
} else toast.error(response.data as string);
2023-05-28 00:55:49 -05:00
setSubmitLoader(false);
2023-05-28 00:55:49 -05:00
};
return (
<div className="flex flex-col gap-3 sm:w-[35rem] w-80">
<div className="flex flex-col sm:flex-row gap-3">
<div className="w-full">
2023-11-02 23:09:50 -05:00
<p className="text-black dark:text-white mb-2">Name</p>
2023-06-02 06:59:52 -05:00
<div className="flex flex-col gap-3">
2023-08-17 15:05:44 -05:00
<TextInput
2023-06-02 06:59:52 -05:00
value={collection.name}
2023-08-17 15:05:44 -05:00
placeholder="e.g. Example Collection"
2023-06-02 06:59:52 -05:00
onChange={(e) =>
setCollection({ ...collection, name: e.target.value })
}
/>
<div className="color-picker flex justify-between">
<div className="flex flex-col justify-between items-center w-32">
2023-11-02 23:09:50 -05:00
<p className="w-full text-black dark:text-white mb-2">Color</p>
2023-06-02 22:50:16 -05:00
<div style={{ color: collection.color }}>
2023-06-02 06:59:52 -05:00
<FontAwesomeIcon
icon={faFolder}
className="w-12 h-12 drop-shadow"
/>
</div>
<div
2023-08-14 22:25:25 -05:00
className="py-1 px-2 rounded-md text-xs font-semibold cursor-pointer text-black dark:text-white hover:bg-slate-200 hover:dark:bg-neutral-700 duration-100"
2023-06-02 06:59:52 -05:00
onClick={() =>
2023-06-05 04:54:43 -05:00
setCollection({ ...collection, color: "#0ea5e9" })
2023-06-02 06:59:52 -05:00
}
>
Reset
</div>
</div>
<HexColorPicker
2023-06-02 22:50:16 -05:00
color={collection.color}
2023-06-02 06:59:52 -05:00
onChange={(e) => setCollection({ ...collection, color: e })}
/>
</div>
</div>
2023-05-28 00:55:49 -05:00
</div>
<div className="w-full">
2023-11-02 23:09:50 -05:00
<p className="text-black dark:text-white mb-2">Description</p>
2023-05-28 00:55:49 -05:00
<textarea
2023-08-22 17:34:46 -05:00
className="w-full h-[11.4rem] resize-none border rounded-md duration-100 bg-gray-50 dark:bg-neutral-950 p-2 outline-none border-sky-100 dark:border-neutral-700 focus:border-sky-300 dark:focus:border-sky-600"
2023-05-28 00:55:49 -05:00
placeholder="The purpose of this Collection..."
value={collection.description}
onChange={(e) =>
setCollection({
...collection,
description: e.target.value,
})
}
/>
</div>
</div>
2023-05-31 13:33:01 -05:00
<SubmitButton
onClick={submit}
loading={submitLoader}
label={method === "CREATE" ? "Add" : "Save"}
2023-05-31 13:33:01 -05:00
icon={method === "CREATE" ? faPlus : faPenToSquare}
className="mx-auto mt-2"
/>
2023-05-28 00:55:49 -05:00
</div>
);
}