Many improvements.

This commit is contained in:
Daniel 2023-03-06 00:33:20 +03:30
parent f3e104aafe
commit cff10fa9b6
19 changed files with 314 additions and 79 deletions

View File

@ -3,26 +3,22 @@ import CollectionSelection from "./InputSelect/CollectionSelection";
import TagSelection from "./InputSelect/TagSelection"; import TagSelection from "./InputSelect/TagSelection";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faPlus } from "@fortawesome/free-solid-svg-icons"; import { faPlus } from "@fortawesome/free-solid-svg-icons";
interface NewLink { import { useRouter } from "next/router";
name: string; import { NewLink } from "@/types/global";
url: string; import useLinkSlice from "@/store/links";
tags: string[];
collectionId:
| {
id: string | number;
isNew: boolean | undefined;
}
| object;
}
export default function () { export default function () {
const router = useRouter();
const [newLink, setNewLink] = useState<NewLink>({ const [newLink, setNewLink] = useState<NewLink>({
name: "", name: "",
url: "", url: "",
tags: [], tags: [],
collectionId: {}, collectionId: { id: Number(router.query.id) },
}); });
const { addLink } = useLinkSlice();
const setTags = (e: any) => { const setTags = (e: any) => {
const tagNames = e.map((e: any) => { const tagNames = e.map((e: any) => {
return e.label; return e.label;
@ -54,7 +50,7 @@ export default function () {
}; };
return ( return (
<div className="border-sky-100 border-solid border rounded-md shadow-lg p-5 bg-white flex flex-col gap-3"> <div className="slide-up border-sky-100 border-solid border rounded-md shadow-lg p-5 bg-white flex flex-col gap-3">
<p className="font-bold text-sky-300 mb-2 text-center">New Link</p> <p className="font-bold text-sky-300 mb-2 text-center">New Link</p>
<div className="flex gap-5 items-center justify-between"> <div className="flex gap-5 items-center justify-between">
@ -64,8 +60,7 @@ export default function () {
onChange={(e) => setNewLink({ ...newLink, name: e.target.value })} onChange={(e) => setNewLink({ ...newLink, name: e.target.value })}
type="text" type="text"
placeholder="e.g. Example Link" placeholder="e.g. Example Link"
className="w-60 rounded p-2 border-sky-100 border-solid border text-sm outline-none focus:border-sky-500 duration-100" className="w-60 rounded p-3 border-sky-100 border-solid border text-sm outline-none focus:border-sky-500 duration-100"
autoFocus
/> />
</div> </div>
@ -76,7 +71,7 @@ export default function () {
onChange={(e) => setNewLink({ ...newLink, url: e.target.value })} onChange={(e) => setNewLink({ ...newLink, url: e.target.value })}
type="text" type="text"
placeholder="e.g. http://example.com/" placeholder="e.g. http://example.com/"
className="w-60 rounded p-2 border-sky-100 border-solid border text-sm outline-none focus:border-sky-500 duration-100" className="w-60 rounded p-3 border-sky-100 border-solid border text-sm outline-none focus:border-sky-500 duration-100"
/> />
</div> </div>
@ -92,7 +87,7 @@ export default function () {
<div <div
className="mx-auto mt-2 bg-sky-500 text-white flex items-center gap-2 py-2 px-5 rounded select-none font-bold cursor-pointer duration-100 hover:bg-sky-400" className="mx-auto mt-2 bg-sky-500 text-white flex items-center gap-2 py-2 px-5 rounded select-none font-bold cursor-pointer duration-100 hover:bg-sky-400"
onClick={() => postLink()} onClick={() => addLink(newLink)}
> >
<FontAwesomeIcon icon={faPlus} className="h-5" /> <FontAwesomeIcon icon={faPlus} className="h-5" />
Add Link Add Link

View File

@ -19,10 +19,8 @@ function useOutsideAlerter(
onClickOutside(); onClickOutside();
} }
} }
// Bind the event listener
document.addEventListener("mouseup", handleClickOutside); document.addEventListener("mouseup", handleClickOutside);
return () => { return () => {
// Unbind the event listener on clean up
document.removeEventListener("mouseup", handleClickOutside); document.removeEventListener("mouseup", handleClickOutside);
}; };
}, [ref, onClickOutside]); }, [ref, onClickOutside]);

View File

@ -1,6 +1,7 @@
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faChevronRight } from "@fortawesome/free-solid-svg-icons"; import { faChevronRight } from "@fortawesome/free-solid-svg-icons";
import { Collection } from "@prisma/client"; import { Collection } from "@prisma/client";
import Link from "next/link";
export default function ({ collection }: { collection: Collection }) { export default function ({ collection }: { collection: Collection }) {
const formattedDate = new Date(collection.createdAt).toLocaleString("en-US", { const formattedDate = new Date(collection.createdAt).toLocaleString("en-US", {
@ -10,6 +11,7 @@ export default function ({ collection }: { collection: Collection }) {
}); });
return ( return (
<Link href={`/collections/${collection.id}`}>
<div className="p-5 bg-gray-100 m-2 h-40 w-60 rounded border-sky-100 border-solid border flex flex-col justify-between cursor-pointer hover:bg-gray-50 duration-100"> <div className="p-5 bg-gray-100 m-2 h-40 w-60 rounded border-sky-100 border-solid border flex flex-col justify-between cursor-pointer hover:bg-gray-50 duration-100">
<div className="flex justify-between text-sky-900 items-center"> <div className="flex justify-between text-sky-900 items-center">
<p className="text-lg w-max">{collection.name}</p> <p className="text-lg w-max">{collection.name}</p>
@ -17,5 +19,6 @@ export default function ({ collection }: { collection: Collection }) {
</div> </div>
<p className="text-sm text-sky-300 font-bold">{formattedDate}</p> <p className="text-sm text-sky-300 font-bold">{formattedDate}</p>
</div> </div>
</Link>
); );
} }

View File

@ -1,4 +1,4 @@
import useCollectionSlice from "@/store/collection"; import useCollectionSlice from "@/store/collections";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import CreatableSelect from "react-select/creatable"; import CreatableSelect from "react-select/creatable";

23
components/LinkList.tsx Normal file
View File

@ -0,0 +1,23 @@
import { LinkAndTags } from "@/types/global";
export default function ({
link,
count,
}: {
link: LinkAndTags;
count: number;
}) {
return (
<div className="border border-sky-100 mb-5 bg-gray-100 p-5 rounded">
<div className="flex items-baseline gap-1">
<p className="text-sm text-sky-600">{count + 1}.</p>
<p className="text-lg text-sky-500">{link.name}</p>
</div>
<div className="flex gap-1 items-baseline">
{link.tags.map((e, i) => (
<p key={i}>{e.name}</p>
))}
</div>
</div>
);
}

View File

@ -1,23 +1,64 @@
import { signOut } from "next-auth/react"; import { signOut } from "next-auth/react";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import useCollectionSlice from "@/store/collection"; import useCollectionSlice from "@/store/collections";
import { Collection } from "@prisma/client"; import { Collection, Tag } from "@prisma/client";
import ClickAwayHandler from "./ClickAwayHandler"; import ClickAwayHandler from "./ClickAwayHandler";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faPlus, faMagnifyingGlass } from "@fortawesome/free-solid-svg-icons"; import {
import { useState } from "react"; faPlus,
faFolder,
faBox,
faTag,
faBookmark,
faMagnifyingGlass,
IconDefinition,
} from "@fortawesome/free-solid-svg-icons";
import { useEffect, useState } from "react";
import AddLinkModal from "./AddLinkModal"; import AddLinkModal from "./AddLinkModal";
import useTagSlice from "@/store/tags";
export default function () { export default function () {
const router = useRouter(); const router = useRouter();
const collectionId = router.query.id; const [pageName, setPageName] = useState<string | null>("");
const [pageIcon, setPageIcon] = useState<IconDefinition | null>(null);
const { collections } = useCollectionSlice(); const { collections } = useCollectionSlice();
const { tags } = useTagSlice();
useEffect(() => {
if (router.route === "/collections/[id]") {
const collectionId = router.query.id;
const activeCollection: Collection | undefined = collections.find( const activeCollection: Collection | undefined = collections.find(
(e) => e.id.toString() == collectionId (e) => e.id.toString() == collectionId
); );
if (activeCollection) {
setPageName(activeCollection?.name);
}
setPageIcon(faFolder);
} else if (router.route === "/tags/[id]") {
const tagId = router.query.id;
const activeTag: Tag | undefined = tags.find(
(e) => e.id.toString() == tagId
);
if (activeTag) {
setPageName(activeTag?.name);
}
setPageIcon(faTag);
} else if (router.route === "/collections") {
setPageName("All Collections");
setPageIcon(faBox);
} else if (router.route === "/links") {
setPageName("All Links");
setPageIcon(faBookmark);
}
}, [router, collections, tags]);
const [collectionInput, setCollectionInput] = useState(false); const [collectionInput, setCollectionInput] = useState(false);
const toggleCollectionInput = () => { const toggleCollectionInput = () => {
@ -26,7 +67,12 @@ export default function () {
return ( return (
<div className="flex justify-between items-center p-5 border-solid border-b-sky-100 border border-l-white"> <div className="flex justify-between items-center p-5 border-solid border-b-sky-100 border border-l-white">
<p>{activeCollection?.name}</p> <div className="text-sky-900 rounded my-1 flex items-center gap-2 font-bold">
{pageIcon ? (
<FontAwesomeIcon icon={pageIcon} className="w-4 text-sky-300" />
) : null}
<p>{pageName}</p>
</div>
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<FontAwesomeIcon <FontAwesomeIcon
icon={faPlus} icon={faPlus}
@ -45,7 +91,7 @@ export default function () {
</div> </div>
{collectionInput ? ( {collectionInput ? (
<div className="fixed top-0 bottom-0 right-0 left-0 bg-gray-500 bg-opacity-10 flex items-center"> <div className="fixed top-0 bottom-0 right-0 left-0 bg-gray-500 bg-opacity-10 flex items-center fade-in">
<ClickAwayHandler <ClickAwayHandler
onClickOutside={toggleCollectionInput} onClickOutside={toggleCollectionInput}
className="w-fit mx-auto" className="w-fit mx-auto"

View File

@ -1,26 +1,21 @@
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Collection } from "@prisma/client";
import { IconProp } from "@fortawesome/fontawesome-svg-core"; import { IconProp } from "@fortawesome/fontawesome-svg-core";
import { MouseEventHandler } from "react"; import { MouseEventHandler } from "react";
import Link from "next/link";
interface TextObject {
name: string;
}
interface SidebarItemProps { interface SidebarItemProps {
item: Collection | TextObject; text: string;
icon: IconProp; icon: IconProp;
onClick?: MouseEventHandler; path: string;
} }
export default function ({ item, icon, onClick }: SidebarItemProps) { export default function ({ text, icon, path }: SidebarItemProps) {
return ( return (
<div <Link href={path}>
onClick={onClick} <div className="hover:bg-gray-50 duration-100 text-sky-900 rounded my-1 p-3 cursor-pointer flex items-center gap-2">
className="hover:bg-gray-50 duration-100 text-sky-900 rounded my-1 p-3 cursor-pointer flex items-center gap-2"
>
<FontAwesomeIcon icon={icon} className="w-4 text-sky-300" /> <FontAwesomeIcon icon={icon} className="w-4 text-sky-300" />
<p>{item.name}</p> <p>{text}</p>
</div> </div>
</Link>
); );
} }

View File

@ -1,19 +1,20 @@
import { useSession } from "next-auth/react"; import { useSession } from "next-auth/react";
import ClickAwayHandler from "@/components/ClickAwayHandler"; import ClickAwayHandler from "@/components/ClickAwayHandler";
import { useState } from "react"; import { useState } from "react";
import useCollectionSlice from "@/store/collection"; import useCollectionSlice from "@/store/collections";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faUserCircle } from "@fortawesome/free-regular-svg-icons"; import { faUserCircle } from "@fortawesome/free-regular-svg-icons";
import { import {
faPlus, faPlus,
faChevronDown, faChevronDown,
faFolder, faFolder,
faBoxesStacked, faBox,
faHashtag, faTag,
faBookmark, faBookmark,
} from "@fortawesome/free-solid-svg-icons"; } from "@fortawesome/free-solid-svg-icons";
import SidebarItem from "./SidebarItem"; import SidebarItem from "./SidebarItem";
import useTagSlice from "@/store/tags"; import useTagSlice from "@/store/tags";
import Link from "next/link";
export default function () { export default function () {
const { data: session } = useSession(); const { data: session } = useSession();
@ -51,9 +52,19 @@ export default function () {
</div> </div>
</div> </div>
<SidebarItem item={{ name: "All Links" }} icon={faBookmark} /> <Link href="links">
<div className="hover:bg-gray-50 duration-100 text-sky-900 rounded my-1 p-3 cursor-pointer flex items-center gap-2">
<FontAwesomeIcon icon={faBookmark} className="w-4 text-sky-300" />
<p>All Links</p>
</div>
</Link>
<SidebarItem item={{ name: "All Collections" }} icon={faBoxesStacked} /> <Link href="/collections">
<div className="hover:bg-gray-50 duration-100 text-sky-900 rounded my-1 p-3 cursor-pointer flex items-center gap-2">
<FontAwesomeIcon icon={faBox} className="w-4 text-sky-300" />
<p>All Collections</p>
</div>
</Link>
<div className="text-gray-500 flex items-center justify-between mt-5"> <div className="text-gray-500 flex items-center justify-between mt-5">
<p className="text-sm p-3">Collections</p> <p className="text-sm p-3">Collections</p>
@ -80,7 +91,14 @@ export default function () {
</div> </div>
<div> <div>
{collections.map((e, i) => { {collections.map((e, i) => {
return <SidebarItem key={i} item={e} icon={faFolder} />; return (
<SidebarItem
key={i}
text={e.name}
icon={faFolder}
path={`/collections/${e.id}`}
/>
);
})} })}
</div> </div>
<div className="text-gray-500 flex items-center justify-between mt-5"> <div className="text-gray-500 flex items-center justify-between mt-5">
@ -88,7 +106,14 @@ export default function () {
</div> </div>
<div> <div>
{tags.map((e, i) => { {tags.map((e, i) => {
return <SidebarItem key={i} item={e} icon={faHashtag} />; return (
<SidebarItem
key={i}
text={e.name}
icon={faTag}
path={`/tags/${e.id}`}
/>
);
})} })}
</div> </div>
</div> </div>

View File

@ -0,0 +1,33 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db";
import { Session } from "next-auth";
export default async function (
req: NextApiRequest,
res: NextApiResponse,
session: Session
) {
const tags = await prisma.link.findMany({
where: {
collection: {
OR: [
{
ownerId: session?.user.id,
},
{
members: {
some: {
userId: session?.user.id,
},
},
},
],
},
},
include: { tags: true },
});
return res.status(200).json({
response: tags || [],
});
}

View File

@ -1,18 +1,7 @@
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db"; import { prisma } from "@/lib/api/db";
import { Session } from "next-auth"; import { Session } from "next-auth";
import { Link } from "@prisma/client"; import { LinkAndTags, NewLink } from "@/types/global";
interface LinkObject {
id: number;
name: string;
url: string;
tags: string[];
collectionId: {
id: string | number;
isNew: boolean | undefined;
};
}
export default async function ( export default async function (
req: NextApiRequest, req: NextApiRequest,
@ -24,7 +13,7 @@ export default async function (
} }
const email: string = session.user.email; const email: string = session.user.email;
const link: LinkObject = req?.body; const link: NewLink = req?.body;
if (!link.name) { if (!link.name) {
return res return res
@ -70,10 +59,10 @@ export default async function (
const collectionId = link.collectionId.id as number; const collectionId = link.collectionId.id as number;
const createLink: Link = await prisma.link.create({ const createLink: LinkAndTags = await prisma.link.create({
data: { data: {
name: link.name, name: link.name,
url: "https://www.example.com", url: link.url,
collection: { collection: {
connect: { connect: {
id: collectionId, id: collectionId,
@ -98,6 +87,7 @@ export default async function (
})), })),
}, },
}, },
include: { tags: true },
}); });
return res.status(200).json({ return res.status(200).json({

View File

@ -1,17 +1,20 @@
import useCollectionSlice from "@/store/collection"; import useCollectionSlice from "@/store/collections";
import { useEffect } from "react"; import { useEffect } from "react";
import { useSession } from "next-auth/react"; import { useSession } from "next-auth/react";
import useTagSlice from "@/store/tags"; import useTagSlice from "@/store/tags";
import useLinkSlice from "@/store/links";
export default function () { export default function () {
const { status } = useSession(); const { status } = useSession();
const { setCollections } = useCollectionSlice(); const { setCollections } = useCollectionSlice();
const { setTags } = useTagSlice(); const { setTags } = useTagSlice();
const { setLinks } = useLinkSlice();
useEffect(() => { useEffect(() => {
if (status === "authenticated") { if (status === "authenticated") {
setCollections(); setCollections();
setTags(); setTags();
setLinks();
} }
}, [status]); }, [status]);
} }

View File

@ -1,6 +1,7 @@
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "pages/api/auth/[...nextauth]"; import { authOptions } from "pages/api/auth/[...nextauth]";
import getLinks from "@/lib/api/controllers/links/getLinks";
import postLink from "@/lib/api/controllers/links/postLink"; import postLink from "@/lib/api/controllers/links/postLink";
type Data = { type Data = {
@ -20,5 +21,6 @@ export default async function (
// Check if user is unauthorized to the collection (If isn't owner or doesn't has the required permission...) // Check if user is unauthorized to the collection (If isn't owner or doesn't has the required permission...)
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (req.method === "GET") return await getLinks(req, res, session);
if (req.method === "POST") return await postLink(req, res, session); if (req.method === "POST") return await postLink(req, res, session);
} }

View File

@ -1,11 +1,24 @@
import LinkList from "@/components/LinkList";
import useLinkSlice from "@/store/links";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
export default function () { export default function () {
const router = useRouter(); const router = useRouter();
const { links } = useLinkSlice();
const collectionId = Number(router.query.id); const linksByCollection = links.filter(
(e) => e.collectionId === Number(router.query.id)
);
console.log(collectionId); return (
// ml-80
return <div>{"HI"}</div>; <div className="p-5">
<p className="text-center mb-5 text-gray-500 font-bold text-sm">
{linksByCollection.length || 0} Links Found
</p>
{linksByCollection.map((e, i) => {
return <LinkList key={i} link={e} count={i} />;
})}
</div>
);
} }

View File

@ -1,5 +1,5 @@
import { useSession } from "next-auth/react"; import { useSession } from "next-auth/react";
import useCollectionSlice from "@/store/collection"; import useCollectionSlice from "@/store/collections";
import CollectionCard from "@/components/CollectionCard"; import CollectionCard from "@/components/CollectionCard";

9
pages/tags/[id].tsx Normal file
View File

@ -0,0 +1,9 @@
import { useRouter } from "next/router";
export default function () {
const router = useRouter();
const tagId = Number(router.query.id);
return <div>{"HI"}</div>;
}

48
store/links.ts Normal file
View File

@ -0,0 +1,48 @@
import { create } from "zustand";
import { LinkAndTags, NewLink } from "@/types/global";
type LinkSlice = {
links: LinkAndTags[];
setLinks: () => void;
addLink: (linkName: NewLink) => void;
updateLink: (link: LinkAndTags) => void;
removeLink: (linkId: number) => void;
};
const useLinkSlice = create<LinkSlice>()((set) => ({
links: [],
setLinks: async () => {
const response = await fetch("/api/routes/links");
const data = await response.json();
if (response.ok) set({ links: data.response });
},
addLink: async (newLink) => {
const response = await fetch("/api/routes/links", {
body: JSON.stringify(newLink),
headers: {
"Content-Type": "application/json",
},
method: "POST",
});
const data = await response.json();
if (response.ok)
set((state) => ({
links: [...state.links, data.response],
}));
},
updateLink: (link) =>
set((state) => ({
links: state.links.map((c) => (c.id === link.id ? link : c)),
})),
removeLink: (linkId) => {
set((state) => ({
links: state.links.filter((c) => c.id !== linkId),
}));
},
}));
export default useLinkSlice;

View File

@ -10,3 +10,40 @@
-ms-overflow-style: none; -ms-overflow-style: none;
scrollbar-width: none; scrollbar-width: none;
} }
::selection {
background-color: #0ea5e9;
color: white;
}
.hyphens {
hyphens: auto;
}
.slide-up {
animation: slide-up-animation 70ms;
}
.fade-in {
animation: fade-in-animation 100ms;
}
@keyframes fade-in-animation {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes slide-up-animation {
0% {
transform: translateY(10%);
opacity: 0;
}
100% {
transform: translateY(0);
opacity: 1;
}
}

15
types/global.ts Normal file
View File

@ -0,0 +1,15 @@
import { Link, Tag } from "@prisma/client";
export interface LinkAndTags extends Link {
tags: Tag[];
}
export interface NewLink {
name: string;
url: string;
tags: string[];
collectionId: {
id: string | number;
isNew?: boolean;
};
}