Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

Improve endpoint & reordering when a collection is deleted

Isaac4333d2686cd8
diff --git a/apps/web/components/DashboardLayoutDropdown.tsx b/apps/web/components/DashboardLayoutDropdown.tsx
index 25799a5e..5a18b472 100644
--- a/apps/web/components/DashboardLayoutDropdown.tsx
+++ b/apps/web/components/DashboardLayoutDropdown.tsx
@@ -198,7 +198,7 @@ export default function DashboardLayoutDropdown() {
return {
...s,
enabled: !s.enabled,
- order: !s.enabled ? highestOrder + 1 : s.order,
+ order: !s.enabled ? highestOrder + 1 : undefined,
};
}
return s;
diff --git a/apps/web/lib/api/controllers/collections/collectionId/deleteCollectionById.ts b/apps/web/lib/api/controllers/collections/collectionId/deleteCollectionById.ts
index 20221225..5765f329 100644
--- a/apps/web/lib/api/controllers/collections/collectionId/deleteCollectionById.ts
+++ b/apps/web/lib/api/controllers/collections/collectionId/deleteCollectionById.ts
@@ -32,7 +32,10 @@ export default async function deleteCollection(
},
});
- await removeFromOrders(userId, collectionId);
+ await Promise.all([
+ removeFromOrders(userId, collectionId),
+ updateDashboardSectionLayout(userId, collectionId),
+ ]);
await prisma.link.updateMany({
where: {
@@ -62,7 +65,10 @@ export default async function deleteCollection(
await removeFolder({ filePath: `archives/${collectionId}` });
await removeFolder({ filePath: `archives/preview/${collectionId}` });
- await removeFromOrders(userId, collectionId);
+ await Promise.all([
+ removeFromOrders(userId, collectionId),
+ updateDashboardSectionLayout(userId, collectionId),
+ ]);
const links = await prisma.link.findMany({
where: {
@@ -167,3 +173,41 @@ async function removeFromOrders(userId: number, collectionId: number) {
},
});
}
+
+async function updateDashboardSectionLayout(
+ userId: number,
+ collectionId: number
+) {
+ const dashboardSection = await prisma.dashboardSection.findFirst({
+ where: {
+ userId,
+ collectionId,
+ },
+ });
+
+ if (dashboardSection) {
+ const sectionOrder = dashboardSection.order;
+
+ await prisma.$transaction(async (tx) => {
+ await tx.dashboardSection.delete({
+ where: {
+ id: dashboardSection.id,
+ },
+ });
+
+ await tx.dashboardSection.updateMany({
+ where: {
+ userId,
+ order: {
+ gt: sectionOrder,
+ },
+ },
+ data: {
+ order: {
+ decrement: 1,
+ },
+ },
+ });
+ });
+ }
+}
diff --git a/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts b/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts
index ab8af240..1954f6bb 100644
--- a/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts
+++ b/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts
@@ -3,6 +3,7 @@ import {
UpdateDashboardLayoutSchemaType,
} from "@linkwarden/lib/schemaValidation";
import { prisma } from "@linkwarden/prisma";
+import { DashboardSection } from "@linkwarden/prisma/client";
export default async function updateDashboardLayout(
userId: number,
@@ -22,43 +23,76 @@ export default async function updateDashboardLayout(
const data = dataValidation.data;
- // This can be improved - less queries and not in a loop
+ const existingSections = await prisma.dashboardSection.findMany({
+ where: {
+ userId: userId,
+ },
+ });
+
+ const existingSectionsMap = new Map(
+ existingSections.map((section) => [
+ `${section.type}-${section.collectionId || "default"}`,
+ section,
+ ])
+ );
+
+ const sectionsToCreate: Pick<
+ DashboardSection,
+ "userId" | "type" | "collectionId" | "order"
+ >[] = [];
+ const sectionsToUpdate: Partial<DashboardSection>[] = [];
+ const sectionsToDelete: number[] = [];
+
for (const section of data) {
- const existingSection = await prisma.dashboardSection.findFirst({
- where: {
- userId: userId,
- type: section.type,
- collectionId: section.collectionId || undefined,
- },
- });
+ const key = `${section.type}-${section.collectionId || "default"}`;
+ const existingSection = existingSectionsMap.get(key);
if (existingSection && !section.enabled) {
- await prisma.dashboardSection.delete({
- where: {
- id: existingSection.id,
- },
- });
+ sectionsToDelete.push(existingSection.id);
} else if (existingSection && section.enabled) {
- await prisma.dashboardSection.update({
- where: {
- id: existingSection.id,
- },
- data: {
- order: section.order,
- },
+ sectionsToUpdate.push({
+ id: existingSection.id,
+ order: section.order,
});
} else if (section.enabled) {
- await prisma.dashboardSection.create({
- data: {
- userId: userId,
- type: section.type,
- collectionId: section.collectionId || undefined,
- order: section.order || 0,
- },
+ sectionsToCreate.push({
+ userId: userId,
+ type: section.type,
+ collectionId: section.collectionId || null,
+ order: section.order!,
});
}
}
+ await prisma.$transaction(async (tx) => {
+ if (sectionsToDelete.length > 0) {
+ await tx.dashboardSection.deleteMany({
+ where: {
+ id: {
+ in: sectionsToDelete,
+ },
+ },
+ });
+ }
+
+ if (sectionsToUpdate.length > 0) {
+ await Promise.all(
+ sectionsToUpdate.map((section: any) =>
+ tx.dashboardSection.update({
+ where: { id: section.id },
+ data: { order: section.order },
+ })
+ )
+ );
+ }
+
+ if (sectionsToCreate.length > 0) {
+ await tx.dashboardSection.createMany({
+ data: sectionsToCreate,
+ });
+ }
+ });
+
return {
status: 200,
response: data,