Skip to content

Commit 95c4b74

Browse files
committed
Fix create qustion bug
1 parent fb09b02 commit 95c4b74

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

frontend/components/questions/question-form-modal.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ interface QuestionFormModalProps {
3030
}
3131

3232
const QuestionFormModal: React.FC<QuestionFormModalProps> = ({ ...props }) => {
33+
const initialQuestionState: Question = {
34+
id: "",
35+
title: "",
36+
category: "",
37+
complexity: "easy",
38+
description: "",
39+
};
40+
3341
const [question, setQuestion] = useState<Question>(
34-
props.initialData || {
35-
id: "",
36-
title: "",
37-
category: "",
38-
complexity: "easy",
39-
description: "",
40-
}
42+
props.initialData || initialQuestionState
4143
);
4244

4345
useEffect(() => {
@@ -50,6 +52,16 @@ const QuestionFormModal: React.FC<QuestionFormModalProps> = ({ ...props }) => {
5052
e.preventDefault();
5153

5254
props.handleSubmit(question);
55+
setQuestion(initialQuestionState);
56+
};
57+
58+
const handleExit = () => {
59+
if (props.initialData) {
60+
setQuestion(props.initialData);
61+
} else {
62+
setQuestion(initialQuestionState);
63+
}
64+
props.setShowModal(false);
5365
};
5466

5567
return (
@@ -136,10 +148,7 @@ const QuestionFormModal: React.FC<QuestionFormModalProps> = ({ ...props }) => {
136148
{props.isAdmin && (
137149
<Button type="submit">{props.submitButtonText}</Button>
138150
)}
139-
<Button
140-
variant="destructive"
141-
onClick={() => props.setShowModal(false)}
142-
>
151+
<Button variant="destructive" onClick={handleExit}>
143152
Exit
144153
</Button>
145154
</DialogFooter>

0 commit comments

Comments
 (0)