Skip to content

Commit

Permalink
Merge pull request #5 from JollyGrin/feat/hand
Browse files Browse the repository at this point in the history
feat/hand working
  • Loading branch information
JollyGrin authored Sep 8, 2024
2 parents 5c466b8 + 37707bd commit cbd6575
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 463 deletions.
2 changes: 2 additions & 0 deletions src/components/molecules/DropGridItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { useDroppable } from "@dnd-kit/core";
export const DroppableGridItem = (props: {
children: ReactNode;
id: string;
gridIndex: number;
}) => {
const { isOver, setNodeRef } = useDroppable({
id: "droppable-" + props.id,
data: { gridIndex: props.gridIndex },
});

return (
Expand Down
7 changes: 4 additions & 3 deletions src/components/organisms/GameBoard/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ export const GameFooter = (props: GameStateActions) => {
console.log({ cardsInHand });
return (
<div
data-testid="footer"
style={{
height: LAYOUT_HEIGHTS.footer,
maxWidth: "100vw",
overflowX: "auto",
}}
>
<Grid h="100%" gridTemplateColumns="100px 100px 1fr">
<p>hjkl</p>
<p>hjkl</p>
<DroppableGridItem id={gridIndex.toString()}>
<p>Grave</p>
<p>Deck</p>
<DroppableGridItem id={gridIndex.toString()} gridIndex={gridIndex}>
<SortableContext
id={`grid-${gridIndex}`}
items={cardsInHand?.map(
Expand Down
41 changes: 35 additions & 6 deletions src/components/organisms/GameBoard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { CardAtlas } from "@/components/atoms/mock-cards/atlas";
import { SortableContext, rectSortingStrategy } from "@dnd-kit/sortable";
import { GameLayout } from "./Layout";
import { DroppableGridItem } from "@/components/molecules/DropGridItem";
import { closestCorners, DndContext, DragOverlay } from "@dnd-kit/core";
import {
closestCenter,
closestCorners,
CollisionDetection,
DndContext,
DragOverlay,
} from "@dnd-kit/core";
import { useHandleDrag } from "./useHandleDrag";
import { SorceryCard } from "@/types/card";
import { CardImage } from "@/components/atoms/mock-cards/card";
Expand All @@ -22,29 +28,52 @@ export const GameBoard = ({ gridItems, setGridItems }: GameStateActions) => {
setGridItems,
});

const collision: CollisionDetection = (props) => {
// Access the current translated Y position of the dragged item
const currentY = props?.active?.rect?.current?.translated?.top;

// Get the height of the viewport
const viewportHeight = window.innerHeight;

// Check if the current Y position is within the bottom 170px of the page
const isInFooter = currentY && currentY > viewportHeight - 170;

// Check if the current Y position is within the bottom 170px of the page
// If the item is in the bottom 170px, use closestCenter for the footer
if (isInFooter) {
console.log("isInFooter");
return closestCenter(props);
}

return closestCorners(props);
};

return (
<DndContext
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
collisionDetection={closestCorners}
collisionDetection={collision}
>
<GameLayout {...{ gridItems, setGridItems }}>
{gridItems?.slice(0, 20)?.map((cards, gridIndex) => (
<DroppableGridItem
key={"grid-" + gridIndex}
id={gridIndex.toString()}
gridIndex={gridIndex}
>
<SortableContext
id={`grid-${gridIndex}`}
items={cards.map(
(_, cardIndex) => `card-${gridIndex}-${cardIndex}`,
)}
items={cards.map((card) => card.id)}
// items={cards.map(
// (card, cardIndex) => `card-${gridIndex}-${cardIndex}`,
// )}
strategy={rectSortingStrategy}
>
{cards.map((card, cardIndex) => (
<SortableItem
key={`card-${gridIndex}-${cardIndex}`}
id={`card-${gridIndex}-${cardIndex}`}
// id={`card-${gridIndex}-${cardIndex}`}
id={card.id}
gridIndex={gridIndex}
index={cardIndex}
>
Expand Down
10 changes: 6 additions & 4 deletions src/components/organisms/GameBoard/useHandleDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ export const useHandleDrag = ({

if (over) {
const originIndex = parseInt(active.data.current?.gridIndex, 10);
const destinationIndex = parseInt(
(over?.id as string)?.split("-")[1],
10, // base 10 number
);
const destinationIndex = over?.data?.current?.gridIndex;

// const destinationIndex = parseInt(
// (over?.id as string)?.split("-")[1],
// 10, // base 10 number
// );

if (originIndex === destinationIndex) {
const updatedGrid = [...gridItems];
Expand Down
142 changes: 0 additions & 142 deletions src/pages/dnd.tsx

This file was deleted.

Loading

0 comments on commit cbd6575

Please sign in to comment.