diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml index 33418b29..8c9ef24d 100644 --- a/i18n/en_US.yaml +++ b/i18n/en_US.yaml @@ -781,6 +781,9 @@ ui: email: label: Email msg: Email cannot be empty. + pass: + label: Current Password + msg: Password cannot be empty. password_title: Password current_pass: label: Current Password diff --git a/ui/src/components/Modal/PicAuthCodeModal.tsx b/ui/src/components/Modal/PicAuthCodeModal.tsx index ec194a64..3de920e9 100644 --- a/ui/src/components/Modal/PicAuthCodeModal.tsx +++ b/ui/src/components/Modal/PicAuthCodeModal.tsx @@ -51,7 +51,7 @@ const Index: React.FC = ({ type="text" autoComplete="off" placeholder={t('placeholder')} - isInvalid={captcha.isInvalid} + isInvalid={captcha?.isInvalid} onChange={(e) => { Storage.set(CAPTCHA_CODE_STORAGE_KEY, e.target.value); handleCaptcha({ diff --git a/ui/src/pages/Users/Settings/Account/components/ModifyEmail/index.tsx b/ui/src/pages/Users/Settings/Account/components/ModifyEmail/index.tsx index fe8b7ea3..173cbfd6 100644 --- a/ui/src/pages/Users/Settings/Account/components/ModifyEmail/index.tsx +++ b/ui/src/pages/Users/Settings/Account/components/ModifyEmail/index.tsx @@ -4,20 +4,37 @@ import { useTranslation } from 'react-i18next'; import type * as Type from '@/common/interface'; import { useToast } from '@/hooks'; -import { getLoggedUserInfo, changeEmail } from '@/services'; +import { getLoggedUserInfo, changeEmail, checkImgCode } from '@/services'; import { handleFormError } from '@/utils'; +import { PicAuthCodeModal } from '@/components/Modal'; const Index: FC = () => { const { t } = useTranslation('translation', { keyPrefix: 'settings.account', }); const [step, setStep] = useState(1); + const [showModal, setModalState] = useState(false); + const [imgCode, setImgCode] = useState({ + captcha_id: '', + captcha_img: '', + verify: false, + }); const [formData, setFormData] = useState({ e_mail: { value: '', isInvalid: false, errorMsg: '', }, + pass: { + value: '', + isInvalid: false, + errorMsg: '', + }, + captcha_code: { + value: '', + isInvalid: false, + errorMsg: '', + }, }); const [userInfo, setUserInfo] = useState(); const toast = useToast(); @@ -27,13 +44,27 @@ const Index: FC = () => { }); }, []); + const getImgCode = () => { + checkImgCode({ + action: 'e_mail', + }).then((res) => { + setImgCode(res); + }); + }; + + useEffect(() => { + if (step > 1) { + getImgCode(); + } + }, [step]); + const handleChange = (params: Type.FormDataType) => { setFormData({ ...formData, ...params }); }; const checkValidated = (): boolean => { let bol = true; - const { e_mail } = formData; + const { e_mail, pass } = formData; if (!e_mail.value) { bol = false; @@ -43,41 +74,86 @@ const Index: FC = () => { errorMsg: t('email.msg'), }; } + + if (!pass.value) { + bol = false; + formData.pass = { + value: '', + isInvalid: true, + errorMsg: t('pass.msg'), + }; + } setFormData({ ...formData, }); return bol; }; + const initFormData = () => { + setFormData({ + e_mail: { + value: '', + isInvalid: false, + errorMsg: '', + }, + pass: { + value: '', + isInvalid: false, + errorMsg: '', + }, + captcha_code: { + value: '', + isInvalid: false, + errorMsg: '', + }, + }); + }; + + const postEmail = () => { + const params: any = { + e_mail: formData.e_mail.value, + pass: formData.pass.value, + }; + + if (imgCode.verify) { + params.captcha_code = formData.captcha_code.value; + params.captcha_id = imgCode.captcha_id; + } + changeEmail(params) + .then(() => { + setStep(1); + setModalState(false); + toast.onShow({ + msg: t('change_email_info'), + variant: 'warning', + }); + initFormData(); + }) + .catch((err) => { + if (err.isError) { + const data = handleFormError(err, formData); + setFormData({ ...data }); + if (!err.list.find((v) => v.error_field.indexOf('captcha') >= 0)) { + setModalState(false); + } + } + }) + .finally(() => { + getImgCode(); + }); + }; + const handleSubmit = (event: FormEvent) => { event.preventDefault(); event.stopPropagation(); if (!checkValidated()) { return; } - changeEmail({ - e_mail: formData.e_mail.value, - }) - .then(() => { - setStep(1); - toast.onShow({ - msg: t('change_email_info'), - variant: 'warning', - }); - setFormData({ - e_mail: { - value: '', - isInvalid: false, - errorMsg: '', - }, - }); - }) - .catch((err) => { - if (err.isError) { - const data = handleFormError(err, formData); - setFormData({ ...data }); - } - }); + + if (imgCode.verify) { + setModalState(true); + } + postEmail(); }; return ( @@ -103,6 +179,29 @@ const Index: FC = () => { )} {step === 2 && (
+ + {t('pass.label')} + + handleChange({ + pass: { + value: e.target.value, + isInvalid: false, + errorMsg: '', + }, + }) + } + /> + + {formData.pass.errorMsg} + + + {t('email.label')} { {formData.e_mail.errorMsg} +
)} + + setModalState(false)} + /> ); }; diff --git a/ui/src/services/common.ts b/ui/src/services/common.ts index 486fc55c..108473ef 100644 --- a/ui/src/services/common.ts +++ b/ui/src/services/common.ts @@ -249,7 +249,7 @@ export const closeQuestion = (params: { return request.put('/answer/api/v1/question/status', params); }; -export const changeEmail = (params: { e_mail: string }) => { +export const changeEmail = (params: { e_mail: string; pass?: string }) => { return request.post('/answer/api/v1/user/email/change/code', params); };