⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
Added clickAwayHandler + POST functionality + Bug fixed.
Danielcefeb5e7a982
diff --git a/components/Layouts/MainLayout.tsx b/Layouts/MainLayout.tsxsimilarity index 92%rename from components/Layouts/MainLayout.tsxrename to Layouts/MainLayout.tsxindex ed4fea26..57ffbf4d 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.tsxnew file mode 100644index 00000000..dec4c934--- /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<HTMLElement>,+ 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 <div ref={wrapperRef}>{children}</div>;+}diff --git a/components/Collections.tsx b/components/Collections.tsxindex a1b936a2..99e13498 100644--- a/components/Collections.tsx+++ b/components/Collections.tsx@@ -18,7 +18,7 @@ export default function Collections() {<div className="flex flex-wrap">{collections.map((e, i) => {return (- <div className="p-5 bg-gray-100 m-2 w-max " key={i}>+ <div className="p-5 bg-gray-200 m-2 w-max rounded" key={i}><p>{e.name}</p></div>);diff --git a/components/Sidebar.tsx b/components/Sidebar.tsxindex 54f073e0..36caab4b 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<HTMLInputElement>+ ) => {+ 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 (- <div className="fixed bg-gray-100 top-0 bottom-0 left-0 w-80">- <p>{user?.name}</p>+ <div className="fixed bg-gray-200 top-0 bottom-0 left-0 w-80 p-5">+ <div className="flex justify-between gap-5 items-center h-9">+ <p>{user?.name}</p>++ {addCollection ? (+ <ClickAwayHandler onClickOutside={toggleAddCollection}>+ <input+ type="text"+ placeholder="Enter Collection Name"+ className="w-48 rounded p-2"+ onKeyDown={submitCollection}+ autoFocus+ />+ </ClickAwayHandler>+ ) : (+ <div+ onClick={toggleAddCollection}+ className="select-none cursor-pointer bg-white rounded p-2"+ >+ Create Collection ++ </div>+ )}+ </div></div>);}diff --git a/pages/_app.tsx b/pages/_app.tsxindex 4101ff05..70ecb537 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.tsindex 535429d6..4d7dab67 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." });}}