From 46d10d6ef48a3bfed2b7a1d97c0ceea63819bb30 Mon Sep 17 00:00:00 2001 From: Philipp Traber Date: Tue, 1 Apr 2025 13:43:32 +0200 Subject: [PATCH] drag and drop for custom page links --- src/actions/block_actions.ts | 79 +++++++++++++- src/app/~/dash/[uuid]/page.tsx | 15 ++- src/components/CustomPageDashboard.tsx | 104 ++++++++++++------- src/components/forms/createLinkBlockForm.tsx | 6 +- 4 files changed, 158 insertions(+), 46 deletions(-) diff --git a/src/actions/block_actions.ts b/src/actions/block_actions.ts index 92e0a88..db5a3af 100644 --- a/src/actions/block_actions.ts +++ b/src/actions/block_actions.ts @@ -30,7 +30,8 @@ export async function createLinkBlock(prevState: any, formData: FormData) { const customPage = await prisma.customPage.findUnique({ where: { uuid: data.data.customPageUuid }, include: { - page: true + page: true, + linkBlock: true } }); if (!customPage) { @@ -53,12 +54,15 @@ export async function createLinkBlock(prevState: any, formData: FormData) { } try { + const currentLinkCount = customPage.linkBlock.length; + const newOrder = currentLinkCount + 1; + await prisma.linkBlock.create({ data: { customPageUuid: data.data.customPageUuid, redirectPageUuid: redirectPage?.uuid, title: data.data.title, - order: data.data.order + order: newOrder } }); } catch (e: any) { @@ -112,3 +116,74 @@ export async function deleteLinkBlock(prevState: any, formData: FormData) { }; } +export async function updateLinkBlockOrder(prevState: any, formData: FormData) { + const schema = z.object({ + linkBlockUuid: z.string().uuid(), + newOrder: z.coerce.number() + }); + + const data = schema.safeParse({ + linkBlockUuid: formData.get("linkBlockUuid"), + newOrder: formData.get("newOrder") + }); + + if (!data.success) { + return { + message: "Invalid data", + error: data.error.flatten().fieldErrors, + }; + } + + try { + // Get the link block being moved + const movedLinkBlock = await prisma.linkBlock.findUnique({ + where: { uuid: data.data.linkBlockUuid }, + include: { customPage: true } + }); + + if (!movedLinkBlock) { + return { + message: "Link block not found", + error: {} + }; + } + + // Get all link blocks for this custom page ordered by their current order + const allLinkBlocks = await prisma.linkBlock.findMany({ + where: { customPageUuid: movedLinkBlock.customPageUuid }, + orderBy: { order: "asc" } + }); + + // Find the current index of the moved block + const currentIndex = allLinkBlocks.findIndex(block => block.uuid === data.data.linkBlockUuid); + + // Create a new array with the block moved to its new position + const reorderedBlocks = [...allLinkBlocks]; + const [movedBlock] = reorderedBlocks.splice(currentIndex, 1); + reorderedBlocks.splice(data.data.newOrder - 1, 0, movedBlock); + + // Update orders based on the new positions + const updates = reorderedBlocks.map((block, index) => { + return prisma.linkBlock.update({ + where: { uuid: block.uuid }, + data: { order: index + 1 } + }); + }); + + // Execute all updates in a transaction + await prisma.$transaction(updates); + } catch (e: any) { + return { + message: "Error updating link order", + error: e.message + }; + } + + revalidatePath("/~/dash"); + revalidatePath(`/~/dash/${data.data.linkBlockUuid}/page`); + + return { + message: "Link order updated" + }; +} + diff --git a/src/app/~/dash/[uuid]/page.tsx b/src/app/~/dash/[uuid]/page.tsx index b5fd2c0..7b0d524 100644 --- a/src/app/~/dash/[uuid]/page.tsx +++ b/src/app/~/dash/[uuid]/page.tsx @@ -67,6 +67,19 @@ export default async function DetailedStats({ params }: PageProps) { const linkPages = customPage ? await prisma.redirectPage.findMany({ include: { page: true } }) : undefined const shortcodes = linkPages ? linkPages.map((p) => p.page.shortcode) : undefined + // get link blocks if the page is a custom page + const linkBlocks = customPage ? await prisma.linkBlock.findMany({ + where: { + customPageUuid: customPage.uuid + }, + orderBy: { + order: "asc" + }, + include: { + redirectPage: { include: { page: true } } + } + }) : undefined; + return (
@@ -112,7 +125,7 @@ export default async function DetailedStats({ params }: PageProps) {
- {customPage ? : ""} + {customPage ? : ""}

Visitors

All visits (not unique): {page._count.stat}

diff --git a/src/components/CustomPageDashboard.tsx b/src/components/CustomPageDashboard.tsx index 6a38be8..03d3655 100644 --- a/src/components/CustomPageDashboard.tsx +++ b/src/components/CustomPageDashboard.tsx @@ -1,51 +1,79 @@ +"use client" + import { CustomPage } from "@prisma/client" import prisma from "../../prisma/prisma" import CreateLinkBlockForm from "./forms/createLinkBlockForm" import DeleteLinkBlockForm from "./forms/deleteLinkBlockForm" -import { ArrowTopRightOnSquareIcon, ArrowTrendingUpIcon, ArrowUpCircleIcon, ArrowUpRightIcon } from "@heroicons/react/24/solid" +import { ArrowTopRightOnSquareIcon, ArrowTrendingUpIcon } from "@heroicons/react/24/solid" +import { DragDropContext, Droppable, Draggable } from "@hello-pangea/dnd" +import { updateLinkBlockOrder } from "@/actions/block_actions" +import { useRouter } from "next/navigation" +import { useState } from "react" -export default async function CustomPageDashboard(params: { customPage: CustomPage }) { +export default function CustomPageDashboard({ customPage, linkBlocks, shortcodes }: { + customPage: CustomPage, + linkBlocks: any[], + shortcodes: string[] +}) { + const router = useRouter(); + const [localLinkBlocks, setLocalLinkBlocks] = useState(linkBlocks); - const customPage = params.customPage - const redirectPages = await prisma.redirectPage.findMany({ include: { page: true } }) - const shortcodes = redirectPages.map((p) => p.page.shortcode) + const handleDragEnd = async (result: any) => { + if (!result.destination) return; - const linkBlocks = await prisma.linkBlock.findMany({ - where: { - customPageUuid: customPage.uuid - }, - orderBy: { - order: "asc" - }, - include: { - redirectPage: { include: { page: true } } - } - }); + const items = Array.from(localLinkBlocks); + const [reorderedItem] = items.splice(result.source.index, 1); + items.splice(result.destination.index, 0, reorderedItem); + + // Update local state immediately + setLocalLinkBlocks(items); + + // Update the database in the background + const formData = new FormData(); + formData.append("linkBlockUuid", reorderedItem.uuid); + formData.append("newOrder", result.destination.index + 1); + + await updateLinkBlockOrder(null, formData); + }; return (<>

Links on Page

- {linkBlocks.map((block) => ( - -
-
-
order: {block.order}
-
title: {block.title}
-
link page: {block.redirectPage.page.shortcode}
-
-
- - - -
-
- ))} + + + {(provided) => ( +
+ {localLinkBlocks.map((block, index) => ( + + {(provided) => ( +
+
+
title: {block.title}
+
link page: {block.redirectPage.page.shortcode}
+
+
+ + + +
+
+ )} +
+ ))} + {provided.placeholder} +
+ )} +
+

Add new link page connection

- - - ) + ) } \ No newline at end of file diff --git a/src/components/forms/createLinkBlockForm.tsx b/src/components/forms/createLinkBlockForm.tsx index 4eef7b5..83545b2 100644 --- a/src/components/forms/createLinkBlockForm.tsx +++ b/src/components/forms/createLinkBlockForm.tsx @@ -31,11 +31,7 @@ export default function CreateLinkBlockForm({ customPage, shortcodes }: { custom

{state.error?.title}

-
- - -

{state.error?.order}

-
+ add to page