Question.tsx 6.7 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
import React, { useState } from "react";
import { BasicQuestionType } 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";
Yoon, Daeki's avatar
Yoon, Daeki committed
11
import { QUESTION_TYPES } from "../commons";
12
13
14

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

Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
22
23
export const Question = ({
  element,
  handleQuestion,
  deleteQuestion,
Yoon, Daeki's avatar
Yoon, Daeki committed
24
25
26
27
28
}: // isSave,
Props) => {
  const [question, setQuestion] = useState({ ...element });
  const [isSaved, setIsSaved] = useState(false);

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

Yoon, Daeki's avatar
Yoon, Daeki committed
82
83
84
85
86
  const handleElement = () => {
    console.log("handle element");
    setQuestion({ ...question });
  };

Jiwon Yoon's avatar
Jiwon Yoon committed
87
88
  function handleQuestionInfo(event: React.ChangeEvent<HTMLInputElement>) {
    const { name, value } = event.currentTarget;
Yoon, Daeki's avatar
Yoon, Daeki committed
89
90
91
    // question[name] = value;
    setQuestion({ ...question, [name]: value });
    // handleQuestion(question._id);
Jiwon Yoon's avatar
Jiwon Yoon committed
92
  }
93
94
95
96

  function getContent(element: BasicQuestionType) {
    switch (element.type) {
      case "essay":
Yoon, Daeki's avatar
Yoon, Daeki committed
97
        return <EssayForm element={element} save={isSaved} />;
98
      case "radio":
Jiwon Yoon's avatar
Jiwon Yoon committed
99
100
        return (
          <RadioForm
Yoon, Daeki's avatar
Yoon, Daeki committed
101
            handleQuestion={handleElement}
Jiwon Yoon's avatar
Jiwon Yoon committed
102
            element={element}
Yoon, Daeki's avatar
Yoon, Daeki committed
103
            save={isSaved}
Jiwon Yoon's avatar
Jiwon Yoon committed
104
105
          />
        );
106
      case "checkbox":
Jiwon Yoon's avatar
Jiwon Yoon committed
107
        return (
Jiwon Yoon's avatar
Jiwon Yoon committed
108
          <CheckboxForm
Yoon, Daeki's avatar
Yoon, Daeki committed
109
            handleQuestion={handleElement}
Jiwon Yoon's avatar
Jiwon Yoon committed
110
            element={element}
Yoon, Daeki's avatar
Yoon, Daeki committed
111
            save={isSaved}
Jiwon Yoon's avatar
Jiwon Yoon committed
112
          />
Jiwon Yoon's avatar
Jiwon Yoon committed
113
        );
114
      case "dropdown":
Jiwon Yoon's avatar
Jiwon Yoon committed
115
        return (
Jiwon Yoon's avatar
Jiwon Yoon committed
116
          <DropdownForm
Yoon, Daeki's avatar
Yoon, Daeki committed
117
            handleQuestion={handleElement}
Jiwon Yoon's avatar
Jiwon Yoon committed
118
            element={element}
Yoon, Daeki's avatar
Yoon, Daeki committed
119
            save={isSaved}
Jiwon Yoon's avatar
Jiwon Yoon committed
120
          />
Jiwon Yoon's avatar
Jiwon Yoon committed
121
        );
122
      case "file":
Yoon, Daeki's avatar
Yoon, Daeki committed
123
        return <FileForm element={element} save={isSaved} />;
124
      case "rating":
Jiwon Yoon's avatar
Jiwon Yoon committed
125
126
        return (
          <RatingForm
Yoon, Daeki's avatar
Yoon, Daeki committed
127
            handleQuestion={handleElement}
Jiwon Yoon's avatar
Jiwon Yoon committed
128
            element={element}
Yoon, Daeki's avatar
Yoon, Daeki committed
129
            save={isSaved}
Jiwon Yoon's avatar
Jiwon Yoon committed
130
131
          />
        );
Jiwon Yoon's avatar
Jiwon Yoon committed
132
133
      case "date":
        return <DateForm />;
134
135
136
137
      default:
        return <></>;
    }
  }
Yoon, Daeki's avatar
Yoon, Daeki committed
138

139
140
  const handleRequired = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { checked, value } = event.currentTarget;
Yoon, Daeki's avatar
Yoon, Daeki committed
141
142
143
144
145
146
147
148
149
150
    question[value] = checked;
    setQuestion({ ...question, [value]: checked });
    // handleQuestion(question._id);
  };

  const onCancel = () => {
    console.log("element canceled button clicked", element);
    console.log("question canceled button clicked", question);
    setQuestion(element);
    setIsSaved(true);
151
  };
Yoon, Daeki's avatar
Yoon, Daeki committed
152

153
  const handleDelete = () => {
Yoon, Daeki's avatar
Yoon, Daeki committed
154
    deleteQuestion(question._id);
155
  };
Yoon, Daeki's avatar
Yoon, Daeki committed
156

157
  const handleEditClick = () => {
Yoon, Daeki's avatar
Yoon, Daeki committed
158
    setIsSaved(false);
159
  };
Yoon, Daeki's avatar
Yoon, Daeki committed
160

161
  return (
Jiwon Yoon's avatar
Jiwon Yoon committed
162
    <div
Yoon, Daeki's avatar
Yoon, Daeki committed
163
      style={{ borderColor: isSaved ? "#58ACFA" : "red" }}
Jiwon Yoon's avatar
Jiwon Yoon committed
164
165
      className="flex flex-col container w-4/5 h-auto border-2 items-center m-3 py-2"
    >
166
167
168
169
      <div className="flex h-16 w-full place-content-between items-center">
        <input
          type="text"
          name="title"
Yoon, Daeki's avatar
Yoon, Daeki committed
170
          id={question._id}
171
          className="text-xl font-bold ml-6 border-b-2 w-1/2"
Jiwon Yoon's avatar
Jiwon Yoon committed
172
          placeholder={"Question Title"}
Yoon, Daeki's avatar
Yoon, Daeki committed
173
          value={question.title}
Jiwon Yoon's avatar
Jiwon Yoon committed
174
          onChange={handleQuestionInfo}
Yoon, Daeki's avatar
Yoon, Daeki committed
175
          disabled={isSaved}
176
177
        ></input>
        <select
Yoon, Daeki's avatar
Yoon, Daeki committed
178
          id={question._id}
179
          name="type"
Jiwon Yoon's avatar
Jiwon Yoon committed
180
          onChange={handleSelect}
Yoon, Daeki's avatar
Yoon, Daeki committed
181
182
          disabled={isSaved}
          value={question.type}
jang dong hyeok's avatar
.    
jang dong hyeok committed
183
          className="w-32 md: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"
184
        >
Yoon, Daeki's avatar
Yoon, Daeki committed
185
          {Array.from(QUESTION_TYPES.entries()).map(([key, value]) => (
Jiwon Yoon's avatar
Jiwon Yoon committed
186
            <option
187
              key={key}
Yoon, Daeki's avatar
Yoon, Daeki committed
188
              id={question._id}
Jiwon Yoon's avatar
Jiwon Yoon committed
189
              value={key}
Yoon, Daeki's avatar
Yoon, Daeki committed
190
              // selected={key === element.type}
Jiwon Yoon's avatar
Jiwon Yoon committed
191
192
193
            >
              {value}
            </option>
194
195
196
197
198
199
200
          ))}
        </select>
      </div>
      <div className="flex w-full justify-center">
        <input
          type="text"
          name="comment"
Yoon, Daeki's avatar
Yoon, Daeki committed
201
          id={question._id}
202
203
          className="border w-11/12"
          placeholder="질문에 대한 설명을 입력해주세요"
Yoon, Daeki's avatar
Yoon, Daeki committed
204
          value={question.comment}
Jiwon Yoon's avatar
Jiwon Yoon committed
205
          onChange={handleQuestionInfo}
Yoon, Daeki's avatar
Yoon, Daeki committed
206
          disabled={isSaved}
207
208
        ></input>
      </div>
Yoon, Daeki's avatar
Yoon, Daeki committed
209
      {getContent(question)}
210

Jiwon Yoon's avatar
Jiwon Yoon committed
211
      <div className="place-self-end py-2">
212
213
214
215
216
        <input
          type="checkbox"
          id="isRequired"
          value="isRequired"
          onChange={handleRequired}
Yoon, Daeki's avatar
Yoon, Daeki committed
217
218
          disabled={isSaved}
          checked={question.isRequired}
219
220
        />
        <label htmlFor="isRequired" className="px-1">
Jiwon Yoon's avatar
Jiwon Yoon committed
221
          필수
222
        </label>
Yoon, Daeki's avatar
Yoon, Daeki committed
223
224
225
226
227
228
229
230
231
        {isSaved ? (
          <>
            <button type="button" className="px-1" onClick={handleDelete}>
              삭제
            </button>
            <button type="button" className="px-1" onClick={handleEditClick}>
              수정
            </button>
          </>
Jiwon Yoon's avatar
Jiwon Yoon committed
232
        ) : (
Yoon, Daeki's avatar
Yoon, Daeki committed
233
234
235
236
237
238
239
240
241
          <>
            <button type="button" className="px-1" onClick={onCancel}>
              취소
            </button>

            <button type="button" className="px-1" onClick={handleEditComplete}>
              확인
            </button>
          </>
Jiwon Yoon's avatar
Jiwon Yoon committed
242
        )}
243
244
245
246
      </div>
    </div>
  );
};