Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix(Question): Handling form errors when submitting answer

haitao(lj)1f2800c668b2
diff --git a/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx b/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
index f1c7abbb..050ce0fd 100644
--- a/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
@@ -8,7 +8,7 @@ import classNames from 'classnames';
import { Editor, Modal, TextArea } from '@/components';
import { FormDataType } from '@/common/interface';
import { postAnswer } from '@/services';
-import { guard } from '@/utils';
+import { guard, handleFormError } from '@/utils';
interface Props {
visible?: boolean;
@@ -53,17 +53,24 @@ const Index: FC<Props> = ({ visible = false, data, callback }) => {
question_id: data?.qid,
content: formData.content.value,
html: marked.parse(formData.content.value),
- }).then((res) => {
- setShowEditor(false);
- setFormData({
- content: {
- value: '',
- isInvalid: false,
- errorMsg: '',
- },
+ })
+ .then((res) => {
+ setShowEditor(false);
+ setFormData({
+ content: {
+ value: '',
+ isInvalid: false,
+ errorMsg: '',
+ },
+ });
+ callback?.(res.info);
+ })
+ .catch((ex) => {
+ if (ex.isError) {
+ const stateData = handleFormError(ex, formData);
+ setFormData({ ...stateData });
+ }
});
- callback?.(res.info);
- });
};
const clickBtn = () => {