From 692b9b99e71bc95fdc19def15788a0aecf7b9632 Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Wed, 29 Nov 2023 00:46:31 -0500 Subject: [PATCH] implemented new modal --- components/Modals/New.tsx | 55 +++++++-- components/Modals/Tabs.tsx/NewCollection.tsx | 120 ++++++++++++++++++- components/Modals/Tabs.tsx/NewLink.tsx | 72 +++++------ components/Navbar.tsx | 14 ++- pages/collections/index.tsx | 18 +-- 5 files changed, 216 insertions(+), 63 deletions(-) diff --git a/components/Modals/New.tsx b/components/Modals/New.tsx index 5305753..36be204 100644 --- a/components/Modals/New.tsx +++ b/components/Modals/New.tsx @@ -1,12 +1,40 @@ import { Tab } from "@headlessui/react"; -import React from "react"; +import React, { useEffect, useState } from "react"; import { Toaster } from "react-hot-toast"; import NewLink from "./Tabs.tsx/NewLink"; import NewCollection from "./Tabs.tsx/NewCollection"; -export default function New() { +type Props = { + index?: number; + modalId: string; + isOpen: boolean; + onClose: Function; +}; + +export default function New({ index, modalId, isOpen, onClose }: Props) { + const newModal = document.getElementById(modalId); + const [tabIndex, setTabIndex] = useState(index); + + useEffect(() => { + setTabIndex(index); + + newModal?.addEventListener("close", () => { + onClose(); + }); + + return () => { + newModal?.addEventListener("close", () => { + onClose(); + }); + }; + }, [isOpen]); + return ( - + - + setTabIndex(i)} + selectedIndex={tabIndex} + > @@ -42,6 +73,15 @@ export default function New() { > File */} + {/* + `${ + selected ? "btn-primary" : "btn-ghost" + } outline-none rounded-md btn btn-sm w-24` + } + > + Note + */} `${ @@ -54,11 +94,12 @@ export default function New() { - + - {/* Content 2 */} + {/* File */} + {/* Note */} - + diff --git a/components/Modals/Tabs.tsx/NewCollection.tsx b/components/Modals/Tabs.tsx/NewCollection.tsx index d539c3d..8cdccd0 100644 --- a/components/Modals/Tabs.tsx/NewCollection.tsx +++ b/components/Modals/Tabs.tsx/NewCollection.tsx @@ -1,5 +1,119 @@ -import React from "react"; +import { useEffect, useState } from "react"; +import { faFolder } from "@fortawesome/free-solid-svg-icons"; +import useCollectionStore from "@/store/collections"; +import { HexColorPicker } from "react-colorful"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { toast } from "react-hot-toast"; +import TextInput from "@/components/TextInput"; +import { useRouter } from "next/router"; +import { Collection } from "@prisma/client"; -export default function NewCollection() { - return
NewCollection
; +type Props = { + isOpen: boolean; + modalId: string; +}; + +export default function NewCollection({ isOpen, modalId }: Props) { + const router = useRouter(); + + const initial = { + name: "", + description: "", + color: "#0ea5e9", + }; + + const [collection, setCollection] = useState>(initial); + + useEffect(() => { + setCollection(initial); + }, [isOpen]); + + const [submitLoader, setSubmitLoader] = useState(false); + const { addCollection } = useCollectionStore(); + + const submit = async () => { + if (!submitLoader) { + setSubmitLoader(true); + if (!collection) return null; + + setSubmitLoader(true); + + const load = toast.loading("Creating..."); + + let response; + + response = await addCollection(collection as any); + + toast.dismiss(load); + + if (response.ok) { + toast.success(`Collection "Created!"`); + (document.getElementById(modalId) as any).close(); + } else toast.error(response.data as string); + + setSubmitLoader(false); + } + }; + + return ( +
+
+
+

Name

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

Color

+
+ +
+
+ setCollection({ ...collection, color: "#0ea5e9" }) + } + > + Reset +
+
+ setCollection({ ...collection, color: e })} + /> +
+
+
+ +
+

Description

+