QuestionsList.tsx 2.35 KB
Newer Older
jang dong hyeok's avatar
dnd    
jang dong hyeok committed
1
import React, { useRef, useState, DragEvent } from "react";
Yoon, Daeki's avatar
Yoon, Daeki committed
2
3
import { CreateQuestionData } from "../types";
import { Question } from "./Question";
jang dong hyeok's avatar
dnd    
jang dong hyeok committed
4
import { DraggableItem } from "./DraggableItem";
Yoon, Daeki's avatar
Yoon, Daeki committed
5
6
7
8
9
10
11
12
13
14
15
16

type Props = {
  questions: CreateQuestionData[];
  handleQuestion: Function;
  deleteQuestion: Function;
};

export const QuestionsList = ({
  questions,
  handleQuestion,
  deleteQuestion,
}: Props) => {
jang dong hyeok's avatar
dnd    
jang dong hyeok committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  const [items, setItems] = useState(questions);
  const currentItem = useRef<HTMLDivElement>();
  const [currentItemId, setCurrentItemId] = useState<string>("");

  function arrayMove(questionArr: any[], oldIndex: number, newIndex: number) {
    questionArr.splice(newIndex, 0, questionArr.splice(oldIndex, 1)[0]);
    return questionArr; // for testing
  }

  const onDragStart = (e: DragEvent<HTMLDivElement>) => {
    currentItem.current = e.currentTarget;
    const { id } = e.currentTarget;
    const index = items.findIndex(
      (question: CreateQuestionData) => question._id === String(id)
    );
    console.log(id, "drag start, ", "start index: ", index);
    setCurrentItemId(id);
  };

  const onDrop = (e: DragEvent<HTMLDivElement>) => {
    e.preventDefault();
    // const { id } = e.target;
    const { id } = e.currentTarget;
    const curId = e.currentTarget.id;
    const currentItemIndex = items.findIndex(
      (question: CreateQuestionData) => question._id === curId
    );
    const dropIndex = items.findIndex(
      (question: CreateQuestionData) => question.id === String(id)
    );
    console.log(
      id,
      "drop, ",
      "current question:",
      curId,
      ", drop index:",
      dropIndex,
      ", currentItem index:",
      currentItemIndex
    );

    const newArray = arrayMove(questions, currentItemIndex, dropIndex);
    newArray.forEach((question, index) => {
      question.order = index;
    });
    console.log("new array=", newArray);
    setItems([...newArray]);
  };

Yoon, Daeki's avatar
Yoon, Daeki committed
66
67
68
  return (
    <>
      {questions.map((question) => (
jang dong hyeok's avatar
dnd    
jang dong hyeok committed
69
        <DraggableItem
Yoon, Daeki's avatar
Yoon, Daeki committed
70
          key={question._id}
jang dong hyeok's avatar
dnd    
jang dong hyeok committed
71
72
73
74
75
76
77
78
79
80
81
82
          currentItemId={currentItemId}
          question={question}
          onDragStart={onDragStart}
          onDrop={onDrop}
        >
          <Question
            key={question._id}
            element={question}
            handleQuestion={handleQuestion}
            deleteQuestion={deleteQuestion}
          />
        </DraggableItem>
Yoon, Daeki's avatar
Yoon, Daeki committed
83
84
85
86
      ))}
    </>
  );
};