Guest User

Untitled

a guest
Oct 6th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Fragment, useState, useRef } from "react";
  2. import { useDrag, useDrop } from "react-dnd";
  3. import Modal from "./Modal";
  4. import ITEM_TYPE from "./data/types";
  5.  
  6. import { MDBContainer, MDBBtn, MDBModal, MDBModalBody, MDBModalHeader, MDBModalFooter } from 'mdbreact';
  7.  
  8.  
  9. export default function Item({ item, index, moveItem, status }) {
  10.     const ref = useRef(null);
  11.  
  12.     const [, drop] = useDrop({
  13.         accept: ITEM_TYPE,
  14.         hover(item, monitor) {
  15.             if (!ref.current) {
  16.                 return
  17.             }
  18.             const dragIndex = item.index;
  19.             const hoverIndex = index;
  20.  
  21.             if (dragIndex === hoverIndex) {
  22.                 return
  23.             }
  24.  
  25.             const hoveredRect = ref.current.getBoundingClientRect();
  26.             const hoverMiddleY = (hoveredRect.bottom - hoveredRect.top) / 2;
  27.             const mousePosition = monitor.getClientOffset();
  28.             const hoverClientY = mousePosition.y - hoveredRect.top;
  29.  
  30.             if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
  31.                 return;
  32.             }
  33.  
  34.             if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
  35.                 return;
  36.             }
  37.             moveItem(dragIndex, hoverIndex);
  38.             item.index = hoverIndex;
  39.         },
  40.     });
  41.  
  42.     const [{ isDragging }, drag] = useDrag({
  43.         item: { type: ITEM_TYPE, ...item, index },
  44.         collect: monitor => ({
  45.             isDragging: monitor.isDragging()
  46.         })
  47.     });
  48.  
  49.     drag(drop(ref));
  50.  
  51.    
  52.  
  53.     return (
  54.         <Fragment>
  55.             <div
  56.                 ref={ref}
  57.                 style={{ opacity: isDragging ? 0 : 1 }}
  58.                 className={"item"}
  59.             >
  60.                 <div className={"color-bar"} style={{ backgroundColor: status.color }}/>
  61.                 <p className={"item-title"}>{item.content}</p>
  62.                 <p className={"item-status"}>{item.icon}</p>
  63.             </div>
  64.  
  65.             <Modal item={item} toggle={true} />
  66.  
  67.         </Fragment>
  68.     );
  69. };
  70.  
  71.  
Advertisement
Add Comment
Please, Sign In to add comment