CreateSurveyFormPage.tsx 4.65 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
41
42
43
44
45
  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: {
    rateMin: number;
    rateMax: number;
    minRateDescription: string;
    maxRateDescription: string;
  };
46
47
}

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

Jiwon Yoon's avatar
Jiwon Yoon committed
115
116
117
118
119
120
121
export const CreateSurveyForm = () => {
  const [questionList, setQuestionList] = useState<Array<BasicQuestionType>>([
    EssayQ,
    RadioQ,
    CheckboxQ,
  ]);
  const [survey, setSurvey] = useState();
122

Jiwon Yoon's avatar
Jiwon Yoon committed
123
124
125
126
127
128
129
  function QuestionListChange(e: React.ChangeEvent<HTMLInputElement>): void {
    const newList: BasicQuestionType[] = [...questionList];
    const targetId: any = e.target.name;
    const obj: any = newList.find((a) => a.id === e.target.id);
    obj[targetId] = e.target.value;
    setQuestionList(newList);
  }
Jiwon Yoon's avatar
Jiwon Yoon committed
130

Jiwon Yoon's avatar
Jiwon Yoon committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  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]);
  }
151

Jiwon Yoon's avatar
Jiwon Yoon committed
152
153
154
155
  function deleteQuestion(): void {}

  return (
    <div className="flex flex-col place-items-center">
Jiwon Yoon's avatar
Jiwon Yoon committed
156
      <div className="flex flex-col container place-items-center mt-4">
Jiwon Yoon's avatar
Jiwon Yoon committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
        <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}
      />
Jiwon Yoon's avatar
Jiwon Yoon committed
174
175
176
177
178
      <div>
        <button className="border bg-themeColor my-5 py-2 px-3 font-bold text-white">
          설문조사 생성
        </button>
      </div>
179
    </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
180
181
  );
};