From 27b04e620ee80fb508505209da097154d4101dd7 Mon Sep 17 00:00:00 2001 From: Meier Lukas Date: Sun, 7 Jul 2024 12:37:24 +0200 Subject: [PATCH] wip: experiment with dropping to a div and removing item --- .../app/[locale]/boards/(content)/_client.tsx | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/apps/nextjs/src/app/[locale]/boards/(content)/_client.tsx b/apps/nextjs/src/app/[locale]/boards/(content)/_client.tsx index ea80dc68b..1b253e475 100644 --- a/apps/nextjs/src/app/[locale]/boards/(content)/_client.tsx +++ b/apps/nextjs/src/app/[locale]/boards/(content)/_client.tsx @@ -1,10 +1,11 @@ "use client"; -import { useCallback, useRef } from "react"; +import { useCallback, useEffect, useRef } from "react"; import { Box, LoadingOverlay, Stack } from "@mantine/core"; import type { RouterOutputs } from "@homarr/api"; import { clientApi } from "@homarr/api/client"; +import { DDGridStack } from "@homarr/gridstack"; import { BoardCategorySection } from "~/components/board/sections/category-section"; import { BoardEmptySection } from "~/components/board/sections/empty-section"; @@ -40,13 +41,47 @@ export const useUpdateBoard = () => { }; }; +const dd = new DDGridStack(); + export const ClientBoard = () => { const board = useRequiredBoard(); const isReady = useIsBoardReady(); + const { updateBoard } = useUpdateBoard(); const sortedSections = board.sections.sort((sectionA, sectionB) => sectionA.position - sectionB.position); const ref = useRef(null); + const boxRef = useRef(null); + + useEffect(() => { + if (!boxRef.current) return; + dd.droppable(boxRef.current, { + accept: () => true, + }) + .on(boxRef.current, "dropover", (event, el, helper) => { + console.log("dropover", event, el, helper); + }) + .on(boxRef.current, "dropout", (event, el, helper) => { + console.log("dropout", event, el, helper); + }) + .on(boxRef.current, "drop", (event, el, helper) => { + console.log("drop", event, el, helper); + const itemId = el.getAttribute("data-id"); + if (!itemId) return; + + // Same as removeItem + updateBoard((previous) => { + return { + ...previous, + // Filter removed item out of items array + sections: previous.sections.map((section) => ({ + ...section, + items: section.items.filter((item) => item.id !== itemId), + })), + }; + }); + }); + }, []); return ( @@ -58,6 +93,7 @@ export const ClientBoard = () => { h={fullHeightWithoutHeaderAndFooter} /> + {sortedSections.map((section) => section.kind === "empty" ? (