SurveyForm.tsx 1.71 KB
Newer Older
Lee SeoYeon's avatar
Lee SeoYeon committed
1
2
3
4
5
6
import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { surveyApi } from "../apis";
import { catchErrors } from "../helpers";
import { SurveyType } from "../types";
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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  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: [],
  });

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

  async function ansSurvey() {
    try {
      if (surveyId) {
        const answersurvey: SurveyType = await surveyApi.ansSurvey(surveyId);
        console.log(answersurvey);
        setSurvey(answersurvey);
        setSuccess(true);
        setError("");
      } else {
        setLoading(true);
      }
    } catch (error) {
      catchErrors(error, setError);
    } finally {
      setLoading(false);
    }
  }

Lee SeoYeon's avatar
0708    
Lee SeoYeon committed
44
45
46
  return (
    <div className="flex flex-col place-items-center">
      <div className="flex flex-col container place-items-center mt-4">
Lee SeoYeon's avatar
Lee SeoYeon committed
47
48
49
50
51
        <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}></AQuestion>;
        })}
Lee SeoYeon's avatar
Lee SeoYeon committed
52
53
54
55
        <div>
          <button className="rounded bg-themeColor my-5 py-2 px-5 font-bold text-white">
            제출하기
          </button>
Lee SeoYeon's avatar
Lee SeoYeon committed
56
        </div>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
57
58
      </div>
    </div>
Lee SeoYeon's avatar
0708    
Lee SeoYeon committed
59
60
  );
};