Merge pull request #476 from IsaacWise06/collection-duplicate-names
feat(collections): Allow collections to be the same name
This commit is contained in:
commit
67bf6b7d75
|
@ -44,12 +44,56 @@ export default function CollectionSelection({
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const formatedCollections = collections.map((e) => {
|
const formatedCollections = collections.map((e) => {
|
||||||
return { value: e.id, label: e.name, ownerId: e.ownerId };
|
return {
|
||||||
|
value: e.id,
|
||||||
|
label: e.name,
|
||||||
|
ownerId: e.ownerId,
|
||||||
|
count: e._count,
|
||||||
|
parentId: e.parentId,
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
setOptions(formatedCollections);
|
setOptions(formatedCollections);
|
||||||
}, [collections]);
|
}, [collections]);
|
||||||
|
|
||||||
|
const getParentNames = (parentId: number): string[] => {
|
||||||
|
const parentNames = [];
|
||||||
|
const parent = collections.find((e) => e.id === parentId);
|
||||||
|
|
||||||
|
if (parent) {
|
||||||
|
parentNames.push(parent.name);
|
||||||
|
if (parent.parentId) {
|
||||||
|
parentNames.push(...getParentNames(parent.parentId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Have the top level parent at beginning
|
||||||
|
return parentNames.reverse();
|
||||||
|
};
|
||||||
|
|
||||||
|
const customOption = ({ data, innerProps }: any) => {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
{...innerProps}
|
||||||
|
className="px-2 py-2 last:border-0 border-b border-neutral-content hover:bg-neutral-content cursor-pointer"
|
||||||
|
>
|
||||||
|
<div className="flex w-full justify-between items-center">
|
||||||
|
<span>{data.label}</span>
|
||||||
|
<span className="text-sm text-neutral">{data.count?.links}</span>
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-600 dark:text-gray-300">
|
||||||
|
{getParentNames(data?.parentId).length > 0 ? (
|
||||||
|
<>
|
||||||
|
{getParentNames(data.parentId).join(" > ")} {">"} {data.label}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
data.label
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
if (creatable) {
|
if (creatable) {
|
||||||
return (
|
return (
|
||||||
<CreatableSelect
|
<CreatableSelect
|
||||||
|
@ -60,6 +104,9 @@ export default function CollectionSelection({
|
||||||
options={options}
|
options={options}
|
||||||
styles={styles}
|
styles={styles}
|
||||||
defaultValue={showDefaultValue ? defaultValue : null}
|
defaultValue={showDefaultValue ? defaultValue : null}
|
||||||
|
components={{
|
||||||
|
Option: customOption,
|
||||||
|
}}
|
||||||
// menuPosition="fixed"
|
// menuPosition="fixed"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
@ -73,6 +120,9 @@ export default function CollectionSelection({
|
||||||
options={options}
|
options={options}
|
||||||
styles={styles}
|
styles={styles}
|
||||||
defaultValue={showDefaultValue ? defaultValue : null}
|
defaultValue={showDefaultValue ? defaultValue : null}
|
||||||
|
components={{
|
||||||
|
Option: customOption,
|
||||||
|
}}
|
||||||
// menuPosition="fixed"
|
// menuPosition="fixed"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { useRouter } from "next/router";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { Disclosure, Transition } from "@headlessui/react";
|
import { Disclosure, Transition } from "@headlessui/react";
|
||||||
import SidebarHighlightLink from "@/components/SidebarHighlightLink";
|
import SidebarHighlightLink from "@/components/SidebarHighlightLink";
|
||||||
import CollectionSelection from "@/components/CollectionSelection";
|
import CollectionListing from "@/components/CollectionListing";
|
||||||
|
|
||||||
export default function Sidebar({ className }: { className?: string }) {
|
export default function Sidebar({ className }: { className?: string }) {
|
||||||
const [tagDisclosure, setTagDisclosure] = useState<boolean>(() => {
|
const [tagDisclosure, setTagDisclosure] = useState<boolean>(() => {
|
||||||
|
@ -99,7 +99,7 @@ export default function Sidebar({ className }: { className?: string }) {
|
||||||
leaveTo="transform opacity-0 -translate-y-3"
|
leaveTo="transform opacity-0 -translate-y-3"
|
||||||
>
|
>
|
||||||
<Disclosure.Panel>
|
<Disclosure.Panel>
|
||||||
<CollectionSelection links={true} />
|
<CollectionListing links={true} />
|
||||||
</Disclosure.Panel>
|
</Disclosure.Panel>
|
||||||
</Transition>
|
</Transition>
|
||||||
</Disclosure>
|
</Disclosure>
|
||||||
|
|
|
@ -12,6 +12,12 @@ export default async function getCollection(userId: number) {
|
||||||
_count: {
|
_count: {
|
||||||
select: { links: true },
|
select: { links: true },
|
||||||
},
|
},
|
||||||
|
parent: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
members: {
|
members: {
|
||||||
include: {
|
include: {
|
||||||
user: {
|
user: {
|
||||||
|
|
|
@ -32,27 +32,6 @@ export default async function postCollection(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const findCollection = await prisma.user.findUnique({
|
|
||||||
where: {
|
|
||||||
id: userId,
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
collections: {
|
|
||||||
where: {
|
|
||||||
name: collection.name,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const checkIfCollectionExists = findCollection?.collections[0];
|
|
||||||
|
|
||||||
if (checkIfCollectionExists)
|
|
||||||
return {
|
|
||||||
response: "Oops! There's already a Collection with that name.",
|
|
||||||
status: 400,
|
|
||||||
};
|
|
||||||
|
|
||||||
const newCollection = await prisma.collection.create({
|
const newCollection = await prisma.collection.create({
|
||||||
data: {
|
data: {
|
||||||
owner: {
|
owner: {
|
||||||
|
@ -65,10 +44,10 @@ export default async function postCollection(
|
||||||
color: collection.color,
|
color: collection.color,
|
||||||
parent: collection.parentId
|
parent: collection.parentId
|
||||||
? {
|
? {
|
||||||
connect: {
|
connect: {
|
||||||
id: collection.parentId,
|
id: collection.parentId,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
|
|
|
@ -22,8 +22,69 @@ export default async function postLink(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!link.collection.name) {
|
if (!link.collection.id && link.collection.name) {
|
||||||
|
link.collection.name = link.collection.name.trim();
|
||||||
|
|
||||||
|
// find the collection with the name and the user's id
|
||||||
|
const findCollection = await prisma.collection.findFirst({
|
||||||
|
where: {
|
||||||
|
name: link.collection.name,
|
||||||
|
ownerId: userId,
|
||||||
|
parentId: link.collection.parentId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (findCollection) {
|
||||||
|
const collectionIsAccessible = await getPermission({
|
||||||
|
userId,
|
||||||
|
collectionId: findCollection.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
const memberHasAccess = collectionIsAccessible?.members.some(
|
||||||
|
(e: UsersAndCollections) => e.userId === userId && e.canCreate
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
|
||||||
|
return { response: "Collection is not accessible.", status: 401 };
|
||||||
|
|
||||||
|
link.collection.id = findCollection.id;
|
||||||
|
} else {
|
||||||
|
const collection = await prisma.collection.create({
|
||||||
|
data: {
|
||||||
|
name: link.collection.name,
|
||||||
|
ownerId: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
link.collection.id = collection.id;
|
||||||
|
}
|
||||||
|
} else if (link.collection.id) {
|
||||||
|
const collectionIsAccessible = await getPermission({
|
||||||
|
userId,
|
||||||
|
collectionId: link.collection.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
const memberHasAccess = collectionIsAccessible?.members.some(
|
||||||
|
(e: UsersAndCollections) => e.userId === userId && e.canCreate
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
|
||||||
|
return { response: "Collection is not accessible.", status: 401 };
|
||||||
|
} else if (!link.collection.id) {
|
||||||
link.collection.name = "Unorganized";
|
link.collection.name = "Unorganized";
|
||||||
|
link.collection.parentId = null;
|
||||||
|
|
||||||
|
// find the collection with the name "Unorganized" and the user's id
|
||||||
|
const unorganizedCollection = await prisma.collection.findFirst({
|
||||||
|
where: {
|
||||||
|
name: "Unorganized",
|
||||||
|
ownerId: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
link.collection.id = unorganizedCollection?.id;
|
||||||
|
} else {
|
||||||
|
return { response: "Uncaught error.", status: 500 };
|
||||||
}
|
}
|
||||||
|
|
||||||
const numberOfLinksTheUserHas = await prisma.link.count({
|
const numberOfLinksTheUserHas = await prisma.link.count({
|
||||||
|
@ -42,22 +103,6 @@ export default async function postLink(
|
||||||
|
|
||||||
link.collection.name = link.collection.name.trim();
|
link.collection.name = link.collection.name.trim();
|
||||||
|
|
||||||
if (link.collection.id) {
|
|
||||||
const collectionIsAccessible = await getPermission({
|
|
||||||
userId,
|
|
||||||
collectionId: link.collection.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
const memberHasAccess = collectionIsAccessible?.members.some(
|
|
||||||
(e: UsersAndCollections) => e.userId === userId && e.canCreate
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
|
|
||||||
return { response: "Collection is not accessible.", status: 401 };
|
|
||||||
} else {
|
|
||||||
link.collection.ownerId = userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
const description =
|
const description =
|
||||||
link.description && link.description !== ""
|
link.description && link.description !== ""
|
||||||
? link.description
|
? link.description
|
||||||
|
@ -86,17 +131,8 @@ export default async function postLink(
|
||||||
description,
|
description,
|
||||||
type: linkType,
|
type: linkType,
|
||||||
collection: {
|
collection: {
|
||||||
connectOrCreate: {
|
connect: {
|
||||||
where: {
|
id: link.collection.id,
|
||||||
name_ownerId: {
|
|
||||||
ownerId: link.collection.ownerId,
|
|
||||||
name: link.collection.name,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
create: {
|
|
||||||
name: link.collection.name.trim(),
|
|
||||||
ownerId: userId,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
tags: {
|
tags: {
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
-- DropIndex
|
||||||
|
DROP INDEX "Collection_name_ownerId_key";
|
|
@ -91,8 +91,6 @@ model Collection {
|
||||||
links Link[]
|
links Link[]
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @default(now()) @updatedAt
|
updatedAt DateTime @default(now()) @updatedAt
|
||||||
|
|
||||||
@@unique([name, ownerId])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
model UsersAndCollections {
|
model UsersAndCollections {
|
||||||
|
|
Ŝarĝante…
Reference in New Issue