-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/#35
- Loading branch information
Showing
20 changed files
with
1,072 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
"use client"; | ||
import React, { useMemo, useState } from "react"; | ||
import { DragDropContext, DropResult, Draggable } from "react-beautiful-dnd"; | ||
import Image from "next/image"; | ||
import CardWithCourse from "@/components/common/Cards/CardWithCourse"; | ||
import { SheetWithCourse } from "@/components/common/BottomSheet/SheetWithCourse"; | ||
import { iconInfo } from "@/lib/utils"; | ||
import { StrictModeDroppable } from "./Droppable"; | ||
|
||
export type OrderType = "food" | "dessert" | "beer" | "play"; | ||
|
||
export type ValueType = { | ||
globalIndex: number; | ||
title: string; | ||
type: OrderType; | ||
icon: string; | ||
}; | ||
|
||
export type ColumnsType = { | ||
course: { | ||
id: "course"; | ||
list: Record<OrderType, ValueType[]>; | ||
}; | ||
}; | ||
|
||
type DragAndDropProps = { | ||
initialColumns: ColumnsType; | ||
}; | ||
|
||
const updateColumnsOnDelete = ( | ||
prevColumns: ColumnsType, | ||
globalIndexToDelete: number | ||
): ColumnsType => { | ||
const updatedList: Record<OrderType, ValueType[]> = { | ||
food: [], | ||
dessert: [], | ||
beer: [], | ||
play: [], | ||
}; | ||
|
||
Object.values(prevColumns.course.list) | ||
.flat() | ||
.forEach((item) => { | ||
if (item.globalIndex !== globalIndexToDelete) { | ||
updatedList[item.type].push(item); | ||
} | ||
}); | ||
|
||
return { | ||
...prevColumns, | ||
course: { | ||
...prevColumns.course, | ||
list: updatedList, | ||
}, | ||
}; | ||
}; | ||
|
||
const reorder = ( | ||
list: ValueType[], | ||
startIndex: number, | ||
endIndex: number | ||
): ValueType[] => { | ||
const result = Array.from(list); | ||
const [removed] = result.splice(startIndex, 1); | ||
|
||
result.splice(endIndex, 0, removed); | ||
return result; | ||
}; | ||
|
||
const DragAndDropArea: React.FC<DragAndDropProps> = ({ initialColumns }) => { | ||
const [columns, setColumns] = useState(initialColumns); | ||
const [itemCount, setItemCount] = useState( | ||
Object.values(initialColumns.course.list).flat().length | ||
); | ||
|
||
const allItems = useMemo(() => { | ||
return Object.values(columns.course.list) | ||
.flat() | ||
.sort((a, b) => a.globalIndex - b.globalIndex); | ||
}, [columns.course.list]); | ||
|
||
const onDragEnd = (result: DropResult) => { | ||
if (!result.destination) { | ||
return; | ||
} | ||
|
||
const sourceIndex = result.source.index; | ||
const destinationIndex = result.destination.index; | ||
|
||
const reorderedItems = reorder(allItems, sourceIndex, destinationIndex); | ||
|
||
const newColumns = { ...columns }; | ||
const newList: Record<OrderType, ValueType[]> = { | ||
food: [], | ||
dessert: [], | ||
beer: [], | ||
play: [], | ||
}; | ||
|
||
reorderedItems.forEach((item, index) => { | ||
item.globalIndex = index; | ||
newList[item.type].push(item); | ||
}); | ||
|
||
newColumns.course.list = newList; | ||
setColumns(newColumns); | ||
}; | ||
|
||
const handleItemDelete = (globalIndexToDelete: number) => { | ||
setColumns((prevColumns) => | ||
updateColumnsOnDelete(prevColumns, globalIndexToDelete) | ||
); | ||
setItemCount(itemCount - 1); | ||
}; | ||
|
||
const handleItemClick = (type: OrderType) => { | ||
const icon = iconInfo.find((item) => item.type === type); | ||
if (!icon) return; | ||
|
||
const newItem = { | ||
globalIndex: itemCount, | ||
title: `${icon.label}`, | ||
type: icon.type, | ||
icon: icon.icon, | ||
}; | ||
|
||
setColumns((prevColumns) => ({ | ||
...prevColumns, | ||
course: { | ||
...prevColumns.course, | ||
list: { | ||
...prevColumns.course.list, | ||
[type]: [...prevColumns.course.list[type], newItem], | ||
}, | ||
}, | ||
})); | ||
setItemCount(itemCount + 1); | ||
}; | ||
|
||
return ( | ||
<> | ||
<DragDropContext onDragEnd={onDragEnd}> | ||
<StrictModeDroppable droppableId="droppable"> | ||
{(provided) => ( | ||
<div | ||
{...provided.droppableProps} | ||
ref={provided.innerRef} | ||
className="flex flex-col gap-y-[16px]" | ||
> | ||
{allItems.map((item: ValueType, index: number) => ( | ||
<Draggable | ||
key={item.globalIndex} | ||
draggableId={`item-${item.type}-${item.globalIndex}`} | ||
index={index} | ||
> | ||
{(provided) => ( | ||
<div | ||
className="flex flex-row items-center justify-center w-[335px] h-[56px] gap-x-[16px]" | ||
ref={provided.innerRef} | ||
{...provided.draggableProps} | ||
{...provided.dragHandleProps} | ||
> | ||
<Image | ||
src="/svg/ic_x.svg" | ||
alt="x" | ||
width={16} | ||
height={16} | ||
priority | ||
unoptimized | ||
onClick={() => handleItemDelete(item.globalIndex)} | ||
/> | ||
<CardWithCourse item={item} /> | ||
</div> | ||
)} | ||
</Draggable> | ||
))} | ||
{provided.placeholder} | ||
</div> | ||
)} | ||
</StrictModeDroppable> | ||
</DragDropContext> | ||
{itemCount < 5 && <SheetWithCourse handleItemClick={handleItemClick} />} | ||
</> | ||
); | ||
}; | ||
|
||
export default DragAndDropArea; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useEffect, useState } from "react"; | ||
import { Droppable, DroppableProps } from "react-beautiful-dnd"; | ||
|
||
export const StrictModeDroppable = ({ children, ...props }: DroppableProps) => { | ||
const [enabled, setEnabled] = useState(false); | ||
|
||
useEffect(() => { | ||
const animation = requestAnimationFrame(() => setEnabled(true)); | ||
|
||
return () => { | ||
cancelAnimationFrame(animation); | ||
setEnabled(false); | ||
}; | ||
}, []); | ||
|
||
if (!enabled) { | ||
return null; | ||
} | ||
|
||
return <Droppable {...props}>{children}</Droppable>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from "react"; | ||
import DragAndDropArea, { ColumnsType } from "./_components/DragAndDropArea"; | ||
|
||
// ์ฌ์ฉ์๊ฐ ์ค์ ํ ๋ฐ์ดํฐ๋ผ๊ณ ๊ฐ์ | ||
const initialColumns: ColumnsType = { | ||
course: { | ||
id: "course", | ||
list: { | ||
food: [ | ||
{ globalIndex: 0, title: "์์", type: "food", icon: "๐" }, | ||
{ globalIndex: 2, title: "์์", type: "food", icon: "๐" }, | ||
], | ||
dessert: [ | ||
{ globalIndex: 1, title: "๋์ ํธ", type: "dessert", icon: "๐ฅจ" }, | ||
], | ||
beer: [], | ||
play: [], | ||
}, | ||
}, | ||
}; | ||
|
||
const EditCoursePage = () => { | ||
return <DragAndDropArea initialColumns={initialColumns} />; | ||
}; | ||
|
||
export default EditCoursePage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.