question.context.tsx 3.79 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
  currentId: string;
  addQuestion: (e: React.MouseEvent<HTMLButtonElement>) => Promise<void>;
20
  questionTypeChange: (e: React.ChangeEvent<HTMLSelectElement>) => void;
Jiwon Yoon's avatar
Jiwon Yoon committed
21
22
23
}

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

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

Jiwon Yoon's avatar
Jiwon Yoon committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  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
71
72
73
74
75
76
77
  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);
  }
78
79
80
81
82
83
84
  function questionTypeChange(e: React.ChangeEvent<HTMLSelectElement>): void {
    const newType: BasicQuestionType[] = [...questionList];
    const objType: any = newType.find((t) => t._id === e.target.id);
    const targetType: string = e.target.name;
    objType[targetType] = e.target.value;
    setQuestionList(newType);
  }
Jiwon Yoon's avatar
Jiwon Yoon committed
85

Jiwon Yoon's avatar
Jiwon Yoon committed
86
  async function addQuestion() {
Jiwon Yoon's avatar
Jiwon Yoon committed
87
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
88
      const newQ: BasicQuestionType = await questionApi.createQuestion();
89
      setSurvey({ ...survey, questions: [...survey.questions, newQ] });
Jiwon Yoon's avatar
Jiwon Yoon committed
90
      setQuestionList([...questionList, newQ]);
Jiwon Yoon's avatar
Jiwon Yoon committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
      // 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
105
106
  function editCompleteClick(e: React.MouseEvent<HTMLButtonElement>) {}

Jiwon Yoon's avatar
Jiwon Yoon committed
107
108
109
  return (
    <QuestionContext.Provider
      value={{
Jiwon Yoon's avatar
Jiwon Yoon committed
110
111
        handleSurvey,
        handleSubmit,
Jiwon Yoon's avatar
Jiwon Yoon committed
112
113
114
115
        questionListChange,
        addQuestion,
        questionList,
        editClick,
Jiwon Yoon's avatar
Jiwon Yoon committed
116
        editCompleteClick,
Jiwon Yoon's avatar
Jiwon Yoon committed
117
        currentId,
118
        questionTypeChange,
Jiwon Yoon's avatar
Jiwon Yoon committed
119
120
121
122
123
124
125
126
      }}
    >
      {children}
    </QuestionContext.Provider>
  );
};

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