Skip to content

Commit

Permalink
fix: Fix DragAndDropArea with Adding CopyColumns
Browse files Browse the repository at this point in the history
  • Loading branch information
yihyun-kim1 committed Aug 19, 2024
1 parent 6bc4c01 commit dcbddc6
Showing 1 changed file with 75 additions and 40 deletions.
115 changes: 75 additions & 40 deletions app/edit-course/_components/DragAndDropArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ const generateUniqueTitles = (
.sort((a, b) => a.number - b.number);

const newNumber =
sortedItems.findIndex((i) => i.scheduleId === item.scheduleId) + 1;
sortedItems.length > 1
? sortedItems.findIndex((i) => i.scheduleId === item.scheduleId) + 1
: 0;

return {
...item,
name:
sortedItems.length > 1
? `${item.name.split(" ")[0]} ${newNumber}์ฐจ`
newNumber > 0
? `${item.name.split(" ")[0]}${newNumber}์ฐจ`
: item.name.split(" ")[0],
};
});
Expand Down Expand Up @@ -94,6 +96,7 @@ const DragAndDropArea: React.FC = () => {
const [itemCount, setItemCount] = useState(
categoryList ? categoryList.length : 0
);
const [copyColumns, setCopyColumns] = useState<ScheduleResponse[]>(columns);
const [isDisabled, setIsDisabled] = useState(false);
const toast = useToast();
const searchParams = useSearchParams();
Expand All @@ -110,6 +113,12 @@ const DragAndDropArea: React.FC = () => {
<>&apos;{selectedItemText}&apos; ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์‚ญ์ œํ• ๊นŒ์š”?</>
);

useEffect(() => {
setColumns(categoryList ?? []);
setCopyColumns(categoryList ?? []); // copyColumns๋ฅผ ์ดˆ๊ธฐํ™”
setItemCount((categoryList ?? []).length);
}, [categoryList]);

const handleClickDisabledButton = () => {
if (isDisabled) {
console.log(isDisabled);
Expand All @@ -121,14 +130,26 @@ const DragAndDropArea: React.FC = () => {
return;
};

const generateTempKey = () => {
// 1,000,000 ~ 9,999,999 ๋ฒ”์œ„์˜ ๊ฐ’์„ ์ƒ์„ฑ
return Math.floor(Math.random() * (9999999 - 1000000 + 1)) + 1000000;
};

const isTempKey = (id: number) => {
// 1,000,000 ~ 9,999,999 ๋ฒ”์œ„์˜ ๊ฐ’์„ ๊ฐ€์ง„ `scheduleId`๋Š” ์ž„์‹œ ํ‚ค๋กœ ๊ฐ„์ฃผ
return id >= 1000000 && id <= 9999999;
};

const prepareSchedulesForApi = (
roomUid: string,
schedules: ScheduleResponse[]
): RegisterSchedulesRequest => {
return {
roomUid: roomUid,
schedules: schedules.map((schedule) => ({
scheduleId: schedule.scheduleId,
scheduleId: isTempKey(schedule.scheduleId as number)
? null
: schedule.scheduleId,
name: schedule.name,
type: schedule.type,
sequence: schedule.sequence,
Expand All @@ -138,11 +159,10 @@ const DragAndDropArea: React.FC = () => {

const changeSchedule = async (roomUid: string) => {
try {
const requestPayload = prepareSchedulesForApi(roomUid, columns);
const requestPayload = prepareSchedulesForApi(roomUid, copyColumns);
const response = await scheduleApi.createSchedules(requestPayload);

if (response) {
//setcategory๋ฅผ
router.back();
}
} catch (error) {
Expand All @@ -166,21 +186,23 @@ const DragAndDropArea: React.FC = () => {
const sourceIndex = result.source.index;
const destinationIndex = result.destination.index;

const reorderedItems = reorder(columns, sourceIndex, destinationIndex);
const reorderedItems = reorder(copyColumns, sourceIndex, destinationIndex);

const updatedList = reorderedItems.map((item, index) => ({
...item,
sequence: index + 1,
}));
const updatedList = generateUniqueTitles(
reorderedItems.map((item, index) => ({
...item,
sequence: index + 1,
}))
);

setCategoryList(updatedList);
setCopyColumns(updatedList);
};

const onDeleteButtonClick = (item: ScheduleResponse) => {
setSelectedSequence(item.sequence);
setSelectedItemText(item.name);

const matchedSchedule = roomPlacesInfo?.flatMap(
const matchedSchedule = roomPlacesInfo?.filter(
(place) => place.scheduleId === item.scheduleId
);

Expand All @@ -190,27 +212,17 @@ const DragAndDropArea: React.FC = () => {

setIsModalOpen(true);
};

const handleItemDelete = (sequence: number) => {
const filteredList = columns.filter((item) => item.sequence !== sequence);

const updatedList = generateUniqueTitles(
filteredList.map((item, index) => ({
...item,
sequence: index + 1,
}))
);

setCategoryList(updatedList);
};

const onConfirmDelete = () => {
if (selectedSequence !== null) {
handleItemDelete(selectedSequence);
const selectedItem = copyColumns.find(
(item) => item.sequence === selectedSequence
);
if (selectedItem) {
handleItemDelete(selectedSequence, selectedItem.type);
}
}
setIsModalOpen(false);
};

const onCancelDelete = () => {
setIsModalOpen(false);
};
Expand All @@ -222,26 +234,49 @@ const DragAndDropArea: React.FC = () => {
const icon = iconInfo.find((icon) => icon.type === type);
return icon ? icon.label : type;
};

const handleItemClick = (type: OrderType2) => {
const label = findLabelForType(type);

// ํ•ด๋‹น ํƒ€์ž…์— ์†ํ•œ ๊ธฐ์กด ์•„์ดํ…œ๋“ค ํ•„ํ„ฐ๋ง
const itemsOfType = copyColumns.filter((item) => item.type === type);

// ์ƒˆ๋กœ์šด ์•„์ดํ…œ์˜ sequence๋ฅผ ๊ฒฐ์ • (๊ธฐ์กด ์•„์ดํ…œ ์ˆ˜ + 1)
const newSequence = itemsOfType.length + 1;

const newItem: ScheduleResponse = {
scheduleId: null,
scheduleId: generateTempKey(),
type,
name: `${label} ${columns.length + 1}์ฐจ`,
sequence: columns.length + 1,
name: newSequence > 1 ? `${label} ${newSequence}์ฐจ` : `${label}`,
sequence: newSequence,
};

const existingItems = columns.filter((item) => item.type === type);
// ์•„์ดํ…œ ๋ฆฌ์ŠคํŠธ์— ์ƒˆ๋กœ์šด ์•„์ดํ…œ ์ถ”๊ฐ€ ํ›„ ์ˆœ์„œ์— ๋งž๊ฒŒ ๋ผ๋ฒจ๋ง
const updatedList = generateUniqueTitles([...copyColumns, newItem]);

if (existingItems.length > 0) {
newItem.name = `${label} ${existingItems.length + 1}์ฐจ`;
}
setCopyColumns(updatedList);
};
const handleItemDelete = (sequence: number, type: OrderType2) => {
// ์‚ญ์ œํ•  ํ•ญ๋ชฉ์„ ์ œ์™ธํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ
const filteredList = copyColumns.filter(
(item) => !(item.sequence === sequence && item.type === type)
);

const updatedList = generateUniqueTitles([...columns, newItem]);
// ์‚ญ์ œํ•˜๊ณ  -๋™์ผํ•œ ํƒ€์ž…์˜ ํ•ญ๋ชฉ๋“ค์˜ sequence๋งŒ ๊ณจ๋ผ์„œ ์กฐ์ •
const updatedList = filteredList.map((item) => {
if (item.type === type && item.sequence > sequence) {
return {
...item,
sequence: item.sequence - 1,
name:
item.sequence > 2
? `${item.name.split(" ")[0]}${item.sequence - 1}์ฐจ`
: item.name.split(" ")[0],
};
}
return item;
});

setCategoryList(updatedList);
setCopyColumns(updatedList);
};

return (
Expand All @@ -254,7 +289,7 @@ const DragAndDropArea: React.FC = () => {
ref={provided.innerRef}
className="flex flex-col gap-y-[16px]"
>
{columns.map((item: ScheduleResponse, index: number) => (
{copyColumns.map((item: ScheduleResponse, index: number) => (
<Draggable
key={item.scheduleId}
draggableId={`item-${item.type}-${item.scheduleId}`}
Expand Down

0 comments on commit dcbddc6

Please sign in to comment.