import { FC } from "react"; // react-beautiful-dnd import { DragDropContext, Draggable, DropResult } from "react-beautiful-dnd"; import StrictModeDroppable from "components/dnd/StrictModeDroppable"; // helpers import { ChartDraggable } from "../helpers/draggable"; import { renderDateFormat } from "helpers/date-time.helper"; // types import { IBlockUpdateData, IGanttBlock } from "../types"; export const GanttChartBlocks: FC<{ itemsContainerWidth: number; blocks: IGanttBlock[] | null; sidebarBlockRender: FC; blockRender: FC; blockUpdateHandler: (block: any, payload: IBlockUpdateData) => void; enableLeftDrag: boolean; enableRightDrag: boolean; enableReorder: boolean; }> = ({ itemsContainerWidth, blocks, sidebarBlockRender, blockRender, blockUpdateHandler, enableLeftDrag, enableRightDrag, enableReorder, }) => { const handleChartBlockPosition = ( block: IGanttBlock, totalBlockShifts: number, dragDirection: "left" | "right" ) => { let updatedDate = new Date(); if (dragDirection === "left") { const originalDate = new Date(block.start_date); const currentDay = originalDate.getDate(); updatedDate = new Date(originalDate); updatedDate.setDate(currentDay - totalBlockShifts); } else { const originalDate = new Date(block.target_date); const currentDay = originalDate.getDate(); updatedDate = new Date(originalDate); updatedDate.setDate(currentDay + totalBlockShifts); } blockUpdateHandler(block.data, { [dragDirection === "left" ? "start_date" : "target_date"]: renderDateFormat(updatedDate), }); }; const handleOrderChange = (result: DropResult) => { if (!blocks) return; const { source, destination, draggableId } = result; if (!destination) return; if (source.index === destination.index && document) { // const draggedBlock = document.querySelector(`#${draggableId}`) as HTMLElement; // const blockStyles = window.getComputedStyle(draggedBlock); // console.log(blockStyles.marginLeft); return; } let updatedSortOrder = blocks[source.index].sort_order; if (destination.index === 0) updatedSortOrder = blocks[0].sort_order - 1000; else if (destination.index === blocks.length - 1) updatedSortOrder = blocks[blocks.length - 1].sort_order + 1000; else { const destinationSortingOrder = blocks[destination.index].sort_order; const relativeDestinationSortingOrder = source.index < destination.index ? blocks[destination.index + 1].sort_order : blocks[destination.index - 1].sort_order; updatedSortOrder = (destinationSortingOrder + relativeDestinationSortingOrder) / 2; } const removedElement = blocks.splice(source.index, 1)[0]; blocks.splice(destination.index, 0, removedElement); blockUpdateHandler(removedElement.data, { sort_order: { destinationIndex: destination.index, newSortOrder: updatedSortOrder, sourceIndex: source.index, }, }); }; return (