Question.tsx 5.19 KB
Newer Older
1
2
import React, { useState } from "react";
import { BasicQuestionType, EssayType } from "../types";
Jiwon Yoon's avatar
Jiwon Yoon committed
3
import { questionApi } from "../apis";
4
import { EssayForm } from "./EssayForm";
Jiwon Yoon's avatar
Jiwon Yoon committed
5
6
7
8
9
import { CheckboxForm } from "./CheckboxForm";
import { RadioForm } from "./RadioForm";
import { DropdownForm } from "./DropdownForm";
import { FileForm } from "./FileForm";
import { RatingForm } from "./RatingForm";
10
11
12

type Props = {
  element: BasicQuestionType;
Jiwon Yoon's avatar
Jiwon Yoon committed
13
14
  handleQuestion: (id: string) => void;
  deleteQuestion: (id: string) => void;
15
16
};

Jiwon Yoon's avatar
Jiwon Yoon committed
17
18
19
20
21
22
23
24
25
26
const typeDropDown = new Map([
  ["essay", "주관식"],
  ["radio", "객관식"],
  ["dropdown", "드롭다운"],
  ["checkbox", "체크박스"],
  ["file", "파일"],
  ["rating", "선형"],
  ["grid", "그리드"],
  ["date", "날짜"],
]);
27

Jiwon Yoon's avatar
Jiwon Yoon committed
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
export const Question = ({
  element,
  handleQuestion,
  deleteQuestion,
}: Props) => {
  const handleEdit = () => {
    //setCurrentId해주고 currentId===element._id가 같은 input들만 disabled=false
  };
  async function handleComplete() {
    //db에서 element._id인 애를 findOneAndUpdate() 해준다.
    try {
      const newQuestion: BasicQuestionType = await questionApi.updateQuestion(
        element
      );
      console.log(newQuestion);
      // setSuccess(true);
      // setError("");
    } catch (error) {
      console.log("에러발생");
      // catchErrors(error, setError)
    } finally {
      // setLoading(false);
    }
  }

  function handleSelect(event: React.ChangeEvent<HTMLSelectElement>) {
    const selectedType = event.currentTarget.value;
    console.log(selectedType);
    if (
      selectedType === "radio" ||
      selectedType === "dropdown" ||
      selectedType === "checkbox"
    ) {
      element.content = {
        choices: [
          { text: "선택지1", value: "1" },
          { text: "선택지2", value: "2" },
          { text: "선택지3", value: "3" },
        ],
      };
    } else if (selectedType === "essay") {
      element.content = { choices: [] };
    } else if (selectedType === "rating") {
      element.content = {
        minRateDescription: "가장 낮음",
        maxRateDescription: "가장 높음",
        choices: [
          { text: "1", value: "1" },
          { text: "2", value: "2" },
          { text: "3", value: "3" },
        ],
      };
    }
    element.type = selectedType;
    handleQuestion(element._id);
  }

  function handleQuestionInfo(event: React.ChangeEvent<HTMLInputElement>) {
    const { name, value } = event.currentTarget;
    element[name] = value;
    handleQuestion(element._id);
  }
90

Jiwon Yoon's avatar
Jiwon Yoon committed
91
92
  function handleDelete() {
    deleteQuestion(element._id);
93
94
95
96
97
98
99
  }

  function getContent(element: BasicQuestionType) {
    switch (element.type) {
      case "essay":
        return <EssayForm element={element} />;
      case "radio":
Jiwon Yoon's avatar
Jiwon Yoon committed
100
        return <RadioForm handleQuestion={handleQuestion} element={element} />;
101
      case "checkbox":
Jiwon Yoon's avatar
Jiwon Yoon committed
102
103
104
        return (
          <CheckboxForm handleQuestion={handleQuestion} element={element} />
        );
105
      case "dropdown":
Jiwon Yoon's avatar
Jiwon Yoon committed
106
107
108
        return (
          <DropdownForm handleQuestion={handleQuestion} element={element} />
        );
109
      case "file":
Jiwon Yoon's avatar
Jiwon Yoon committed
110
        return <FileForm element={element} />;
111
      case "rating":
Jiwon Yoon's avatar
Jiwon Yoon committed
112
        return <RatingForm handleQuestion={handleQuestion} element={element} />;
113
114
115
116
117
118
119
120
121
122
123
124
125
      default:
        return <></>;
    }
  }

  return (
    <div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2">
      <div className="flex h-16 w-full place-content-between items-center">
        <input
          type="text"
          name="title"
          id={element._id}
          className="text-xl font-bold ml-6 border-b-2 w-1/2"
Jiwon Yoon's avatar
Jiwon Yoon committed
126
127
128
          placeholder={"Question Title"}
          value={element.title}
          onChange={handleQuestionInfo}
129
130
        ></input>
        <select
Jiwon Yoon's avatar
Jiwon Yoon committed
131
          id={element._id}
132
          name="type"
Jiwon Yoon's avatar
Jiwon Yoon committed
133
          onChange={handleSelect}
134
135
          className="w-36 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-themeColor w-full mr-3 p-2.5"
        >
Jiwon Yoon's avatar
Jiwon Yoon committed
136
137
138
139
140
141
142
143
          {Array.from(typeDropDown.entries()).map(([key, value]) => (
            <option
              id={element._id}
              value={key}
              selected={key === element.type}
            >
              {value}
            </option>
144
145
146
147
148
149
150
151
152
153
          ))}
        </select>
      </div>
      <div className="flex w-full justify-center">
        <input
          type="text"
          name="comment"
          id={element._id}
          className="border w-11/12"
          placeholder="질문에 대한 설명을 입력해주세요"
Jiwon Yoon's avatar
Jiwon Yoon committed
154
155
          value={element.comment}
          onChange={handleQuestionInfo}
156
157
158
159
        ></input>
      </div>
      {getContent(element)}

Jiwon Yoon's avatar
Jiwon Yoon committed
160
161
162
163
164
165
166
167
168
169
170
      <div className="place-self-end py-2">
        <button type="button" className="px-1">
          필수
        </button>
        <button type="button" className="px-1">
          옵션
        </button>
        <button type="button" className="px-1" onClick={handleDelete}>
          삭제
        </button>
        <button type="button" className="px-1" onClick={handleEdit}>
171
172
          수정
        </button>
Jiwon Yoon's avatar
Jiwon Yoon committed
173
174
175
        <button type="button" className="px-1" onClick={handleComplete}>
          완료
        </button>
176
177
178
179
      </div>
    </div>
  );
};