From 9025085447121a60cd6da80f7f5edf3ddf42d8e2 Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Sat, 2 Oct 2021 12:58:21 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AE=20Functional=20scaffold=20for=20Dn?= =?UTF-8?q?D=20grid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/components/ComicDetail.tsx | 77 ++------------------------- src/client/components/DnD.tsx | 75 ++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 73 deletions(-) create mode 100644 src/client/components/DnD.tsx diff --git a/src/client/components/ComicDetail.tsx b/src/client/components/ComicDetail.tsx index d521dac..c9b43c3 100644 --- a/src/client/components/ComicDetail.tsx +++ b/src/client/components/ComicDetail.tsx @@ -19,27 +19,7 @@ import { getComicBookDetailById } from "../actions/comicinfo.actions"; import { detectTradePaperbacks } from "../shared/utils/tradepaperback.utils"; import dayjs from "dayjs"; const prettyBytes = require("pretty-bytes"); -// https://codesandbox.io/s/dndkit-sortable-image-grid-py6ve?file=/src/Grid.jsx -import { - DndContext, - closestCenter, - MouseSensor, - TouchSensor, - DragOverlay, - useSensor, - useSensors, -} from "@dnd-kit/core"; -import { - arrayMove, - SortableContext, - rectSortingStrategy, -} from "@dnd-kit/sortable"; - -import { Grid } from "./Grid"; -import { SortableCover } from "./SortableCover"; -import { Cover } from "./Cover"; -import photos from "./photos.json"; - +import { DnD } from "./DnD"; import { useDispatch, useSelector } from "react-redux"; import { removeLeadingPeriod, @@ -62,10 +42,6 @@ export const ComicDetail = ({}: ComicDetailProps): ReactElement => { const [visible, setVisible] = useState(false); const [slidingPanelContentId, setSlidingPanelContentId] = useState(""); - const [items, setItems] = useState(photos); - const [activeId, setActiveId] = useState(null); - const sensors = useSensors(useSensor(MouseSensor), useSensor(TouchSensor)); - const comicVineSearchResults = useSelector( (state: RootState) => state.comicInfo.searchResults, ); @@ -158,29 +134,6 @@ export const ComicDetail = ({}: ComicDetailProps): ReactElement => { }, }; - function handleDragStart(event) { - setActiveId(event.active.id); - } - - function handleDragEnd(event) { - const { active, over } = event; - - if (active.id !== over.id) { - setItems((items) => { - const oldIndex = items.indexOf(active.id); - const newIndex = items.indexOf(over.id); - - return arrayMove(items, oldIndex, newIndex); - }); - } - console.log(items); - setActiveId(null); - } - - function handleDragCancel() { - setActiveId(null); - } - const openDrawerWithCVMatches = useCallback(() => { dispatch(fetchComicVineMatches(comicBookDetailData)); setSlidingPanelContentId("CVMatches"); @@ -262,31 +215,9 @@ export const ComicDetail = ({}: ComicDetailProps): ReactElement => { id: 2, icon: , name: "Archive Operations", - content: ( -
- - - - {items.map((url, index) => ( - - ))} - - - - - {activeId ? ( - - ) : null} - - -
- ), + content:
+ +
, }, { id: 3, diff --git a/src/client/components/DnD.tsx b/src/client/components/DnD.tsx new file mode 100644 index 0000000..7b7d89f --- /dev/null +++ b/src/client/components/DnD.tsx @@ -0,0 +1,75 @@ +import React, { ReactElement, useState } from "react"; +// https://codesandbox.io/s/dndkit-sortable-image-grid-py6ve?file=/src/Grid.jsx +import { + DndContext, + closestCenter, + MouseSensor, + TouchSensor, + DragOverlay, + useSensor, + useSensors, +} from "@dnd-kit/core"; +import { + arrayMove, + SortableContext, + rectSortingStrategy, +} from "@dnd-kit/sortable"; + +import { Grid } from "./Grid"; +import { SortableCover } from "./SortableCover"; +import { Cover } from "./Cover"; +import photos from "./photos.json"; + +export const DnD = () => { + const [items, setItems] = useState(photos); + const [activeId, setActiveId] = useState(null); + const sensors = useSensors(useSensor(MouseSensor), useSensor(TouchSensor)); + + function handleDragStart(event) { + setActiveId(event.active.id); + } + + function handleDragEnd(event) { + const { active, over } = event; + + if (active.id !== over.id) { + setItems((items) => { + const oldIndex = items.indexOf(active.id); + const newIndex = items.indexOf(over.id); + + return arrayMove(items, oldIndex, newIndex); + }); + } + console.log(items); + setActiveId(null); + } + + function handleDragCancel() { + setActiveId(null); + } + return ( + + + + {items.map((url, index) => ( + + ))} + + + + + {activeId ? ( + + ) : null} + + + ); +}; + +export default DnD;