diff --git a/frontend/src/components/QuizForm/QuizCard.jsx b/frontend/src/components/QuizForm/QuizCard.jsx index 94fec6e..b734fc2 100644 --- a/frontend/src/components/QuizForm/QuizCard.jsx +++ b/frontend/src/components/QuizForm/QuizCard.jsx @@ -1,17 +1,23 @@ -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import styles from './QuizCard.module.css'; import CloseIcon from '/src/assets/icons/close.svg?react'; import PlusIcon from '/src/assets/icons/plus.svg?react'; +import { Toggle } from '../Toggle'; +import { STATIC_URL } from '../../constants'; export default function QuizCard({ quiz, updateQuiz, deleteQuiz }) { - // TODO: 카드 디자인 완성 및 이쁘게 바꾸기 const [question, setQuestion] = useState(quiz.question || ''); const [answer, setAnswer] = useState(Number(quiz.answer) || ''); - const [choices, setChoices] = useState(quiz.choices || []); + const [choices, setChoices] = useState(quiz.choices || [{ num: 1, content: '' }]); const [image, setImage] = useState(quiz.image || null); - const [imagePreview, setImagePreview] = useState( - quiz.image ? `${import.meta.env.VITE_STATIC_URL}${quiz.image}` : null - ); + const [imagePreview, setImagePreview] = useState(quiz.image ? `${STATIC_URL}${quiz.image}` : null); + const [quizType, setQuizType] = useState('단답식'); + + const clearImage = () => { + setImage(null); + setImagePreview(null); + }; + const handleChoiceChange = (num, content) => { const updatedChoices = choices.map((choice) => (choice.num === num ? { ...choice, content } : choice)); setChoices(updatedChoices); @@ -19,32 +25,36 @@ export default function QuizCard({ quiz, updateQuiz, deleteQuiz }) { }; const handleAddChoice = () => { - if (choices.length < 4) { - const newChoice = { num: choices.length + 1, content: '' }; - const updatedChoices = [...choices, newChoice]; - setChoices(updatedChoices); - updateQuiz(quiz.id, { ...quiz, question, answer, choices: updatedChoices, image }); + if (choices.length >= 4) { + return; } + const newChoice = { num: choices.length + 1, content: '' }; + const updatedChoices = [...choices, newChoice]; + setChoices(updatedChoices); + updateQuiz(quiz.id, { ...quiz, question, answer, choices: updatedChoices, image }); }; const handlePopChoice = () => { - if (choices.length > 0) { - const updatedChoices = choices.slice(0, -1); - setChoices(updatedChoices); - if (updatedChoices.length < answer) { - setAnswer(''); - } - updateQuiz(quiz.id, { ...quiz, question, answer, choices: updatedChoices, image }); + if (choices.length <= 1) { + return; } + const updatedChoices = choices.slice(0, -1); + setChoices(updatedChoices); + if (updatedChoices.length < answer) { + setAnswer(1); + } + updateQuiz(quiz.id, { ...quiz, question, answer, choices: updatedChoices, image }); }; const handleFileChange = (e) => { - const file = e.target.files[0] ?? null; - if (!file || !file.type.startsWith('image/')) { + const file = e.target.files[0]; + if (!file) { + return; + } + if (!file.type.startsWith('image/')) { alert('이미지 파일만 업로드 해주세요'); e.target.value = null; - setImage(null); - setImagePreview(null); + clearImage(); return; } setImage(file); @@ -61,15 +71,31 @@ export default function QuizCard({ quiz, updateQuiz, deleteQuiz }) { }; const handleChoiceSelect = (choiceContent) => { - console.log(choiceContent); setAnswer(choiceContent); updateQuiz(quiz.id, { ...quiz, question, answer: choiceContent, choices, image }); }; + useEffect(() => { + quizType === '단답식' ? setAnswer('') : setAnswer(1); + }, [quizType]); + return (
- 퀴즈 생성 카드 +
+ Q. + { + setQuestion(e.target.value); + updateQuiz(quiz.id, { ...quiz, question: e.target.value, answer, choices, image }); + }} + placeholder="질문 내용을 입력하세요" + /> +
-