question.context.tsx 3.94 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";
8
import { BasicQuestionType, SurveyType, EssayType, RadioType } from "../types";
Jiwon Yoon's avatar
Jiwon Yoon committed
9
import { questionApi, surveyApi } from "../apis";
Jiwon Yoon's avatar
Jiwon Yoon committed
10

11
12
13
14
15
interface questionTypeChangeProp {
  id: string;
  tt: string;
}

Jiwon Yoon's avatar
Jiwon Yoon committed
16
interface IQuestionContext {
Jiwon Yoon's avatar
Jiwon Yoon committed
17
18
  handleSurvey: (e: React.ChangeEvent<HTMLInputElement>) => void;
  handleSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
Jiwon Yoon's avatar
Jiwon Yoon committed
19
20
21
  questionListChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
  questionList: BasicQuestionType[];
  editClick: (e: React.MouseEvent<HTMLButtonElement>) => void;
Jiwon Yoon's avatar
Jiwon Yoon committed
22
  editCompleteClick: (e: React.MouseEvent<HTMLButtonElement>) => void;
Jiwon Yoon's avatar
Jiwon Yoon committed
23
24
  currentId: string;
  addQuestion: (e: React.MouseEvent<HTMLButtonElement>) => Promise<void>;
25
  questionTypeChange: ({ id, tt }: questionTypeChangeProp) => void;
Jiwon Yoon's avatar
Jiwon Yoon committed
26
27
28
}

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

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

Jiwon Yoon's avatar
Jiwon Yoon committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  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);
    }
  }
76
  //질문 Title, Comment바꾸는 함수
Jiwon Yoon's avatar
Jiwon Yoon committed
77
78
79
80
81
82
83
  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);
  }
84
85
  //질문 Type 바꾸는 함수
  function questionTypeChange({ id, tt }: questionTypeChangeProp): void {
86
    const newType: BasicQuestionType[] = [...questionList];
87
88
89
90
91
92
    const objType: any = newType.find((t) => t._id === id);
    objType.type = tt;
    // TODO
    if ((tt = "essay")) {
      objType.content;
    }
93
94
    setQuestionList(newType);
  }
Jiwon Yoon's avatar
Jiwon Yoon committed
95

Jiwon Yoon's avatar
Jiwon Yoon committed
96
  async function addQuestion() {
Jiwon Yoon's avatar
Jiwon Yoon committed
97
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
98
      const newQ: BasicQuestionType = await questionApi.createQuestion();
Jiwon Yoon's avatar
Jiwon Yoon committed
99
      setSurvey({ ...survey, questions: [...survey.questions, newQ._id] });
Jiwon Yoon's avatar
Jiwon Yoon committed
100
      setQuestionList([...questionList, newQ]);
Jiwon Yoon's avatar
Jiwon Yoon committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
      // 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
115
116
  function editCompleteClick(e: React.MouseEvent<HTMLButtonElement>) {}

Jiwon Yoon's avatar
Jiwon Yoon committed
117
118
119
  return (
    <QuestionContext.Provider
      value={{
Jiwon Yoon's avatar
Jiwon Yoon committed
120
121
        handleSurvey,
        handleSubmit,
Jiwon Yoon's avatar
Jiwon Yoon committed
122
123
124
125
        questionListChange,
        addQuestion,
        questionList,
        editClick,
Jiwon Yoon's avatar
Jiwon Yoon committed
126
        editCompleteClick,
Jiwon Yoon's avatar
Jiwon Yoon committed
127
        currentId,
128
        questionTypeChange,
Jiwon Yoon's avatar
Jiwon Yoon committed
129
130
131
132
133
134
135
136
      }}
    >
      {children}
    </QuestionContext.Provider>
  );
};

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