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]) => ( {value} )); export const Question = ({ element, handleQuestion, deleteQuestion, }: IQuestionProps) => { const [question, setQuestion] = useState(element); const isEditing = question.isEditing; async function handleEditComplete() { if (question.content.length < 0 || question.title.length > 1) { question.isEditing = false; handleQuestion(question); } question.content.choices.map((choice) => { if (question.title.length < 1 || choice.text.trim() === "") { alert("빈칸을 채워주세요"); return (question.isEditing = true); } else { question.isEditing = false; console.log("editing completed:", question); handleQuestion(question); } }); } 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)} {options} 필수 {isEditing ? ( <> 취소 확인 > ) : ( <> 삭제 수정 > )} ); };