diff --git a/components/Layouts/MainLayout.tsx b/Layouts/MainLayout.tsx similarity index 92% rename from components/Layouts/MainLayout.tsx rename to Layouts/MainLayout.tsx index ed4fea2..57ffbf4 100644 --- a/components/Layouts/MainLayout.tsx +++ b/Layouts/MainLayout.tsx @@ -3,15 +3,15 @@ import Navbar from "@/components/Navbar"; import Sidebar from "@/components/Sidebar"; import { ReactNode } from "react"; import { useSession } from "next-auth/react"; -import Loader from "../Loader"; +import Loader from "../components/Loader"; import useRedirection from "@/hooks/useRedirection"; import { useRouter } from "next/router"; -interface LayoutProps { +interface Props { children: ReactNode; } -export default function Layout({ children }: LayoutProps) { +export default function Layout({ children }: Props) { const { status } = useSession(); const router = useRouter(); diff --git a/components/ClickAwayHandler.tsx b/components/ClickAwayHandler.tsx new file mode 100644 index 0000000..dec4c93 --- /dev/null +++ b/components/ClickAwayHandler.tsx @@ -0,0 +1,35 @@ +import React, { useRef, useEffect, ReactNode, RefObject } from "react"; + +interface Props { + children: ReactNode; + onClickOutside: Function; +} + +function useOutsideAlerter( + ref: RefObject, + onClickOutside: Function +) { + useEffect(() => { + function handleClickOutside(event: Event) { + if ( + ref.current && + !ref.current.contains(event.target as HTMLInputElement) + ) { + onClickOutside(); + } + } + // Bind the event listener + document.addEventListener("mousedown", handleClickOutside); + return () => { + // Unbind the event listener on clean up + document.removeEventListener("mousedown", handleClickOutside); + }; + }, [ref, onClickOutside]); +} + +export default function OutsideAlerter({ children, onClickOutside }: Props) { + const wrapperRef = useRef(null); + useOutsideAlerter(wrapperRef, onClickOutside); + + return
{children}
; +} diff --git a/components/Collections.tsx b/components/Collections.tsx index a1b936a..99e1349 100644 --- a/components/Collections.tsx +++ b/components/Collections.tsx @@ -18,7 +18,7 @@ export default function Collections() {
{collections.map((e, i) => { return ( -
+

{e.name}

); diff --git a/components/Sidebar.tsx b/components/Sidebar.tsx index 54f073e..36caab4 100644 --- a/components/Sidebar.tsx +++ b/components/Sidebar.tsx @@ -1,13 +1,60 @@ import { useSession } from "next-auth/react"; +import ClickAwayHandler from "@/components/ClickAwayHandler"; +import { useState } from "react"; export default function Sidebar() { const { data: session, status } = useSession(); + const [addCollection, setAddCollection] = useState(false); + const user = session?.user; + const toggleAddCollection = () => { + setAddCollection(!addCollection); + }; + + const submitCollection = async ( + event: React.KeyboardEvent + ) => { + const collectionName: string = (event.target as HTMLInputElement).value; + + if (event.key === "Enter" && collectionName) { + await fetch("/api/routes/collections/postCollection", { + body: JSON.stringify({ collectionName }), + headers: { + "Content-Type": "application/json", + }, + method: "POST", + }) + .then((res) => res.json()) + .then((data) => console.log(data)); + } + }; + return ( -
-

{user?.name}

+
+
+

{user?.name}

+ + {addCollection ? ( + + + + ) : ( +
+ Create Collection + +
+ )} +
); } diff --git a/pages/_app.tsx b/pages/_app.tsx index 4101ff0..70ecb53 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -1,4 +1,4 @@ -import MainLayout from "@/components/Layouts/MainLayout"; +import MainLayout from "@/Layouts/MainLayout"; import "@/styles/globals.css"; import { SessionProvider } from "next-auth/react"; import type { AppProps } from "next/app"; diff --git a/pages/api/auth/register.ts b/pages/api/auth/register.ts index 535429d..4d7dab6 100644 --- a/pages/api/auth/register.ts +++ b/pages/api/auth/register.ts @@ -18,6 +18,9 @@ export default async function handler( ) { const body: User = req.body; + if (!body.email || !body.password || !body.name) + return res.status(400).json({ message: "Please fill out all the fields." }); + const checkIfUserExists = await prisma.user.findFirst({ where: { email: body.email, @@ -50,7 +53,8 @@ export default async function handler( }); res.status(201).json({ message: "User successfully created." }); - } else { + } else if (checkIfUserExists) { + console.log(checkIfUserExists); res.status(400).json({ message: "User already exists." }); } }