fix(Question): Handle unexpected state handling issues when CAPTCHA and draft functions are mixed together.

This commit is contained in:
haitaoo 2023-08-01 14:44:36 +08:00
parent 601721a921
commit 9184b9b416
2 changed files with 11 additions and 14 deletions

View File

@ -192,7 +192,7 @@ const Comment = ({ objectId, mode, commentId }) => {
if (ex.isError) { if (ex.isError) {
const captchaErr = addCaptcha.handleCaptchaError(ex.list); const captchaErr = addCaptcha.handleCaptchaError(ex.list);
// If it is not a CAPTCHA error, leave it to the subsequent error handling logic to continue processing. // If it is not a CAPTCHA error, leave it to the subsequent error handling logic to continue processing.
if (!captchaErr || captchaErr.error_msg !== ex.msg) { if (!(captchaErr && ex.list.length === 1)) {
return Promise.reject(ex); return Promise.reject(ex);
} }
} }

View File

@ -244,7 +244,6 @@ const Ask = () => {
}; };
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
contentChangedRef.current = false;
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
@ -256,6 +255,7 @@ const Ask = () => {
if (isEdit) { if (isEdit) {
editCaptcha.check(() => { editCaptcha.check(() => {
contentChangedRef.current = false;
const ep = { const ep = {
...params, ...params,
id: qid, id: qid,
@ -283,6 +283,7 @@ const Ask = () => {
}); });
} else { } else {
saveCaptcha.check(async () => { saveCaptcha.check(async () => {
contentChangedRef.current = false;
const imgCode = saveCaptcha.getCaptcha(); const imgCode = saveCaptcha.getCaptcha();
if (imgCode.verify) { if (imgCode.verify) {
params.captcha_code = imgCode.captcha_code; params.captcha_code = imgCode.captcha_code;
@ -295,25 +296,21 @@ const Ask = () => {
answer_content: formData.answer_content.value, answer_content: formData.answer_content.value,
}).catch((err) => { }).catch((err) => {
if (err.isError) { if (err.isError) {
saveCaptcha.handleCaptchaError(err.list); const captchaErr = saveCaptcha.handleCaptchaError(err.list);
const data = handleFormError(err, formData); if (!(captchaErr && err.list.length === 1)) {
if (data.keys.includes('captcha')) { const data = handleFormError(err, formData);
delete data.captcha_code; setFormData({ ...data });
delete data.captcha_id;
} }
setFormData({ ...data });
} }
}); });
} else { } else {
res = await saveQuestion(params).catch((err) => { res = await saveQuestion(params).catch((err) => {
if (err.isError) { if (err.isError) {
saveCaptcha.handleCaptchaError(err.list); const captchaErr = saveCaptcha.handleCaptchaError(err.list);
const data = handleFormError(err, formData); if (!(captchaErr && err.list.length === 1)) {
if (data.keys.includes('captcha')) { const data = handleFormError(err, formData);
delete data.captcha_code; setFormData({ ...data });
delete data.captcha_id;
} }
setFormData({ ...data });
} }
}); });
} }