drag and drop for custom page links

This commit is contained in:
2025-04-01 13:43:32 +02:00
parent 01975c900e
commit 46d10d6ef4
4 changed files with 158 additions and 46 deletions
+66 -38
View File
@@ -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 (<>
<h2 className="mb-5">Links on Page</h2>
{linkBlocks.map((block) => (
<div key={block.uuid} className="flex items-center justify-between zebra max-sm:flex-col max-sm:items-start">
<div className="flex">
<div className="mr-5">order: {block.order}</div>
<div className="mr-5">title: <span className="font-bold">{block.title}</span></div>
<div>link page: <span className="font-bold">{block.redirectPage.page.shortcode}</span></div>
</div>
<div className="flex max-sm:mt-3 max-sm:w-full max-sm:justify-between">
<button>
<a href={`/${block.redirectPage.page.shortcode}`} className="btn btn-primary"><ArrowTopRightOnSquareIcon className="button-icon" /></a>
</button>
<button className="ml-3">
<a href={`/~/dash/${block.redirectPage.page.uuid}`} className="btn btn-primary"><ArrowTrendingUpIcon className="button-icon" /></a>
</button>
<DeleteLinkBlockForm linkBlockUuid={block.uuid} />
</div>
</div>
))}
<DragDropContext onDragEnd={handleDragEnd}>
<Droppable droppableId="links">
{(provided) => (
<div {...provided.droppableProps} ref={provided.innerRef}>
{localLinkBlocks.map((block, index) => (
<Draggable key={block.uuid} draggableId={block.uuid} index={index}>
{(provided) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
className="flex items-center justify-between zebra max-sm:flex-col max-sm:items-start"
>
<div className="flex">
<div className="mr-5">title: <span className="font-bold">{block.title}</span></div>
<div>link page: <span className="font-bold">{block.redirectPage.page.shortcode}</span></div>
</div>
<div className="flex max-sm:mt-3 max-sm:w-full max-sm:justify-between">
<button>
<a href={`/${block.redirectPage.page.shortcode}`} className="btn btn-primary"><ArrowTopRightOnSquareIcon className="button-icon" /></a>
</button>
<button className="ml-3">
<a href={`/~/dash/${block.redirectPage.page.uuid}`} className="btn btn-primary"><ArrowTrendingUpIcon className="button-icon" /></a>
</button>
<DeleteLinkBlockForm linkBlockUuid={block.uuid} />
</div>
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>
<h2 className="mt-10">Add new link page connection</h2>
<CreateLinkBlockForm customPage={customPage.uuid} shortcodes={shortcodes || []} />
</>
)
</>)
}
+1 -5
View File
@@ -31,11 +31,7 @@ export default function CreateLinkBlockForm({ customPage, shortcodes }: { custom
<input type="text" name="title" id="title" className={state.error?.title ? "border-red-400" : ""} />
<p className="form-label-error">{state.error?.title}</p>
</div>
<div className="form-group">
<label htmlFor="order">Order</label>
<input defaultValue={1} type="number" name="order" id="order" className={state.error?.order ? "border-red-400" : ""} />
<p className="form-label-error">{state.error?.order}</p>
</div>
<input type="hidden" name="order" value="1" />
<input type="hidden" name="customPageUuid" value={customPage} />
<SubmitButton>add to page <LinkIcon className="ml-2 button-icon" /></SubmitButton>