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

Jiwon Yoon's avatar
Jiwon Yoon committed
4
export interface BasicQuestionType {
5
  type: string;
Jiwon Yoon's avatar
Jiwon Yoon committed
6
  id: string;
7
8
  title: string;
  isRequired: boolean;
Jiwon Yoon's avatar
Jiwon Yoon committed
9
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
  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
41
42
43
44
    rateValues: {
      value: number;
      text: string;
    }[];
Jiwon Yoon's avatar
Jiwon Yoon committed
45
46
47
    minRateDescription: string;
    maxRateDescription: string;
  };
48
49
}

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

Jiwon Yoon's avatar
Jiwon Yoon committed
120
export const CreateSurveyForm = () => {
Jiwon Yoon's avatar
Jiwon Yoon committed
121
  const [currentId, setCurrentId] = useState<string>("");
Jiwon Yoon's avatar
Jiwon Yoon committed
122
123
124
125
126
127
  const [questionList, setQuestionList] = useState<Array<BasicQuestionType>>([
    EssayQ,
    RadioQ,
    CheckboxQ,
  ]);
  const [survey, setSurvey] = useState();
128

Jiwon Yoon's avatar
Jiwon Yoon committed
129
130
131
132
  function changeCurrentId(event: React.MouseEvent<HTMLButtonElement>): void {
    setCurrentId(event.currentTarget.id);
  }

Jiwon Yoon's avatar
Jiwon Yoon committed
133
134
  function QuestionListChange(e: React.ChangeEvent<HTMLInputElement>): void {
    const newList: BasicQuestionType[] = [...questionList];
Jiwon Yoon's avatar
Jiwon Yoon committed
135
136
137
    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
138
139
    setQuestionList(newList);
  }
Jiwon Yoon's avatar
Jiwon Yoon committed
140

Jiwon Yoon's avatar
Jiwon Yoon committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  function addQuestion(): void {
    //무작위로 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]);
  }
161

Jiwon Yoon's avatar
Jiwon Yoon committed
162
163
164
  function deleteQuestion(): void {}

  return (
Jiwon Yoon's avatar
Jiwon Yoon committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    <>
      {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
192
      </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
193
    </>
Jiwon Yoon's avatar
Jiwon Yoon committed
194
195
  );
};