From 23bf495a5a49c20e0073dc65345dcf74c1ba53cd Mon Sep 17 00:00:00 2001 From: gianantoniopini <63844628+gianantoniopini@users.noreply.github.com> Date: Tue, 9 Feb 2021 05:59:32 +0100 Subject: [PATCH] List component: added support for drag & drop --- src/components/builder/lists/List.js | 92 +++++++++++++++------- src/components/builder/lists/ListItem.js | 99 ++++++++++++++---------- 2 files changed, 123 insertions(+), 68 deletions(-) diff --git a/src/components/builder/lists/List.js b/src/components/builder/lists/List.js index 7a5f2163..bfe77b9e 100644 --- a/src/components/builder/lists/List.js +++ b/src/components/builder/lists/List.js @@ -1,10 +1,11 @@ import { get, isEmpty } from 'lodash'; import React, { memo, useContext } from 'react'; +import { DragDropContext, Droppable } from 'react-beautiful-dnd'; import { useTranslation } from 'react-i18next'; import { MdAdd } from 'react-icons/md'; import ModalContext from '../../../contexts/ModalContext'; -import { useSelector } from '../../../contexts/ResumeContext'; -import { formatDateRange } from '../../../utils'; +import { useDispatch, useSelector } from '../../../contexts/ResumeContext'; +import { formatDateRange, reorder } from '../../../utils'; import Button from '../../shared/Button'; import EmptyList from './EmptyList'; import styles from './List.module.css'; @@ -24,42 +25,79 @@ const List = ({ const { t, i18n } = useTranslation(); const items = useSelector(path, []); const { emitter } = useContext(ModalContext); + const dispatch = useDispatch(); const handleAdd = () => emitter.emit(event); const handleEdit = (data) => emitter.emit(event, data); + const onDragEnd = (result) => { + const { source, destination } = result; + + if (!destination) { + return; + } + + const sourceDroppableId = source.droppableId; + const destinationDroppableId = destination.droppableId; + if (sourceDroppableId !== destinationDroppableId) { + return; + } + + const itemsReordered = reorder(items, source.index, destination.index); + dispatch({ + type: 'on_input', + payload: { + path, + value: itemsReordered, + }, + }); + }; + return (