SurveyForm.tsx 2.76 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import React, { FormEvent, useEffect, useState } from "react";
Lee SeoYeon's avatar
Lee SeoYeon committed
2
3
4
import { useParams } from "react-router-dom";
import { surveyApi } from "../apis";
import { catchErrors } from "../helpers";
Jiwon Yoon's avatar
Jiwon Yoon committed
5
import { AnswerType, SurveyType } from "../types";
Lee SeoYeon's avatar
Lee SeoYeon committed
6
import { AQuestion } from "./AQuestion";
Lee SeoYeon's avatar
Lee SeoYeon committed
7
import { ARadioForm } from "./ARadioForm";
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
8

Lee SeoYeon's avatar
0708    
Lee SeoYeon committed
9
export const SurveyForm = () => {
Lee SeoYeon's avatar
Lee SeoYeon committed
10
11
12
13
14
15
16
17
18
19
20
  let { surveyId } = useParams<{ surveyId: string }>();
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);
  const [success, setSuccess] = useState(false);
  const [survey, setSurvey] = useState<SurveyType>({
    _id: surveyId,
    user: {},
    title: "",
    comment: "",
    questions: [],
  });
Jiwon Yoon's avatar
Jiwon Yoon committed
21
22
23
24
25
  const [response, setResponse] = useState<AnswerType>({
    surveyId: surveyId,
    guestId: "",
    answers: [{ questionId: "", answer: "" }],
  });
Lee SeoYeon's avatar
Lee SeoYeon committed
26
27
28
29
30
31
32
33
34
35

  useEffect(() => {
    ansSurvey();
  }, [surveyId]);

  async function ansSurvey() {
    try {
      if (surveyId) {
        const answersurvey: SurveyType = await surveyApi.ansSurvey(surveyId);
        console.log(answersurvey);
Jiwon Yoon's avatar
Jiwon Yoon committed
36
37
38
39
40
41
42
43
44
        const questionIds = answersurvey.questions.map((el) => {
          return { questionId: el._id, answer: "" };
        });
        console.log(questionIds);
        setResponse({
          ...response,
          surveyId: answersurvey._id,
          answers: questionIds,
        });
Lee SeoYeon's avatar
Lee SeoYeon committed
45
46
47
48
49
50
51
52
53
54
55
56
57
        setSurvey(answersurvey);
        setSuccess(true);
        setError("");
      } else {
        setLoading(true);
      }
    } catch (error) {
      catchErrors(error, setError);
    } finally {
      setLoading(false);
    }
  }

Jiwon Yoon's avatar
Jiwon Yoon committed
58
59
60
61
62
63
64
65
66
  function handleSubmit(event: FormEvent) {
    event.preventDefault();
  }

  const handleAnswer = () => {
    const newList = [...response.answers];
    setResponse({ ...response, answers: newList });
  };

Lee SeoYeon's avatar
0708    
Lee SeoYeon committed
67
  return (
Jiwon Yoon's avatar
Jiwon Yoon committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    <>
      {console.log(response)}
      <form onSubmit={handleSubmit}>
        <div className="flex flex-col place-items-center">
          <div className="flex flex-col container place-items-center mt-4">
            <p className="font-bold text-4xl text-center m-2">{survey.title}</p>
            <p className="font-bold text-1xl text-center m-2">
              {survey.comment}
            </p>
            {survey.questions.map((question) => {
              return (
                <AQuestion
                  question={question}
                  response={response}
                  handleAnswer={handleAnswer}
                ></AQuestion>
              );
            })}
            <div>
              <button
                type="submit"
                className="rounded bg-themeColor my-5 py-2 px-5 font-bold text-white"
              >
                제출하기
              </button>
            </div>
          </div>
Lee SeoYeon's avatar
Lee SeoYeon committed
95
        </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
96
97
      </form>
    </>
Lee SeoYeon's avatar
0708    
Lee SeoYeon committed
98
99
  );
};