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: (id: string) => void; deleteQuestion: (id: string) => void; isSave: boolean; }; export const Question = ({ element, handleQuestion, deleteQuestion, isSave, }: Props) => { const [save, setSave] = useState(isSave); async function handleEditComplete() { try { const newQuestion: BasicQuestionType = await questionApi.updateQuestion( element ); console.log(newQuestion); setSave(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); if ( selectedType === "radio" || selectedType === "dropdown" || selectedType === "checkbox" ) { element.content = { choices: [{ text: "", value: 0 }], }; } else if (selectedType === "essay") { element.content = { choices: [] }; } else if (selectedType === "rating") { element.content = { minRateDescription: "", maxRateDescription: "", choices: [{ text: "", value: 0 }], }; } element.type = selectedType; handleQuestion(element._id); } function handleQuestionInfo(event: React.ChangeEvent) { const { name, value } = event.currentTarget; element[name] = value; handleQuestion(element._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; element[value] = checked; handleQuestion(element._id); }; const handleDelete = () => { if (window.confirm("질문을 삭제하시겠습니까?")) { deleteQuestion(element._id); alert("삭제되었습니다."); } else { alert("질문 삭제를 취소합니다."); } }; const handleEditClick = () => { setSave(false); }; return (
{getContent(element)}
{save ? ( ) : ( )}
); };