CreateSurveyFormPage.tsx 5.67 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import React, { useState } from "react";
2
import { Question } from "./Question";
Jiwon Yoon's avatar
Jiwon Yoon committed
3
import axios from "axios";
4

Jiwon Yoon's avatar
Jiwon Yoon committed
5
export interface BasicQuestionType {
6
  type: string;
Jiwon Yoon's avatar
Jiwon Yoon committed
7
  id: string;
8
9
  title: string;
  isRequired: boolean;
Jiwon Yoon's avatar
Jiwon Yoon committed
10
11
12
13
14
15
16
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
  comment: string;
  content: any;
  [key: string]: string | number | boolean | any;
}
export interface EssayType extends BasicQuestionType {}
export interface RadioType extends BasicQuestionType {
  content: {
    hasOther: boolean;
    choices: string[];
    otherText: string;
  };
}
export interface CheckboxType extends BasicQuestionType {
  content: {
    choices: string[];
    maxCount: number;
  };
}
export interface DropdownType extends BasicQuestionType {
  content: {
    choices: string[];
    hasNone: boolean;
  };
}
export interface FileType extends BasicQuestionType {
  content: {
    filename: string;
    value: string;
  };
}
export interface RatingType extends BasicQuestionType {
  content: {
Jiwon Yoon's avatar
QRating    
Jiwon Yoon committed
42
43
44
45
    rateValues: {
      value: number;
      text: string;
    }[];
Jiwon Yoon's avatar
Jiwon Yoon committed
46
47
48
    minRateDescription: string;
    maxRateDescription: string;
  };
49
50
}

Jiwon Yoon's avatar
Jiwon Yoon committed
51
52
53
const EssayQ: EssayType = {
  type: "essay",
  id: "000000000000",
Jiwon Yoon's avatar
Jiwon Yoon committed
54
  title: "Question Title",
55
  isRequired: false,
Jiwon Yoon's avatar
Jiwon Yoon committed
56
57
58
59
60
61
  comment: "질문에 대한 설명을 입력해주세요",
  content: null,
};
const RadioQ: RadioType = {
  type: "radio",
  id: "000000000001",
Jiwon Yoon's avatar
Jiwon Yoon committed
62
  title: "Question Title",
Jiwon Yoon's avatar
Jiwon Yoon committed
63
64
65
66
67
68
69
70
71
72
73
  isRequired: false,
  comment: "질문에 대한 설명을 입력해주세요",
  content: {
    hasOther: false,
    otherText: "",
    choices: ["radio1", "radio2", "radio3"],
  },
};
const CheckboxQ: CheckboxType = {
  type: "checkbox",
  id: "000000000002",
Jiwon Yoon's avatar
Jiwon Yoon committed
74
  title: "Question Title",
Jiwon Yoon's avatar
Jiwon Yoon committed
75
76
77
78
79
80
81
82
83
84
  isRequired: false,
  comment: "질문에 대한 설명을 입력해주세요",
  content: {
    choices: ["check1", "check2", "check3"],
    maxCount: 2,
  },
};
const DropdownQ: DropdownType = {
  type: "dropdown",
  id: "000000000003",
Jiwon Yoon's avatar
Jiwon Yoon committed
85
  title: "Question Title",
Jiwon Yoon's avatar
Jiwon Yoon committed
86
87
88
89
90
91
92
93
94
95
  isRequired: false,
  comment: "질문에 대한 설명을 입력해주세요",
  content: {
    choices: ["drop1", "drop2", "drop3"],
    hasNone: false,
  },
};
const FileQ: FileType = {
  type: "file",
  id: "000000000004",
Jiwon Yoon's avatar
Jiwon Yoon committed
96
  title: "Question Title",
Jiwon Yoon's avatar
Jiwon Yoon committed
97
98
99
100
101
102
103
104
105
106
  isRequired: false,
  comment: "질문에 대한 설명을 입력해주세요",
  content: {
    filename: "",
    value: "",
  },
};
const RatingQ: RatingType = {
  type: "rating",
  id: "000000000005",
Jiwon Yoon's avatar
Jiwon Yoon committed
107
  title: "Question Title",
Jiwon Yoon's avatar
Jiwon Yoon committed
108
109
110
  isRequired: false,
  comment: "질문에 대한 설명을 입력해주세요",
  content: {
Jiwon Yoon's avatar
QRating    
Jiwon Yoon committed
111
112
113
114
115
    rateValues: [
      { value: 1, text: "1" },
      { value: 2, text: "2" },
      { value: 3, text: "3" },
    ],
Jiwon Yoon's avatar
Jiwon Yoon committed
116
117
118
    minRateDescription: "가장 낮음",
    maxRateDescription: "가장 높음",
  },
119
120
};

Jiwon Yoon's avatar
Jiwon Yoon committed
121
export const CreateSurveyForm = () => {
Jiwon Yoon's avatar
Jiwon Yoon committed
122
  const [currentId, setCurrentId] = useState<string>("");
Jiwon Yoon's avatar
Jiwon Yoon committed
123
124
125
  const [error, setError] = useState("");
  const [disabled, setDisabled] = useState(false);
  const [success, setSuccess] = useState(false);
Jiwon Yoon's avatar
Jiwon Yoon committed
126
127
128
129
130
131
  const [questionList, setQuestionList] = useState<Array<BasicQuestionType>>([
    EssayQ,
    RadioQ,
    CheckboxQ,
  ]);
  const [survey, setSurvey] = useState();
132

Jiwon Yoon's avatar
Jiwon Yoon committed
133
134
135
136
  function changeCurrentId(event: React.MouseEvent<HTMLButtonElement>): void {
    setCurrentId(event.currentTarget.id);
  }

Jiwon Yoon's avatar
Jiwon Yoon committed
137
138
  function QuestionListChange(e: React.ChangeEvent<HTMLInputElement>): void {
    const newList: BasicQuestionType[] = [...questionList];
Jiwon Yoon's avatar
Jiwon Yoon committed
139
140
141
    const obj: any = newList.find((a) => a.id === e.target.id); //고유 _id로 질문찾기
    const targetKey: any = e.target.name;
    obj[targetKey] = e.target.value;
Jiwon Yoon's avatar
Jiwon Yoon committed
142
143
    setQuestionList(newList);
  }
Jiwon Yoon's avatar
Jiwon Yoon committed
144

Jiwon Yoon's avatar
Jiwon Yoon committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  async function addQuestion(event: React.MouseEvent<HTMLButtonElement>) {
    event.preventDefault();
    try {
      const res = await axios.post("/api/question/create");
      console.log("서버연결됬나요", res);
      console.log("회원가입");
      setSuccess(true);
      setError("");
    } catch (error) {
      console.log("에러발생");
      // catchErrors(error, setError)
    } finally {
      // setLoading(false);
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    //무작위로 12자리 ID제공, 추후에 질문을 DB에 생성하고 _id를 DB에서 가져오는 것으로 교체할 예정
    function getRandomInt(min: number, max: number): string {
      min = Math.ceil(min);
      max = Math.floor(max);
      const randomNum: number = Math.floor(Math.random() * (max - min)) + min;
      return randomNum.toString();
    }
    const randomId: string = getRandomInt(100000000000, 999999999999);
    //새로운 질문 생성
    const newQ: EssayType = {
      type: "essay",
      id: randomId,
      title: "Question",
      isRequired: false,
      comment: "질문에 대한 설명을 입력해주세요",
      content: null,
    };
    setQuestionList([...questionList, newQ]);
  }
178

Jiwon Yoon's avatar
Jiwon Yoon committed
179
180
181
  function deleteQuestion(): void {}

  return (
Jiwon Yoon's avatar
Jiwon Yoon committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    <>
      {console.log(currentId)}
      <div className="flex flex-col place-items-center">
        <div className="flex flex-col container place-items-center mt-4">
          <input
            type="text"
            className="font-bold text-4xl text-center m-2 border-b-2"
            placeholder="설문지 제목"
          ></input>
          <textarea
            className="font-bold text-1xl text-center m-2 resize-none"
            placeholder="설문조사에 대한 설명을 입력해주세요"
            rows={2}
            cols={60}
          ></textarea>
        </div>
        <Question
          questionList={questionList}
          QuestionListChange={QuestionListChange}
          addQuestion={addQuestion}
          changeCurrentId={changeCurrentId}
        />
        <div>
          <button className="border bg-themeColor my-5 py-2 px-3 font-bold text-white">
            설문조사 생성
          </button>
        </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
209
      </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
210
    </>
Jiwon Yoon's avatar
Jiwon Yoon committed
211
212
  );
};