diff --git a/src/pages/profile/components/profile-content/ProfileContent.js b/src/pages/profile/components/profile-content/ProfileContent.js index 5ac01b9..48c0510 100644 --- a/src/pages/profile/components/profile-content/ProfileContent.js +++ b/src/pages/profile/components/profile-content/ProfileContent.js @@ -1,7 +1,7 @@ import Component from '../../../../core/components/component/Component'; import FormControl from '../../../../core/components/form-control/FormControl'; import {FORM_TYPES, EVENTS} from '../../../../core/consts'; -import {INPUT_IDS, LABELS} from './consts'; +import {INPUT_IDS, LABELS, ERRORS} from './consts'; import notify from '../../../../services/NotifyService'; import profileServiceApi from '../../../../api/ProfileServiceAPI'; @@ -48,22 +48,22 @@ class ProfileContent extends Component { const newPasswordRepeat = this.newPasswordRepeat.getValue(); if (!oldPassword) { - this.oldPassword.setError('Заполните старый пароль'); + this.oldPassword.setError(ERRORS.TYPED_OLD_PASSWORD); } if (!newPassword) { - this.newPassword.setError('Заполните новый пароль'); + this.newPassword.setError(ERRORS.TYPED_NEW_PASSWORD); } const isEqualPassword = newPassword === newPasswordRepeat; if (!isEqualPassword) { - this.newPassword.setError('Пароли не совпадают'); - this.newPasswordRepeat.setError('Пароли не совпадают'); + this.newPassword.setError(ERRORS.PASSWORD_NOT_EQUAL); + this.newPasswordRepeat.setError(ERRORS.PASSWORD_NOT_EQUAL); } if (!newPasswordRepeat) { - this.newPasswordRepeat.setError('Повторите новый пароль'); + this.newPasswordRepeat.setError(ERRORS.REPEAT_PASSWORD); } return this.form.checkValidity() && isEqualPassword; @@ -81,9 +81,16 @@ class ProfileContent extends Component { if (this.validateInputs()) { const oldPassword = this.oldPassword.getValue(); const newPassword = this.newPassword.getValue(); - await profileServiceApi.changePassword(oldPassword, newPassword); + try { + await profileServiceApi.changePassword(oldPassword, newPassword); + } catch (e) { + if (e?.response?.data?.message === ERRORS.NOT_CORRECT_PASSWORD) { + this.oldPassword.setError(ERRORS.NOT_CORRECT_PASSWORD); + } + throw new Error(e); + } this.clearForm(); - notify.success('Пароль успешно изменен'); + notify.success(LABELS.SUCCESS_CHANGE_PASSWORD); } } diff --git a/src/pages/profile/components/profile-content/consts.js b/src/pages/profile/components/profile-content/consts.js index edcf068..c9068fc 100644 --- a/src/pages/profile/components/profile-content/consts.js +++ b/src/pages/profile/components/profile-content/consts.js @@ -11,4 +11,13 @@ export const LABELS = { OLD_PASSWORD_PLACEHOLDER: 'Введите старый пароль', NEW_PASSWORD_PLACEHOLDER: 'Введите новый пароль', NEW_PASSWORD_REPEAT_PLACEHOLDER: 'Введите новый пароль еще раз', + SUCCESS_CHANGE_PASSWORD: '', +}; + +export const ERRORS = { + TYPED_OLD_PASSWORD: 'Заполните старый пароль', + TYPED_NEW_PASSWORD: 'Заполните новый пароль', + PASSWORD_NOT_EQUAL: 'Пароли не совпадают', + REPEAT_PASSWORD: 'Повторите новый пароль', + NOT_CORRECT_PASSWORD: 'Не верный старый пароль', };