"use client" import { create, QuizFormValue, update } from "@/client/quiz" import "@uiw/react-markdown-preview/markdown.css" import "@uiw/react-md-editor/markdown-editor.css" import { Button, Checkbox, Divider, Form, Input, message, Radio } from "antd" import { useForm } from "antd/lib/form/Form" import dynamic from "next/dynamic" import Router from "next/router" import React, { useEffect, useState } from "react" import DefaultForm from "@/components/shared/form/ui/default-form" import FormGroup from "@/components/shared/form/ui/form-group" import FormSection from "@/components/shared/form/ui/form-section" import router from "next/router" interface Props { pid?: string qid?: string data?: any } const QuizCheckForm = (props: Props) => { const { pid, qid, data } = props const program_id = router.query.program_id ? (router.query.program_id as string) : "" const sequence = data?.sequence ? data?.sequence : router.query.sequence ? Number(router.query.sequence as string) : 1 const [form] = useForm() const [isLoading, setIsLoading] = useState(false) const [messageApi, contextHolder] = message.useMessage() const [quizType, setQuizType] = React.useState(data ? data.quiz_choicec : "choice") const [answer, setAnswer] = React.useState("") const [formData, setFormData] = useState(null) const handleFinish = async (value: any) => { console.log("value", value) let answer: string[] = [] if (value.check1) { answer.push("1") } if (value.check2) { answer.push("2") } if (value.check3) { answer.push("3") } if (value.check4) { answer.push("4") } if (answer.length === 0) { messageApi.error("해답을 선택하세요.") return } let params: QuizFormValue = { quiz_type: "check", program_id: data ? data.program_id : pid, answer: answer, choice: [value.choice1!, value.choice2!, value.choice3!, value.choice4!], sequence: Number(value.sequence), question: value.question, hint: value.hint, comment: "", } console.log("params", params) try { setIsLoading(true) if (qid) { await update(qid, params) messageApi.success("수정되었습니다") setTimeout(() => Router.back(), 500) } else { await create(params) messageApi.success("생성되었습니다") setTimeout(() => Router.back(), 500) } } catch (e: unknown) { messageApi.error("에러가 발생했습니다") } finally { setTimeout(() => setIsLoading(false), 500) } } const Editor = dynamic(() => import("@uiw/react-md-editor"), { ssr: false }) const selectQuizType = (e: any) => { setQuizType(e.target.value) } const selectAnswer = (e: any) => { console.log("selectAnswer", e.target.value) setAnswer(e.target.value) } const updateData = (data: any) => { console.log("updateData", data) setAnswer(data?.answer[0]) setFormData(data) setFormData((prevData: any) => ({ ...prevData, ["choice1"]: data?.choice[0] ?? "", ["choice2"]: data?.choice[1] ?? "", ["choice3"]: data?.choice[2] ?? "", ["choice4"]: data?.choice[3] ?? "", ["check1"]: data?.answer?.includes("1"), ["check2"]: data?.answer?.includes("2"), ["check3"]: data?.answer?.includes("3"), ["check4"]: data?.answer?.includes("4"), })) } useEffect(() => { console.log("use effect data", data) if (data) { updateData(data) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [data]) useEffect(() => { console.log("formData", formData) if (!data && !formData) { const sequence = router.query.sequence setFormData({ sequence: sequence }) } form.resetFields() }, [data, form, formData]) return ( <> {contextHolder} form={form} initialValues={formData} onFinish={handleFinish}>
해답 1
해답 2
해답 3
해답 4
) } export default React.memo(QuizCheckForm)