ResultSurvey.tsx 1.63 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import React, { useEffect, useState } from "react";
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { answerApi, surveyApi } from "../apis";
Jiwon Yoon's avatar
Jiwon Yoon committed
3
import { catchErrors } from "../helpers";
4
import { Accordion } from "./Accordion";
Jiwon Yoon's avatar
Jiwon Yoon committed
5
import { useParams } from "react-router-dom";
Yoon, Daeki's avatar
Yoon, Daeki committed
6
import { ISurvey } from "../types";
jang dong hyeok's avatar
.    
jang dong hyeok committed
7
8

export const ResultSurvey = () => {
Jiwon Yoon's avatar
Jiwon Yoon committed
9
  let { surveyId } = useParams<{ surveyId: string }>();
Jiwon Yoon's avatar
Jiwon Yoon committed
10
11
12
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);
  const [success, setSuccess] = useState(false);
Yoon, Daeki's avatar
Yoon, Daeki committed
13
  const [survey, setSurvey] = useState<ISurvey>({
Jiwon Yoon's avatar
Jiwon Yoon committed
14
15
16
17
18
19
    _id: surveyId || "",
    user: {},
    title: "",
    comment: "",
    questions: [],
  });
20

Jiwon Yoon's avatar
Jiwon Yoon committed
21
22
23
24
25
26
27
  useEffect(() => {
    getAnswers();
  }, [surveyId]);

  async function getAnswers() {
    try {
      if (surveyId) {
Jiwon Yoon's avatar
Jiwon Yoon committed
28
        const survey = await answerApi.getAnswers(surveyId);
Yoon, Daeki's avatar
Yoon, Daeki committed
29
        // console.log(survey);
Jiwon Yoon's avatar
Jiwon Yoon committed
30
        setSurvey(survey);
Jiwon Yoon's avatar
Jiwon Yoon committed
31
32
33
34
35
36
37
38
39
      } else {
        setLoading(true);
      }
    } catch (error) {
      catchErrors(error, setError);
    } finally {
      setLoading(false);
    }
  }
Yoon, Daeki's avatar
Yoon, Daeki committed
40

jang dong hyeok's avatar
.    
jang dong hyeok committed
41
42
43
44
  return (
    <div className="flex flex-col place-items-center">
      <div className="flex flex-col container place-items-center mt-4">
        <div className="font-bold text-4xl text-center m-2 border-b-2">
Jiwon Yoon's avatar
Jiwon Yoon committed
45
          {survey.title}
jang dong hyeok's avatar
.    
jang dong hyeok committed
46
        </div>
Lee SeoYeon's avatar
0727    
Lee SeoYeon committed
47
        <div className="font-bold text-xl text-center m-2 resize-none">
Jiwon Yoon's avatar
Jiwon Yoon committed
48
          {survey.comment}
jang dong hyeok's avatar
.    
jang dong hyeok committed
49
50
        </div>
      </div>
jang dong hyeok's avatar
jang dong hyeok committed
51

jang dong hyeok's avatar
jang dong hyeok committed
52
      <div className="container w-11/12 place-self-center">
Jiwon Yoon's avatar
Jiwon Yoon committed
53
54
        {survey.questions.map((question) => (
          <Accordion key={question._id} question={question} />
jang dong hyeok's avatar
jang dong hyeok committed
55
        ))}
jang dong hyeok's avatar
.    
jang dong hyeok committed
56
57
58
59
      </div>
    </div>
  );
};
jang dong hyeok's avatar
jang dong hyeok committed
60
61

export default ResultSurvey;