diff --git a/components/InputSelect/CollectionSelection.tsx b/components/InputSelect/CollectionSelection.tsx index ec586ea6..5e36ff8d 100644 --- a/components/InputSelect/CollectionSelection.tsx +++ b/components/InputSelect/CollectionSelection.tsx @@ -10,11 +10,11 @@ type Props = { onChange: any; showDefaultValue?: boolean; defaultValue?: - | { - label: string; - value?: number; - } - | undefined; + | { + label: string; + value?: number; + } + | undefined; creatable?: boolean; }; @@ -44,12 +44,54 @@ export default function CollectionSelection({ useEffect(() => { 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); }, [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 ( +
+
+ + {data.label} + + + {data.count?.links} + +
+
+ {getParentNames(data?.parentId).length > 0 ? ( + <> + {getParentNames(data.parentId).join(' > ')} {'>'} {data.label} + + ) : ( + data.label + )} +
+
+ ); + }; + if (creatable) { return ( ); } else { @@ -73,7 +118,7 @@ export default function CollectionSelection({ options={options} styles={styles} defaultValue={showDefaultValue ? defaultValue : null} - // menuPosition="fixed" + // menuPosition="fixed" /> ); } diff --git a/lib/api/controllers/collections/getCollections.ts b/lib/api/controllers/collections/getCollections.ts index 37421636..fa13798c 100644 --- a/lib/api/controllers/collections/getCollections.ts +++ b/lib/api/controllers/collections/getCollections.ts @@ -12,6 +12,12 @@ export default async function getCollection(userId: number) { _count: { select: { links: true }, }, + parent: { + select: { + id: true, + name: true, + }, + }, members: { include: { user: { diff --git a/lib/api/controllers/collections/postCollection.ts b/lib/api/controllers/collections/postCollection.ts index 245f6423..94f47634 100644 --- a/lib/api/controllers/collections/postCollection.ts +++ b/lib/api/controllers/collections/postCollection.ts @@ -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({ data: { owner: { @@ -65,10 +44,10 @@ export default async function postCollection( color: collection.color, parent: collection.parentId ? { - connect: { - id: collection.parentId, - }, - } + connect: { + id: collection.parentId, + }, + } : undefined, }, include: { diff --git a/lib/api/controllers/links/postLink.ts b/lib/api/controllers/links/postLink.ts index 1c82fc2f..de2d80c0 100644 --- a/lib/api/controllers/links/postLink.ts +++ b/lib/api/controllers/links/postLink.ts @@ -88,14 +88,11 @@ export default async function postLink( collection: { connectOrCreate: { where: { - name_ownerId: { - ownerId: link.collection.ownerId, - name: link.collection.name, - }, + id: link.collection.id ?? 0, }, create: { name: link.collection.name.trim(), - ownerId: userId, + ownerId: userId }, }, }, diff --git a/prisma/migrations/20240218080348_allow_duplicate_collection_names/migration.sql b/prisma/migrations/20240218080348_allow_duplicate_collection_names/migration.sql new file mode 100644 index 00000000..d73171b7 --- /dev/null +++ b/prisma/migrations/20240218080348_allow_duplicate_collection_names/migration.sql @@ -0,0 +1,2 @@ +-- DropIndex +DROP INDEX "Collection_name_ownerId_key"; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 8cfa7b3f..f081fc53 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -91,8 +91,6 @@ model Collection { links Link[] createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt - - @@unique([name, ownerId]) } model UsersAndCollections {