question.context.tsx 3.34 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
3
4
5
6
7
import React, {
  createContext,
  FC,
  ReactNode,
  useContext,
  useState,
} from "react";
Jiwon Yoon's avatar
Jiwon Yoon committed
8
9
import { BasicQuestionType, SurveyType } from "../types";
import { questionApi, surveyApi } from "../apis";
Jiwon Yoon's avatar
Jiwon Yoon committed
10
11

interface IQuestionContext {
Jiwon Yoon's avatar
Jiwon Yoon committed
12
13
  handleSurvey: (e: React.ChangeEvent<HTMLInputElement>) => void;
  handleSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
Jiwon Yoon's avatar
Jiwon Yoon committed
14
15
16
  questionListChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
  questionList: BasicQuestionType[];
  editClick: (e: React.MouseEvent<HTMLButtonElement>) => void;
Jiwon Yoon's avatar
Jiwon Yoon committed
17
  editCompleteClick: (e: React.MouseEvent<HTMLButtonElement>) => void;
Jiwon Yoon's avatar
Jiwon Yoon committed
18
19
20
21
22
  currentId: string;
  addQuestion: (e: React.MouseEvent<HTMLButtonElement>) => Promise<void>;
}

const QuestionContext = createContext<IQuestionContext>({
Jiwon Yoon's avatar
Jiwon Yoon committed
23
24
  handleSurvey: () => {},
  handleSubmit: () => {},
Jiwon Yoon's avatar
Jiwon Yoon committed
25
26
27
  questionListChange: () => {},
  questionList: [],
  editClick: () => {},
Jiwon Yoon's avatar
Jiwon Yoon committed
28
  editCompleteClick: () => {},
Jiwon Yoon's avatar
Jiwon Yoon committed
29
30
31
32
33
  currentId: "",
  addQuestion: async () => {},
});

export const QuestionProvider: FC<{ children: ReactNode }> = ({ children }) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
34
35
36
  const [error, setError] = useState("");
  const [disabled, setDisabled] = useState(false);
  const [success, setSuccess] = useState(false);
Jiwon Yoon's avatar
Jiwon Yoon committed
37
38
39
40
  const [questionList, setQuestionList] = useState<Array<BasicQuestionType>>(
    []
  );
  const [currentId, setCurrentId] = useState<string>("");
Jiwon Yoon's avatar
Jiwon Yoon committed
41
42
43
44
45
46
  const [survey, setSurvey] = useState<SurveyType>({
    title: "",
    comment: "",
    //questions 는 _id들의 배열
    questions: [],
  });
Jiwon Yoon's avatar
Jiwon Yoon committed
47

Jiwon Yoon's avatar
Jiwon Yoon committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  function handleSurvey(event: React.ChangeEvent<HTMLInputElement>) {
    setSurvey({
      ...survey,
      [event.currentTarget.name]: event.currentTarget.value,
    });
  }

  async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault();
    try {
      const newSurvey: SurveyType = await surveyApi.createSurvey(survey);
      console.log(newSurvey);
      // setSuccess(true);
      // setError("");
    } catch (error) {
      console.log("에러발생");
      // catchErrors(error, setError)
    } finally {
      // setLoading(false);
    }
  }
Jiwon Yoon's avatar
Jiwon Yoon committed
69
70
71
72
73
74
75
76
  function questionListChange(e: React.ChangeEvent<HTMLInputElement>): void {
    const newList: BasicQuestionType[] = [...questionList];
    const obj: any = newList.find((a) => a._id === e.target.id); //고유 _id로 질문찾기
    const targetKey: any = e.target.name;
    obj[targetKey] = e.target.value;
    setQuestionList(newList);
  }

Jiwon Yoon's avatar
Jiwon Yoon committed
77
  async function addQuestion() {
Jiwon Yoon's avatar
Jiwon Yoon committed
78
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
79
      const newQ: BasicQuestionType = await questionApi.createQuestion();
Jiwon Yoon's avatar
Jiwon Yoon committed
80
      setSurvey({ ...survey, questions: [...survey.questions, newQ._id] });
Jiwon Yoon's avatar
Jiwon Yoon committed
81
      setQuestionList([...questionList, newQ]);
Jiwon Yoon's avatar
Jiwon Yoon committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
      // setSuccess(true);
      // setError("");
    } catch (error) {
      console.log("에러발생");
      // catchErrors(error, setError)
    } finally {
      // setLoading(false);
    }
  }

  function editClick(e: React.MouseEvent<HTMLButtonElement>) {
    setCurrentId(e.currentTarget.id);
  }

Jiwon Yoon's avatar
Jiwon Yoon committed
96
97
  function editCompleteClick(e: React.MouseEvent<HTMLButtonElement>) {}

Jiwon Yoon's avatar
Jiwon Yoon committed
98
99
100
  return (
    <QuestionContext.Provider
      value={{
Jiwon Yoon's avatar
Jiwon Yoon committed
101
102
        handleSurvey,
        handleSubmit,
Jiwon Yoon's avatar
Jiwon Yoon committed
103
104
105
106
        questionListChange,
        addQuestion,
        questionList,
        editClick,
Jiwon Yoon's avatar
Jiwon Yoon committed
107
        editCompleteClick,
Jiwon Yoon's avatar
Jiwon Yoon committed
108
109
110
111
112
113
114
115
116
        currentId,
      }}
    >
      {children}
    </QuestionContext.Provider>
  );
};

export const useQuestion = () => useContext(QuestionContext);