Question.tsx 6.01 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import React, { useState, Dispatch, SetStateAction } from "react";
2
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";
Jiwon Yoon's avatar
Jiwon Yoon committed
10
import { DateForm } from "./DateForm";
11
12
13

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

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

Jiwon Yoon's avatar
Jiwon Yoon committed
30
31
32
33
export const Question = ({
  element,
  handleQuestion,
  deleteQuestion,
Jiwon Yoon's avatar
Jiwon Yoon committed
34
  isSave,
Jiwon Yoon's avatar
Jiwon Yoon committed
35
}: Props) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
36
  const [save, setSave] = useState(isSave);
Jiwon Yoon's avatar
Jiwon Yoon committed
37
  async function handleEditComplete() {
Jiwon Yoon's avatar
Jiwon Yoon committed
38
39
40
41
42
    try {
      const newQuestion: BasicQuestionType = await questionApi.updateQuestion(
        element
      );
      console.log(newQuestion);
Jiwon Yoon's avatar
Jiwon Yoon committed
43
      setSave(true);
Jiwon Yoon's avatar
Jiwon Yoon committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
      // 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: [
Jiwon Yoon's avatar
Jiwon Yoon committed
64
65
66
          { text: "", value: 0 },
          { text: "", value: 1 },
          { text: "", value: 2 },
Jiwon Yoon's avatar
Jiwon Yoon committed
67
68
69
70
71
72
        ],
      };
    } else if (selectedType === "essay") {
      element.content = { choices: [] };
    } else if (selectedType === "rating") {
      element.content = {
Jiwon Yoon's avatar
Jiwon Yoon committed
73
74
        minRateDescription: "",
        maxRateDescription: "",
Jiwon Yoon's avatar
Jiwon Yoon committed
75
        choices: [
Jiwon Yoon's avatar
Jiwon Yoon committed
76
77
78
          { text: "", value: 0 },
          { text: "", value: 1 },
          { text: "", value: 2 },
Jiwon Yoon's avatar
Jiwon Yoon committed
79
80
81
82
83
84
85
86
87
88
89
90
        ],
      };
    }
    element.type = selectedType;
    handleQuestion(element._id);
  }

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

  function getContent(element: BasicQuestionType) {
    switch (element.type) {
      case "essay":
Jiwon Yoon's avatar
Jiwon Yoon committed
95
        return <EssayForm element={element} save={save} />;
96
      case "radio":
Jiwon Yoon's avatar
Jiwon Yoon committed
97
98
99
100
        return (
          <RadioForm
            handleQuestion={handleQuestion}
            element={element}
Jiwon Yoon's avatar
Jiwon Yoon committed
101
            save={save}
Jiwon Yoon's avatar
Jiwon Yoon committed
102
103
          />
        );
104
      case "checkbox":
Jiwon Yoon's avatar
Jiwon Yoon committed
105
        return (
Jiwon Yoon's avatar
Jiwon Yoon committed
106
107
108
          <CheckboxForm
            handleQuestion={handleQuestion}
            element={element}
Jiwon Yoon's avatar
Jiwon Yoon committed
109
            save={save}
Jiwon Yoon's avatar
Jiwon Yoon committed
110
          />
Jiwon Yoon's avatar
Jiwon Yoon committed
111
        );
112
      case "dropdown":
Jiwon Yoon's avatar
Jiwon Yoon committed
113
        return (
Jiwon Yoon's avatar
Jiwon Yoon committed
114
115
116
          <DropdownForm
            handleQuestion={handleQuestion}
            element={element}
Jiwon Yoon's avatar
Jiwon Yoon committed
117
            save={save}
Jiwon Yoon's avatar
Jiwon Yoon committed
118
          />
Jiwon Yoon's avatar
Jiwon Yoon committed
119
        );
120
      case "file":
Jiwon Yoon's avatar
Jiwon Yoon committed
121
        return <FileForm element={element} save={save} />;
122
      case "rating":
Jiwon Yoon's avatar
Jiwon Yoon committed
123
124
125
126
        return (
          <RatingForm
            handleQuestion={handleQuestion}
            element={element}
Jiwon Yoon's avatar
Jiwon Yoon committed
127
            save={save}
Jiwon Yoon's avatar
Jiwon Yoon committed
128
129
          />
        );
Jiwon Yoon's avatar
Jiwon Yoon committed
130
131
      case "date":
        return <DateForm />;
132
133
134
135
      default:
        return <></>;
    }
  }
136
137
138
139
140
141
142
143
144
  const handleRequired = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { checked, value } = event.currentTarget;
    element[value] = checked;
    handleQuestion(element._id);
  };
  const handleDelete = () => {
    deleteQuestion(element._id);
  };
  const handleEditClick = () => {
Jiwon Yoon's avatar
Jiwon Yoon committed
145
    setSave(false);
146
  };
147
  return (
Jiwon Yoon's avatar
Jiwon Yoon committed
148
    <div
Jiwon Yoon's avatar
Jiwon Yoon committed
149
      style={{ borderColor: save ? "#58ACFA" : "red" }}
Jiwon Yoon's avatar
Jiwon Yoon committed
150
151
      className="flex flex-col container w-4/5 h-auto border-2 items-center m-3 py-2"
    >
152
153
154
155
156
157
      <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
158
159
160
          placeholder={"Question Title"}
          value={element.title}
          onChange={handleQuestionInfo}
Jiwon Yoon's avatar
Jiwon Yoon committed
161
          disabled={save}
162
163
        ></input>
        <select
Jiwon Yoon's avatar
Jiwon Yoon committed
164
          id={element._id}
165
          name="type"
Jiwon Yoon's avatar
Jiwon Yoon committed
166
          onChange={handleSelect}
167
          disabled={save}
168
169
          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
170
171
172
173
174
175
176
177
          {Array.from(typeDropDown.entries()).map(([key, value]) => (
            <option
              id={element._id}
              value={key}
              selected={key === element.type}
            >
              {value}
            </option>
178
179
180
181
182
183
184
185
186
187
          ))}
        </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
188
189
          value={element.comment}
          onChange={handleQuestionInfo}
Jiwon Yoon's avatar
Jiwon Yoon committed
190
          disabled={save}
191
192
193
194
        ></input>
      </div>
      {getContent(element)}

Jiwon Yoon's avatar
Jiwon Yoon committed
195
      <div className="place-self-end py-2">
196
197
198
199
200
        <input
          type="checkbox"
          id="isRequired"
          value="isRequired"
          onChange={handleRequired}
Jiwon Yoon's avatar
Jiwon Yoon committed
201
          disabled={save}
202
203
204
          checked={element.isRequired}
        />
        <label htmlFor="isRequired" className="px-1">
Jiwon Yoon's avatar
Jiwon Yoon committed
205
          필수
206
        </label>
Jiwon Yoon's avatar
Jiwon Yoon committed
207
208
209
        <button type="button" className="px-1" onClick={handleDelete}>
          삭제
        </button>
Jiwon Yoon's avatar
Jiwon Yoon committed
210
        {save ? (
Jiwon Yoon's avatar
Jiwon Yoon committed
211
          <button type="button" className="px-1" onClick={handleEditClick}>
Jiwon Yoon's avatar
Jiwon Yoon committed
212
213
            수정하기
          </button>
Jiwon Yoon's avatar
Jiwon Yoon committed
214
215
216
217
        ) : (
          <button type="button" className="px-1" onClick={handleEditComplete}>
            수정완료
          </button>
Jiwon Yoon's avatar
Jiwon Yoon committed
218
        )}
219
220
221
222
      </div>
    </div>
  );
};