Allow collections with the same name
This commit is contained in:
@@ -10,11 +10,11 @@ type Props = {
|
|||||||
onChange: any;
|
onChange: any;
|
||||||
showDefaultValue?: boolean;
|
showDefaultValue?: boolean;
|
||||||
defaultValue?:
|
defaultValue?:
|
||||||
| {
|
| {
|
||||||
label: string;
|
label: string;
|
||||||
value?: number;
|
value?: number;
|
||||||
}
|
}
|
||||||
| undefined;
|
| undefined;
|
||||||
creatable?: boolean;
|
creatable?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -44,12 +44,54 @@ 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-primary/20 cursor-pointer'
|
||||||
|
>
|
||||||
|
<div className="flex w-full justify-between items-center">
|
||||||
|
<span>
|
||||||
|
{data.label}
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
{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,7 +102,10 @@ export default function CollectionSelection({
|
|||||||
options={options}
|
options={options}
|
||||||
styles={styles}
|
styles={styles}
|
||||||
defaultValue={showDefaultValue ? defaultValue : null}
|
defaultValue={showDefaultValue ? defaultValue : null}
|
||||||
// menuPosition="fixed"
|
components={{
|
||||||
|
Option: customOption,
|
||||||
|
}}
|
||||||
|
// menuPosition="fixed"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
@@ -73,7 +118,7 @@ export default function CollectionSelection({
|
|||||||
options={options}
|
options={options}
|
||||||
styles={styles}
|
styles={styles}
|
||||||
defaultValue={showDefaultValue ? defaultValue : null}
|
defaultValue={showDefaultValue ? defaultValue : null}
|
||||||
// menuPosition="fixed"
|
// menuPosition="fixed"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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: {
|
||||||
|
|||||||
@@ -88,14 +88,11 @@ export default async function postLink(
|
|||||||
collection: {
|
collection: {
|
||||||
connectOrCreate: {
|
connectOrCreate: {
|
||||||
where: {
|
where: {
|
||||||
name_ownerId: {
|
id: link.collection.id ?? 0,
|
||||||
ownerId: link.collection.ownerId,
|
|
||||||
name: link.collection.name,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
create: {
|
create: {
|
||||||
name: link.collection.name.trim(),
|
name: link.collection.name.trim(),
|
||||||
ownerId: userId,
|
ownerId: userId
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user