import React, { useState } from "react"; import { BasicQuestionType } from "../types"; import { questionApi } from "../apis"; import { EssayForm } from "./EssayForm"; import { CheckboxForm } from "./CheckboxForm"; import { RadioForm } from "./RadioForm"; import { DropdownForm } from "./DropdownForm"; import { FileForm } from "./FileForm"; import { RatingForm } from "./RatingForm"; import { DateForm } from "./DateForm"; import { QUESTION_TYPES } from "../commons"; type Props = { element: BasicQuestionType; handleQuestion: (element: BasicQuestionType) => void; deleteQuestion: (id: string) => void; // isSave: boolean; }; export const Question = ({ element, handleQuestion, deleteQuestion, }: // isSave, Props) => { const [question, setQuestion] = useState({ ...element }); const [isSaved, setIsSaved] = useState(false); async function handleEditComplete() { try { console.log(question); const newQuestion: BasicQuestionType = await questionApi.updateQuestion( element ); // console.log(newQuestion); handleQuestion(question); setIsSaved(true); // setSuccess(true); // setError(""); } catch (error) { console.log("에러발생"); // catchErrors(error, setError) } finally { // setLoading(false); } } function handleSelect(event: React.ChangeEvent) { const selectedType = event.currentTarget.value; console.log(selectedType); let content; if ( selectedType === "radio" || selectedType === "dropdown" || selectedType === "checkbox" ) { content = { choices: [ { text: "", value: 0 }, { text: "", value: 1 }, { text: "", value: 2 }, ], }; } else if (selectedType === "essay") { content = { choices: [] }; } else if (selectedType === "rating") { content = { minRateDescription: "", maxRateDescription: "", choices: [ { text: "", value: 0 }, { text: "", value: 1 }, { text: "", value: 2 }, ], }; } // question.type = selectedType; setQuestion({ ...question, type: selectedType, content: content }); // handleQuestion(question._id); } const handleElement = () => { console.log("handle element"); setQuestion({ ...question }); }; function handleQuestionInfo(event: React.ChangeEvent) { const { name, value } = event.currentTarget; // question[name] = value; setQuestion({ ...question, [name]: value }); // handleQuestion(question._id); } function getContent(element: BasicQuestionType) { switch (element.type) { case "essay": return ; case "radio": return ( ); case "checkbox": return ( ); case "dropdown": return ( ); case "file": return ; case "rating": return ( ); case "date": return ; default: return <>; } } const handleRequired = (event: React.ChangeEvent) => { const { checked, value } = event.currentTarget; question[value] = checked; setQuestion({ ...question, [value]: checked }); // handleQuestion(question._id); }; const onCancel = () => { console.log("element canceled button clicked", element); console.log("question canceled button clicked", question); setQuestion(element); setIsSaved(true); }; const handleDelete = () => { deleteQuestion(question._id); }; const handleEditClick = () => { setIsSaved(false); }; return (
{getContent(question)}
{isSaved ? ( <> ) : ( <> )}
); };