question.context.tsx 3.99 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
interface questionTypeChangeProp {
  id: string;
jang dong hyeok's avatar
.    
jang dong hyeok committed
13
  selectedType: string;
14
15
}

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>;
jang dong hyeok's avatar
.    
jang dong hyeok committed
25
  questionTypeChange: ({ id, selectedType }: 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
  //질문 Type 바꾸는 함수
jang dong hyeok's avatar
.    
jang dong hyeok committed
85
86
87
88
  function questionTypeChange({
    id,
    selectedType,
  }: questionTypeChangeProp): void {
89
    const newType: BasicQuestionType[] = [...questionList];
90
    const objType: any = newType.find((t) => t._id === id);
jang dong hyeok's avatar
.    
jang dong hyeok committed
91
    objType.type = selectedType;
92
    // TODO
jang dong hyeok's avatar
.    
jang dong hyeok committed
93
    if ((selectedType = "essay")) {
94
95
      objType.content;
    }
96
97
    setQuestionList(newType);
  }
Jiwon Yoon's avatar
Jiwon Yoon committed
98

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

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

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