+
{profileStatus && (
diff --git a/components/ModalManagement.tsx b/components/ModalManagement.tsx
index 9c8b772..b8953f8 100644
--- a/components/ModalManagement.tsx
+++ b/components/ModalManagement.tsx
@@ -40,6 +40,7 @@ export default function ModalManagement() {
@@ -43,7 +43,7 @@ export default function ProfilePhoto({
src={src}
height={112}
width={112}
- className={`h-10 w-10 shadow rounded-full aspect-square border-[3px] border-slate-200 ${className}`}
+ className={`h-10 w-10 shadow rounded-full aspect-square border border-slate-200 ${className}`}
/>
);
}
diff --git a/hooks/usePermissions.tsx b/hooks/usePermissions.tsx
index a2fb4e4..746897b 100644
--- a/hooks/usePermissions.tsx
+++ b/hooks/usePermissions.tsx
@@ -1,7 +1,7 @@
import useAccountStore from "@/store/account";
import useCollectionStore from "@/store/collections";
-import { CollectionIncludingMembersAndLinkCount, Member } from "@/types/global";
-import React, { useEffect, useState } from "react";
+import { Member } from "@/types/global";
+import { useEffect, useState } from "react";
export default function usePermissions(collectionId: number) {
const { collections } = useCollectionStore();
@@ -26,7 +26,7 @@ export default function usePermissions(collectionId: number) {
setPermissions(account.id === collection.ownerId || getPermission);
}
- }, [collections]);
+ }, [account, collections, collectionId]);
return permissions;
}
diff --git a/lib/api/controllers/collections/deleteCollection.ts b/lib/api/controllers/collections/deleteCollection.ts
index 7fcd2d2..d45308a 100644
--- a/lib/api/controllers/collections/deleteCollection.ts
+++ b/lib/api/controllers/collections/deleteCollection.ts
@@ -1,24 +1,45 @@
import { prisma } from "@/lib/api/db";
import getPermission from "@/lib/api/getPermission";
+import { UsersAndCollections } from "@prisma/client";
import fs from "fs";
export default async function deleteCollection(
collection: { id: number },
userId: number
) {
- if (!collection.id)
+ const collectionId = collection.id;
+
+ if (!collectionId)
return { response: "Please choose a valid collection.", status: 401 };
- const collectionIsAccessible = await getPermission(userId, collection.id);
+ const collectionIsAccessible = await getPermission(userId, collectionId);
- if (!(collectionIsAccessible?.ownerId === userId))
+ const memberHasAccess = collectionIsAccessible?.members.some(
+ (e: UsersAndCollections) => e.userId === userId
+ );
+
+ if (collectionIsAccessible?.ownerId !== userId && memberHasAccess) {
+ // Remove relation/Leave collection
+ const deletedUsersAndCollectionsRelation =
+ await prisma.usersAndCollections.delete({
+ where: {
+ userId_collectionId: {
+ userId: userId,
+ collectionId: collectionId,
+ },
+ },
+ });
+
+ return { response: deletedUsersAndCollectionsRelation, status: 200 };
+ } else if (collectionIsAccessible?.ownerId !== userId) {
return { response: "Collection is not accessible.", status: 401 };
+ }
const deletedCollection = await prisma.$transaction(async () => {
await prisma.usersAndCollections.deleteMany({
where: {
collection: {
- id: collection.id,
+ id: collectionId,
},
},
});
@@ -26,13 +47,13 @@ export default async function deleteCollection(
await prisma.link.deleteMany({
where: {
collection: {
- id: collection.id,
+ id: collectionId,
},
},
});
try {
- fs.rmdirSync(`data/archives/${collection.id}`, { recursive: true });
+ fs.rmdirSync(`data/archives/${collectionId}`, { recursive: true });
} catch (error) {
console.log(
"Collection's archive directory wasn't deleted most likely because it didn't exist..."
@@ -41,7 +62,7 @@ export default async function deleteCollection(
return await prisma.collection.delete({
where: {
- id: collection.id,
+ id: collectionId,
},
});
});
diff --git a/pages/collections/[id].tsx b/pages/collections/[id].tsx
index 16a29aa..72b338a 100644
--- a/pages/collections/[id].tsx
+++ b/pages/collections/[id].tsx
@@ -17,6 +17,7 @@ import ProfilePhoto from "@/components/ProfilePhoto";
import SortDropdown from "@/components/SortDropdown";
import useModalStore from "@/store/modals";
import useLinks from "@/hooks/useLinks";
+import usePermissions from "@/hooks/usePermissions";
export default function Index() {
const { setModal } = useModalStore();
@@ -35,6 +36,8 @@ export default function Index() {
const [activeCollection, setActiveCollection] =
useState
();
+ const permissions = usePermissions(activeCollection?.id as number);
+
useLinks({ collectionId: Number(router.query.id), sort: sortBy });
useEffect(() => {
@@ -71,13 +74,13 @@ export default function Index() {
>
- activeCollection &&
setModal({
modal: "COLLECTION",
state: true,
method: "UPDATE",
+ isOwner: permissions === true,
active: activeCollection,
- defaultIndex: 1,
+ defaultIndex: permissions === true ? 1 : 0,
})
}
className="flex justify-center sm:justify-end items-center w-fit mx-auto sm:mr-0 sm:ml-auto group cursor-pointer"
@@ -87,10 +90,7 @@ export default function Index() {
activeCollection.members[0] && "mr-1"
}`}
>
- {activeCollection.ownerId === data?.user.id
- ? "Manage"
- : "View"}{" "}
- Team
+ {permissions === true ? "Manage" : "View"} Team
{activeCollection?.members
.sort((a, b) => (a.userId as number) - (b.userId as number))
@@ -99,7 +99,7 @@ export default function Index() {
);
})
@@ -155,54 +155,68 @@ export default function Index() {
{expandDropdown ? (
{
+ setModal({
+ modal: "LINK",
+ state: true,
+ method: "CREATE",
+ });
+ setExpandDropdown(false);
+ },
+ }
+ : undefined,
+ permissions === true
+ ? {
+ name: "Edit Collection Info",
+ onClick: () => {
+ activeCollection &&
+ setModal({
+ modal: "COLLECTION",
+ state: true,
+ method: "UPDATE",
+ isOwner: permissions === true,
+ active: activeCollection,
+ });
+ setExpandDropdown(false);
+ },
+ }
+ : undefined,
{
- name: "Add Link Here",
- onClick: () => {
- setModal({
- modal: "LINK",
- state: true,
- method: "CREATE",
- });
- setExpandDropdown(false);
- },
- },
- {
- name: "Edit Collection Info",
- onClick: () => {
- activeCollection &&
- setModal({
- modal: "COLLECTION",
- state: true,
- method: "UPDATE",
- active: activeCollection,
- });
- setExpandDropdown(false);
- },
- },
- {
- name: "Share/Collaborate",
+ name:
+ permissions === true
+ ? "Share/Collaborate"
+ : "View Team",
onClick: () => {
activeCollection &&
setModal({
modal: "COLLECTION",
state: true,
method: "UPDATE",
+ isOwner: permissions === true,
active: activeCollection,
defaultIndex: 1,
});
setExpandDropdown(false);
},
},
+
{
- name: "Delete Collection",
+ name:
+ permissions === true
+ ? "Delete Collection"
+ : "Leave Collection",
onClick: () => {
activeCollection &&
setModal({
modal: "COLLECTION",
state: true,
method: "UPDATE",
+ isOwner: permissions === true,
active: activeCollection,
- defaultIndex: 2,
+ defaultIndex: permissions === true ? 2 : 1,
});
setExpandDropdown(false);
},
diff --git a/store/modals.ts b/store/modals.ts
index 31f35ff..68e3203 100644
--- a/store/modals.ts
+++ b/store/modals.ts
@@ -30,6 +30,7 @@ type Modal =
modal: "COLLECTION";
state: boolean;
method: "UPDATE";
+ isOwner: boolean;
active: CollectionIncludingMembersAndLinkCount;
defaultIndex?: number;
}
@@ -37,6 +38,7 @@ type Modal =
modal: "COLLECTION";
state: boolean;
method: "CREATE";
+ isOwner: boolean;
active?: CollectionIncludingMembersAndLinkCount;
defaultIndex?: number;
}