import { DragDropContext, Droppable } from 'react-beautiful-dnd'; import { MdAdd } from 'react-icons/md'; import { get, isEmpty } from 'lodash'; import { useTranslation } from 'react-i18next'; import React, { memo, useContext } from 'react'; import * as styles from './List.module.css'; import { formatDateRange, reorder } from '../../../utils'; import { useDispatch, useSelector } from '../../../contexts/ResumeContext'; import Button from '../../shared/Button'; import EmptyList from './EmptyList'; import ListItem from './ListItem'; import ModalContext from '../../../contexts/ModalContext'; const List = ({ path, title, titlePath, subtitle, subtitlePath, text, textPath, hasDate, event, }) => { 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; } if (source.index === destination.index) { return; } const itemsReordered = reorder(items, source.index, destination.index); dispatch({ type: 'on_input', payload: { path, value: itemsReordered, }, }); }; return (
{isEmpty(items) ? ( ) : ( {(dropProvided) => (
{items.map((x, i) => ( handleEdit(x)} isFirst={i === 0} isLast={i === items.length - 1} index={i} /> ))} {dropProvided.placeholder}
)}
)}
); }; export default memo(List);