EditSurvey.tsx 4.61 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import React, { FormEvent, useEffect, useState } from "react";
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { useParams, useLocation } from "react-router-dom";
3
import { questionApi, surveyApi } from "../apis";
Jiwon Yoon's avatar
Jiwon Yoon committed
4
import { SpinnerIcon } from "../icons";
5
6
import { Question } from "../questions";
import { BasicQuestionType, SurveyType } from "../types";
7
import { catchErrors } from "../helpers";
8

Jiwon Yoon's avatar
Jiwon Yoon committed
9
10
export const EditSurvey = () => {
  let { surveyId } = useParams<{ surveyId: string }>();
Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
13
14
15
16
  interface CustomizedState {
    save: boolean;
  }
  const location = useLocation();
  const state = location.state as CustomizedState;

Jiwon Yoon's avatar
Jiwon Yoon committed
17
18
19
20
21
22
  useEffect(() => {
    getSurvey();
  }, [surveyId]);
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);
  const [success, setSuccess] = useState(false);
23
  const [survey, setSurvey] = useState<SurveyType>({
Jiwon Yoon's avatar
Jiwon Yoon committed
24
25
    _id: surveyId,
    user: {},
26
27
28
29
    title: "",
    comment: "",
    questions: [],
  });
Jiwon Yoon's avatar
Jiwon Yoon committed
30
31
32
33
34
35
36
37
38
39
40
  async function getSurvey() {
    try {
      if (surveyId) {
        const thisSurvey: SurveyType = await surveyApi.getSurvey(surveyId);
        setSurvey(thisSurvey);
        setSuccess(true);
        setError("");
      } else {
        setLoading(true);
      }
    } catch (error) {
41
      catchErrors(error, setError);
Jiwon Yoon's avatar
Jiwon Yoon committed
42
43
44
45
    } finally {
      setLoading(false);
    }
  }
46

Jiwon Yoon's avatar
Jiwon Yoon committed
47
48
49
50
51
52
53
54
55
  const handleQuestion = (id: string) => {
    const newList: BasicQuestionType[] = [...survey.questions];
    setSurvey({ ...survey, questions: newList });
  };

  const handleSurvey = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { name, value } = event.currentTarget;
    setSurvey({ ...survey, [name]: value });
  };
56
57
58
59

  async function handleSubmit(event: FormEvent) {
    event.preventDefault();
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
60
      const newSurvey: SurveyType = await surveyApi.editSurvey(survey);
61
      console.log(newSurvey);
62
63
      setSuccess(true);
      setError("");
64
    } catch (error) {
65
      catchErrors(error, setError);
66
    } finally {
67
      setLoading(false);
68
69
70
71
72
    }
  }

  async function addQuestion() {
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
73
74
75
76
77
78
79
80
81
82
83
      if (surveyId) {
        const questions: BasicQuestionType[] = await questionApi.createQuestion(
          surveyId
        );
        console.log(questions);
        setSurvey({ ...survey, questions: questions });
        setSuccess(true);
        setError("");
      } else {
        setLoading(true);
      }
84
    } catch (error) {
85
      catchErrors(error, setError);
86
    } finally {
87
      setLoading(false);
88
89
90
    }
  }

Jiwon Yoon's avatar
Jiwon Yoon committed
91
92
93
94
95
96
97
  async function deleteQuestion(id: string) {
    const newList: BasicQuestionType[] = [...survey.questions];
    try {
      const newQuestion: BasicQuestionType = await questionApi.deleteQuestion(
        id
      );
      setSurvey({ ...survey, questions: newList.filter((a) => a._id !== id) });
98
99
      setSuccess(true);
      setError("");
Jiwon Yoon's avatar
Jiwon Yoon committed
100
    } catch (error) {
101
      catchErrors(error, setError);
Jiwon Yoon's avatar
Jiwon Yoon committed
102
    } finally {
103
      setLoading(false);
Jiwon Yoon's avatar
Jiwon Yoon committed
104
105
    }
  }
106

Jiwon Yoon's avatar
Jiwon Yoon committed
107
108
  const questions = survey.questions;
  console.log(questions);
Jiwon Yoon's avatar
Jiwon Yoon committed
109
  console.log(state);
110
111
  return (
    <>
112
      {error ? alert(error) : <></>}
Jiwon Yoon's avatar
Jiwon Yoon committed
113
114
115
      {loading && (
        <SpinnerIcon className="animate-spin h-5 w-5 mr-1 text-slate" />
      )}
116
117
118
119
120
121
122
123
      <form onSubmit={handleSubmit}>
        <div className="flex flex-col place-items-center">
          <div className="flex flex-col container place-items-center mt-4">
            <input
              type="text"
              name="title"
              className="font-bold text-4xl text-center m-2 border-b-2"
              placeholder="설문지 제목"
124
              value={survey.title}
Jiwon Yoon's avatar
Jiwon Yoon committed
125
              onChange={handleSurvey}
126
127
128
129
130
131
132
            ></input>
            <input
              type="text"
              name="comment"
              className="font-bold text-1xl text-center m-2 resize-none"
              placeholder="설문조사에 대한 설명을 입력해주세요"
              size={50}
133
              value={survey.comment}
Jiwon Yoon's avatar
Jiwon Yoon committed
134
              onChange={handleSurvey}
135
136
137
            ></input>
          </div>
          {questions.map((question) => (
Jiwon Yoon's avatar
Jiwon Yoon committed
138
139
            <Question
              element={question}
Jiwon Yoon's avatar
Jiwon Yoon committed
140
              isSave={state ? true : false}
Jiwon Yoon's avatar
Jiwon Yoon committed
141
142
143
              handleQuestion={handleQuestion}
              deleteQuestion={deleteQuestion}
            />
144
145
146
147
148
149
150
151
152
153
154
          ))}
          <div className="flex w-4/5 content-center justify-center border-2 border-black h-8 mt-3">
            <button type="button" onClick={addQuestion}>
              질문 추가
            </button>
          </div>
          <div>
            <button
              type="submit"
              className="border bg-themeColor my-5 py-2 px-3 font-bold text-white"
            >
155
              저장하기
156
157
158
159
160
161
162
            </button>
          </div>
        </div>
      </form>
    </>
  );
};