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.choices.length === 0) { if (question.type === ("singletext" || "date")) { question.isEditing = false; console.log("editing completed:", question); handleQuestion(question); } else { alert("질문작성이 완료되지 않았습니다."); return (question.isEditing = true); } } else { question.content.choices.map((choice) => { if (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 ? ( <> 취소 확인 > ) : ( <> 삭제 수정 > )} ); };