import React, { useState } from "react"; import { getEnumKeyByEnumValue, QUESTION_TYPES } from "../commons"; import { getElementByQuestionType } from "../helpers"; import { IQuestionProps } from "../types"; const options = Object.entries(QUESTION_TYPES).map(([type, value]) => ( )); export const Question = ({ element, handleQuestion, deleteQuestion, }: IQuestionProps) => { const [question, setQuestion] = useState(element); const isEditing = question.isEditing; async function handleEditComplete() { switch (question.type) { case "rating": case "checkbox": case "radio": case "dropdown": question.content.choices.map((choice) => { if ( question.title.length < 1 || question.content.length < 0 || choice.text.trim() === "" ) { alert("빈칸을 채워주세요"); return (question.isEditing = true); } else { question.isEditing = false; console.log("editing completed:", question); handleQuestion(question); } }); break; default: if (question.title === "") { alert("제목을 입력해주세요"); question.isEditing = true; } else { question.isEditing = false; handleQuestion(question); } break; } } function handleSelect(event: React.ChangeEvent) { const selectedType = event.currentTarget.value; console.log(selectedType); const selectedKind = getEnumKeyByEnumValue(QUESTION_TYPES, selectedType) ?? "singletext"; console.log("selected kind:", selectedKind); setQuestion({ ...question, type: selectedKind }); } const handleElement = () => { console.log("handle element"); setQuestion({ ...question }); }; function handleChange(event: React.ChangeEvent) { const { checked, name, value } = event.currentTarget; if (name === "isRequired") { return setQuestion({ ...question, [name]: checked }); } setQuestion({ ...question, [name]: value }); } const onCancel = () => { const originalQuestion = { ...element, isEditing: false }; setQuestion(originalQuestion); handleQuestion(originalQuestion); }; const onDelete = () => { if (window.confirm("질문을 삭제하시겠습니까?")) { deleteQuestion(question._id); } }; const onEdit = () => { setQuestion({ ...question, isEditing: true }); handleQuestion({ ...question, isEditing: true }); }; return (
{getElementByQuestionType(question, handleElement, isEditing)}
{isEditing ? ( <> ) : ( <> )}
); };