From b7160bff99c5906db8bfbcaabf0fc0af4c983117 Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Mon, 4 Dec 2023 18:45:47 -0500 Subject: [PATCH 01/16] feat: add drag n drop functionality to section cards --- src/course-outline/CourseOutline.jsx | 90 +++++++++++++++++++--------- src/course-outline/data/api.js | 15 +++++ src/course-outline/data/slice.js | 4 ++ src/course-outline/data/thunk.js | 23 +++++++ src/course-outline/hooks.jsx | 6 ++ 5 files changed, 110 insertions(+), 28 deletions(-) diff --git a/src/course-outline/CourseOutline.jsx b/src/course-outline/CourseOutline.jsx index eabe680764..8c8e1d20e5 100644 --- a/src/course-outline/CourseOutline.jsx +++ b/src/course-outline/CourseOutline.jsx @@ -80,6 +80,7 @@ const CourseOutline = ({ courseId }) => { handleDuplicateSubsectionSubmit, handleNewSectionSubmit, handleNewSubsectionSubmit, + handleDragNDrop, } = useCourseOutline({ courseId }); useEffect(() => { @@ -91,6 +92,31 @@ const CourseOutline = ({ courseId }) => { title: processingNotificationTitle, } = useSelector(getProcessingNotification); + const dragItem = useRef(); + const dragOverItem = useRef(); + + const dragStart = (e, position) => { + dragItem.current = position; + }; + + const dragEnter = (e, position) => { + dragOverItem.current = position; + }; + + const drop = (e) => { + const copyListItems = [...sectionsList]; + const dragItemContent = copyListItems[dragItem.current]; + copyListItems.splice(dragItem.current, 1); + copyListItems.splice(dragOverItem.current, 0, dragItemContent); + dragItem.current = null; + dragOverItem.current = null; + const idList = []; + for (let i = 0; i < copyListItems.length; i++) { + idList.push(copyListItems[i].id); + } + handleDragNDrop(idList, copyListItems); + }; + if (isLoading) { // eslint-disable-next-line react/jsx-no-useless-fragment return <>; @@ -152,35 +178,43 @@ const CourseOutline = ({ courseId }) => {
{sectionsList.length ? ( <> - {sectionsList.map((section) => ( - ( +
e.preventDefault()} + onDragStart={(e) => dragStart(e, index)} + onDragEnter={(e) => dragEnter(e, index)} + onDrop={(e) => drop(e)} + draggable > - {section.childInfo.children.map((subsection) => ( - - ))} - + + {section.childInfo.children.map((subsection) => ( + + ))} + +
))}