forked from globalfund/data-explorer-client
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
145 additions
and
6 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
src/app/modules/report-module/components/placeholder/index.tsx
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,134 @@ | ||
import React from "react"; | ||
import { PlaceholderProps } from "app/modules/report-module/views/create/data"; | ||
import { ReportElementsType } from "app/modules/report-module/components/right-panel-create-view"; | ||
import { useDrop } from "react-dnd"; | ||
import { isDividerOrRowFrameDraggingAtom } from "app/state/recoil/atoms"; | ||
import { useRecoilValue } from "recoil"; | ||
import { v4 } from "uuid"; | ||
|
||
const PlaceHolder = (props: PlaceholderProps) => { | ||
const moveCard = React.useCallback((itemId: string) => { | ||
props.updateFramesArray((draft) => { | ||
const dragIndex = draft.findIndex((frame) => frame.id === itemId); | ||
|
||
const dropIndex = | ||
props.index ?? draft.findIndex((frame) => frame.id === props.rowId) + 1; | ||
|
||
const fakeId = v4(); | ||
const tempItem = draft[dragIndex]; | ||
draft[dragIndex].id = fakeId; | ||
|
||
draft.splice(dropIndex, 0, tempItem); | ||
const fakeIndex = draft.findIndex((frame) => frame.id === fakeId); | ||
draft.splice(fakeIndex, 1); | ||
}); | ||
}, []); | ||
const [{ isOver, handlerId, item: dragItem }, drop] = useDrop(() => ({ | ||
// The type (or types) to accept - strings or symbols | ||
accept: [ | ||
ReportElementsType.DIVIDER, | ||
ReportElementsType.ROWFRAME, | ||
ReportElementsType.ROW, | ||
], | ||
// Props to collect | ||
collect: (monitor) => ({ | ||
isOver: monitor.isOver(), | ||
canDrop: monitor.canDrop(), | ||
item: monitor.getItem(), | ||
handlerId: monitor.getHandlerId(), | ||
}), | ||
drop: (item: any, monitor) => { | ||
if (item.type === ReportElementsType.ROW) { | ||
moveCard(item.id); | ||
} else { | ||
props.updateFramesArray((draft) => { | ||
const tempIndex = | ||
props.index ?? | ||
draft.findIndex((frame) => frame.id === props.rowId) + 1; | ||
|
||
const id = v4(); | ||
draft.splice(tempIndex, 0, { | ||
id, | ||
frame: { | ||
rowId: id, | ||
rowIndex: tempIndex, | ||
|
||
type: item.type, | ||
}, | ||
content: | ||
item.type === ReportElementsType.ROWFRAME ? [] : ["divider"], | ||
contentWidths: [], | ||
contentHeights: [], | ||
contentTypes: | ||
item.type === ReportElementsType.ROWFRAME ? [] : ["divider"], | ||
structure: null, | ||
}); | ||
}); | ||
} | ||
}, | ||
})); | ||
|
||
const isItemDragging = useRecoilValue(isDividerOrRowFrameDraggingAtom); | ||
|
||
const itemDragIndex = props.framesArray.findIndex( | ||
(frame) => frame.id === isItemDragging.rowId | ||
); | ||
|
||
const placeholderIndex = | ||
props.index ?? | ||
props.framesArray.findIndex((frame) => frame.id === props.rowId) + 1; | ||
|
||
const dragIndex = props.framesArray.findIndex( | ||
(frame) => frame.id === (dragItem as any)?.id | ||
); | ||
|
||
const placeholderActive = () => { | ||
if (isOver) { | ||
if (dragIndex === -1) { | ||
return true; | ||
} | ||
if (placeholderIndex === dragIndex) { | ||
return false; | ||
} | ||
if (placeholderIndex - 1 === dragIndex) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
const isDroppable = () => { | ||
if (isItemDragging.state) { | ||
if (itemDragIndex === -1) { | ||
return true; | ||
} | ||
if (placeholderIndex === itemDragIndex) { | ||
return false; | ||
} | ||
if (placeholderIndex - 1 === itemDragIndex) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
return ( | ||
<div | ||
data-cy="report-row-placeholder" | ||
data-handler-id={handlerId} | ||
ref={drop} | ||
css={` | ||
width: 100%; | ||
height: 10px; | ||
display: ${isItemDragging.state ? "block" : "none"}; | ||
background-color: ${placeholderActive() ? "#6061E5" : "#262c34"}; | ||
opacity: ${isDroppable() ? 1 : 0.5}; | ||
margin-bottom: 10px; | ||
`} | ||
/> | ||
); | ||
}; | ||
|
||
export default PlaceHolder; |
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