diff --git a/components/ModalManagement.tsx b/components/ModalManagement.tsx
index a674bec..7180b57 100644
--- a/components/ModalManagement.tsx
+++ b/components/ModalManagement.tsx
@@ -3,7 +3,7 @@ import Modal from "./Modal";
import LinkModal from "./Modal/LinkModal";
import {
AccountSettings,
- CollectionIncludingMembers,
+ CollectionIncludingMembersAndLinkCount,
LinkIncludingShortenedCollectionAndTags,
} from "@/types/global";
import CollectionModal from "./Modal/Collection";
@@ -33,7 +33,9 @@ export default function ModalManagement() {
toggleCollectionModal={toggleModal}
method={modal.method}
defaultIndex={modal.defaultIndex}
- activeCollection={modal.active as CollectionIncludingMembers}
+ activeCollection={
+ modal.active as CollectionIncludingMembersAndLinkCount
+ }
/>
);
diff --git a/hooks/useSort.tsx b/hooks/useSort.tsx
index 337c523..3328d4d 100644
--- a/hooks/useSort.tsx
+++ b/hooks/useSort.tsx
@@ -1,12 +1,14 @@
import {
- CollectionIncludingMembers,
+ CollectionIncludingMembersAndLinkCount,
LinkIncludingShortenedCollectionAndTags,
Sort,
} from "@/types/global";
import { SetStateAction, useEffect } from "react";
type Props<
- T extends CollectionIncludingMembers | LinkIncludingShortenedCollectionAndTags
+ T extends
+ | CollectionIncludingMembersAndLinkCount
+ | LinkIncludingShortenedCollectionAndTags
> = {
sortBy: Sort;
@@ -15,7 +17,9 @@ type Props<
};
export default function useSort<
- T extends CollectionIncludingMembers | LinkIncludingShortenedCollectionAndTags
+ T extends
+ | CollectionIncludingMembersAndLinkCount
+ | LinkIncludingShortenedCollectionAndTags
>({ sortBy, data, setData }: Props
) {
useEffect(() => {
const dataArray = [...data];
diff --git a/lib/api/controllers/collections/getCollections.ts b/lib/api/controllers/collections/getCollections.ts
index c578656..d65a21f 100644
--- a/lib/api/controllers/collections/getCollections.ts
+++ b/lib/api/controllers/collections/getCollections.ts
@@ -9,6 +9,9 @@ export default async function getCollection(userId: number) {
],
},
include: {
+ _count: {
+ select: { links: true },
+ },
members: {
include: {
user: {
diff --git a/lib/api/controllers/collections/postCollection.ts b/lib/api/controllers/collections/postCollection.ts
index e120cb0..a62db39 100644
--- a/lib/api/controllers/collections/postCollection.ts
+++ b/lib/api/controllers/collections/postCollection.ts
@@ -1,9 +1,9 @@
import { prisma } from "@/lib/api/db";
-import { CollectionIncludingMembers } from "@/types/global";
+import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import { existsSync, mkdirSync } from "fs";
export default async function postCollection(
- collection: CollectionIncludingMembers,
+ collection: CollectionIncludingMembersAndLinkCount,
userId: number
) {
if (!collection || collection.name.trim() === "")
@@ -50,6 +50,9 @@ export default async function postCollection(
},
},
include: {
+ _count: {
+ select: { links: true },
+ },
members: {
include: {
user: {
diff --git a/lib/api/controllers/collections/updateCollection.ts b/lib/api/controllers/collections/updateCollection.ts
index 5123ec9..d1a6b65 100644
--- a/lib/api/controllers/collections/updateCollection.ts
+++ b/lib/api/controllers/collections/updateCollection.ts
@@ -1,9 +1,9 @@
import { prisma } from "@/lib/api/db";
-import { CollectionIncludingMembers } from "@/types/global";
+import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import getPermission from "@/lib/api/getPermission";
export default async function updateCollection(
- collection: CollectionIncludingMembers,
+ collection: CollectionIncludingMembersAndLinkCount,
userId: number
) {
if (!collection.id)
@@ -27,6 +27,7 @@ export default async function updateCollection(
where: {
id: collection.id,
},
+
data: {
name: collection.name,
description: collection.description,
@@ -42,6 +43,9 @@ export default async function updateCollection(
},
},
include: {
+ _count: {
+ select: { links: true },
+ },
members: {
include: {
user: {
diff --git a/lib/client/addMemberToCollection.ts b/lib/client/addMemberToCollection.ts
index 6c64610..9e37aac 100644
--- a/lib/client/addMemberToCollection.ts
+++ b/lib/client/addMemberToCollection.ts
@@ -1,10 +1,10 @@
-import { CollectionIncludingMembers, Member } from "@/types/global";
+import { CollectionIncludingMembersAndLinkCount, Member } from "@/types/global";
import getPublicUserDataByEmail from "./getPublicUserDataByEmail";
const addMemberToCollection = async (
ownerEmail: string,
memberEmail: string,
- collection: CollectionIncludingMembers,
+ collection: CollectionIncludingMembersAndLinkCount,
setMember: (newMember: Member) => null | undefined
) => {
console.log(collection.members);
diff --git a/pages/api/hello.ts b/pages/api/hello.ts
deleted file mode 100644
index b170c05..0000000
--- a/pages/api/hello.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-import type { NextApiRequest, NextApiResponse } from "next";
-import { getServerSession } from "next-auth/next";
-import { authOptions } from "pages/api/auth/[...nextauth]";
-
-type Data = {
- message: string;
- data?: object;
-};
-
-export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse
-) {
- const session = await getServerSession(req, res, authOptions);
-
- console.log(session);
-
- if (!session) {
- res.status(401).json({ message: "You must be logged in." });
- return;
- }
-
- return res.json({
- message: "Success",
- });
-}
diff --git a/pages/collections/[id].tsx b/pages/collections/[id].tsx
index 8fae662..7b5a5cb 100644
--- a/pages/collections/[id].tsx
+++ b/pages/collections/[id].tsx
@@ -2,7 +2,7 @@ import Dropdown from "@/components/Dropdown";
import LinkCard from "@/components/LinkCard";
import useCollectionStore from "@/store/collections";
import useLinkStore from "@/store/links";
-import { CollectionIncludingMembers, Sort } from "@/types/global";
+import { CollectionIncludingMembersAndLinkCount, Sort } from "@/types/global";
import {
faEllipsis,
faFolder,
@@ -33,7 +33,7 @@ export default function Index() {
const [sortBy, setSortBy] = useState(Sort.DateNewestFirst);
const [activeCollection, setActiveCollection] =
- useState();
+ useState();
useLinks({ collectionId: Number(router.query.id), sort: sortBy });
diff --git a/pages/dashboard.tsx b/pages/dashboard.tsx
index 0de1dc4..590c469 100644
--- a/pages/dashboard.tsx
+++ b/pages/dashboard.tsx
@@ -77,7 +77,11 @@ export default function Dashboard() {
- {links.length}
+ {collections.reduce(
+ (accumulator, collection) =>
+ accumulator + collection._count.links,
+ 0
+ )}
Links
diff --git a/store/collections.ts b/store/collections.ts
index c88828f..27d5971 100644
--- a/store/collections.ts
+++ b/store/collections.ts
@@ -1,13 +1,15 @@
import { create } from "zustand";
-import { CollectionIncludingMembers } from "@/types/global";
+import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import useTagStore from "./tags";
type CollectionStore = {
- collections: CollectionIncludingMembers[];
+ collections: CollectionIncludingMembersAndLinkCount[];
setCollections: () => void;
- addCollection: (body: CollectionIncludingMembers) => Promise;
+ addCollection: (
+ body: CollectionIncludingMembersAndLinkCount
+ ) => Promise;
updateCollection: (
- collection: CollectionIncludingMembers
+ collection: CollectionIncludingMembersAndLinkCount
) => Promise;
removeCollection: (collectionId: number) => Promise;
};
diff --git a/store/modals.ts b/store/modals.ts
index d54ab94..f2de102 100644
--- a/store/modals.ts
+++ b/store/modals.ts
@@ -1,6 +1,6 @@
import {
AccountSettings,
- CollectionIncludingMembers,
+ CollectionIncludingMembersAndLinkCount,
LinkIncludingShortenedCollectionAndTags,
} from "@/types/global";
import { create } from "zustand";
@@ -28,7 +28,7 @@ type Modal =
modal: "COLLECTION";
state: boolean;
method: "CREATE" | "UPDATE";
- active: CollectionIncludingMembers;
+ active: CollectionIncludingMembersAndLinkCount;
defaultIndex?: number;
}
| null;
diff --git a/types/global.ts b/types/global.ts
index 8c9d106..ec21ed6 100644
--- a/types/global.ts
+++ b/types/global.ts
@@ -27,10 +27,11 @@ export interface Member {
user: OptionalExcluding;
}
-export interface CollectionIncludingMembers
+export interface CollectionIncludingMembersAndLinkCount
extends Omit {
id?: number;
createdAt?: string;
+ _count: { links: number };
members: Member[];
}