Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import { usePatchQuestionMutation } from '@lib/api/mutations';
import { QuestionResponseDto } from '@uniquegood/realworld-adventure-interface';
import { Checkbox, Form, Input, InputNumber, Modal, ModalProps, message } from 'antd';
import React from 'react';
import QuillEditor from '@lib/components/QuillEditor';
import { Delta } from 'quill';
declare global {
interface Window {
Quill: object;
}
}
interface ModifyQuestionModalProps {
modalData: ModalProps;
closeModal: () => unknown;
initialData: QuestionResponseDto;
}
export default function ModifyQuestionModal({
modalData,
closeModal,
initialData
}: ModifyQuestionModalProps) {
const [content, setContent] = React.useState<Delta>();
const [hint, setHint] = React.useState<{ delta?: Delta; id: string }>();
const { mutateAsync } = usePatchQuestionMutation({ questionId: initialData.id });
const [form] = Form.useForm();
const handleOkClick = () => {
form.submit();
};
const handleFinish = async (values: {
title: string;
answer: string;
objectVideoUrl?: string;
isRandomAllowed: boolean;
questionHintDeductionPoint: string;
}) => {
const { title, answer, objectVideoUrl, isRandomAllowed, questionHintDeductionPoint } = values;
if (!hint) {
message.error('힌트를 작성해주세요.');
return;
}
const data = await mutateAsync({
title,
content: JSON.stringify(content),
answer,
isQuestionHintAllowed: true,
createOrUpdateHint: {
hintId: hint?.id || null,
content: JSON.stringify(hint?.delta),
deductionScore: Number(questionHintDeductionPoint)
},
objectVideoUrl,
isRandomAllowed
});
if (data.success) {
message.success('문제를 수정했습니다.');
closeModal();
}
};
const handleInitialize = () => {
form.resetFields();
setContent(undefined);
// setHint(undefined);
setContent(JSON.parse(initialData.content));
const hint = initialData.hintList[0];
setHint({
delta: JSON.parse(hint.content || '') as Delta,
id: hint.id
});
form.setFieldsValue({
questionHintDeductionPoint: hint.deductionScore
});
form.setFieldsValue({
title: initialData.title,
answer: initialData.answer,
objectVideoUrl: initialData.objectVideoUrl,
isRandomAllowed: initialData.isRandomAllowed
});
};
React.useEffect(() => {
handleInitialize();
}, [initialData]);
return (
<Modal
{...modalData}
title="문제 수정"
width={1000}
onOk={handleOkClick}
okText="확인"
cancelText="닫기"
afterClose={handleInitialize}
>
<Form
form={form}
onFinish={handleFinish}
labelCol={{ span: 24 }}
wrapperCol={{ span: 24 }}
// eslint-disable-next-line no-template-curly-in-string
validateMessages={{ required: '${label}은(는) 필수로 입력해주세요.' }}
>
<Form.Item name="title" label="제목" rules={[{ required: true }]}>
<Input placeholder="문제를 입력해주세요." />
</Form.Item>
<Form.Item label="문제" rules={[{ required: true }]}>
<QuillEditor value={content} onChange={setContent} />
</Form.Item>
<Form.Item name="answer" label="정답" rules={[{ required: true }]}>
<Input placeholder="정답을 입력해주세요." />
</Form.Item>
<Form.Item name="objectVideoUrl" label="비디오 URL (선택)">
<Input placeholder="유튜브 URL을 입력해주세요." />
</Form.Item>
<Form.Item name="isRandomAllowed" valuePropName="checked">
<Checkbox>랜덤 배정 여부</Checkbox>
</Form.Item>
<Form.Item label="문제 힌트" rules={[{ required: true }]}>
<QuillEditor
value={hint?.delta}
onChange={(delta) => setHint((prev) => ({ delta, id: prev?.id || '' }))}
/>
</Form.Item>
<Form.Item
name="questionHintDeductionPoint"
label="감점될 점수"
rules={[{ required: true }]}
>
<InputNumber placeholder="감점될 점수" controls={false} />
</Form.Item>
</Form>
</Modal>
);
}
|