Advertisement
Guest User

fileReactDnd

a guest
Oct 13th, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useRef } from "react";
  2. import PropTypes from "prop-types";
  3. import { useDrag, useDrop, DragSource, DropTarget } from "react-dnd";
  4.  
  5. import ItemTypes from "../../../../constants/ItemTypes";
  6. import QuestionForm from "./QuestionForm";
  7. import QuestionDetails from "./QuestionDetails";
  8.  
  9. /**
  10.  * Implements the drag source contract.
  11.  */
  12.  
  13. const QuestionGroupItem = ({
  14.   editQuestion,
  15.   activated,
  16.   groupId,
  17.   type,
  18.   name,
  19.   id,
  20.   deleteQuestion,
  21.   copyQuestion,
  22.   activateItem,
  23.   question,
  24.   diagnosticId,
  25. }) => {
  26.   const [stopPropagation, setStopPropagation] = useState(false);
  27.  
  28.  
  29.   ////////////////////////////////////////////////////////
  30.   ////  DRAG ////
  31.   ///////////////////////////////////////////////////////
  32.  
  33.   const [, drag] = useDrag({
  34.     item: {
  35.       type: ItemTypes.QUESTION,
  36.     },
  37.     beginDrag() {
  38.       return {
  39.         id: id,
  40.         groupId: groupId,
  41.       };
  42.     },
  43.  
  44.     endDrag(monitor) {
  45.       const dropResult = monitor.getDropResult();
  46.       if (!dropResult || !dropResult.id) return;
  47.       const draggedId = monitor.getItem().id;
  48.       const draggedGId = monitor.getItem().groupId;
  49.       if (draggedGId !== dropResult.id) {
  50.         props.changeQuestionGroup(draggedGId, draggedId, dropResult.id);
  51.       }
  52.     },
  53.     collect: (monitor) => ({
  54.       opacity: monitor.isDragging() ? 0.5 : 1,
  55.     }),
  56.   });
  57.  
  58.   ////////////////////////////////////////////////////////
  59.   ////  DROP ////
  60.   ///////////////////////////////////////////////////////
  61.   const [, drop] = useDrop({
  62.     accept: [ItemTypes.QUESTION, ItemTypes.CONTROL],
  63.     hover(monitor) {
  64.       if (monitor.type !== ItemTypes.QUESTION) return;
  65.       const draggedId = monitor.getItem().id;
  66.       const draggedGId = monitor.getItem().groupId;
  67.       if (draggedId !== id && draggedGId === groupId) {
  68.         moveQuestion(draggedGId, draggedId, id);
  69.       }
  70.     },
  71.  
  72.     canDrop(monitor) {
  73.       if (monitor.type === ItemTypes.QUESTION) {
  74.         const draggedGId = monitor.getItem().groupId;
  75.         return draggedGId === groupId;
  76.       } else if (monitor.type() === ItemTypes.CONTROL) {
  77.         return monitor.isOver() && monitor.getItem().type !== "advice";
  78.       }
  79.     },
  80.  
  81.     drop(monitor) {
  82.       if (monitor.type() === ItemTypes.CONTROL) {
  83.         return {
  84.           id: groupId,
  85.           position: id,
  86.         };
  87.       }
  88.     },
  89.   });
  90.  
  91.   const ref = useRef(null);
  92.   drop(drag(ref));
  93.  
  94.   const displaySetting = (display) => {
  95.     return display === undefined ? "one-col" : display;
  96.   };
  97.  
  98.   const handleClick = (e, id) => {
  99.     e.stopPropagation();
  100.     if (!stopPropagation) {
  101.       activateItem(id);
  102.     }
  103.   };
  104.  
  105.   const handleClickCopy = (e) => {
  106.     e.stopPropagation();
  107.     setStopPropagation(true);
  108.     copyQuestion(id, groupId);
  109.     setTimeout(() => {
  110.       setStopPropagation(false);
  111.     }, 300);
  112.   };
  113.  
  114.   let element;
  115.  
  116.   if (activated) {
  117.     element = <QuestionForm question={question} diagnosticId={diagnosticId} />;
  118.   } else {
  119.     element = (
  120.       <li ref={ref} className="question-group-item display-flex">
  121.         <QuestionDetails
  122.           id={id}
  123.           name={name}
  124.           type={type}
  125.           handleClick={(e) => handleClick(e)}
  126.           editQuestion={editQuestion}
  127.         >
  128.           <button title="Dupliquer" className="bt-action" onClick={(e) => handleClickCopy(e)}>
  129.             <span className="bt-action-icon bt-action-duplicate2"></span>
  130.           </button>
  131.           <button
  132.             title="Supprimer"
  133.             className="bt-action"
  134.             onClick={() => {
  135.               if (confirm("Êtes-vous sûr de vouloir supprimer cet élément ?"))
  136.                 deleteQuestion(id, groupId);
  137.             }}
  138.           >
  139.             <span className="bt-action-icon bt-action-erase2"></span>
  140.           </button>
  141.         </QuestionDetails>
  142.       </li>
  143.     );
  144.   }
  145.  
  146.   return element;
  147. };
  148.  
  149. export default QuestionGroupItem;
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement