mirror of
https://github.com/traberph/short.git
synced 2026-11-22 22:55:12 +00:00
drag and drop for custom page links
This commit is contained in:
@@ -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"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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 (
|
||||
<div>
|
||||
<div className="flex justify-between max-sm:flex-col mb-10">
|
||||
@@ -112,7 +125,7 @@ export default async function DetailedStats({ params }: PageProps) {
|
||||
<QRCode url={page.shortcode} />
|
||||
</div>
|
||||
|
||||
{customPage ? <CustomPageDashboard customPage={customPage} /> : ""}
|
||||
{customPage ? <CustomPageDashboard customPage={customPage} linkBlocks={linkBlocks || []} shortcodes={shortcodes || []} /> : ""}
|
||||
|
||||
<h2 className="mt-10">Visitors</h2>
|
||||
<p className="my-2">All visits (not unique): {page._count.stat}</p>
|
||||
|
||||
@@ -1,34 +1,57 @@
|
||||
"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">
|
||||
<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">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>
|
||||
@@ -42,10 +65,15 @@ export default async function CustomPageDashboard(params: { customPage: CustomPa
|
||||
<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 || []} />
|
||||
|
||||
</>
|
||||
)
|
||||
</>)
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user