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 (
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 (<>{state.error?.title}
{state.error?.order}
-