import React from 'react'; import { FabButton } from '../base/fab-button'; import { FabModal } from '../base/fab-modal'; import { FormInput } from '../form/form-input'; import { useForm, UseFormRegister } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import Authentication from '../../api/authentication'; import { FieldValues } from 'react-hook-form/dist/types/fields'; import { PasswordInput } from './password-input'; import { FormState } from 'react-hook-form/dist/types/form'; interface ChangePasswordProp { register: UseFormRegister, onError: (message: string) => void, currentFormPassword: string, formState: FormState, } /** * This component shows a button that trigger a modal dialog to verify the user's current password. * If the user's current password is correct, the modal dialog is closed and the button is replaced by a form to set the new password. */ export const ChangePassword = ({ register, onError, currentFormPassword, formState }: ChangePasswordProp) => { const { t } = useTranslation('shared'); const [isModalOpen, setIsModalOpen] = React.useState(false); const [isConfirmedPassword, setIsConfirmedPassword] = React.useState(false); const passwordConfirmationForm = useForm<{ password: string }>(); /** * Opens/closes the dialog asking to confirm the current password before changing it. */ const toggleConfirmationModal = () => { setIsModalOpen(!isModalOpen); }; /** * Callback triggered when the user confirms his current password. */ const onSubmit = (data: { password: string }) => { Authentication.verifyPassword(data.password).then(res => { if (res) { setIsConfirmedPassword(true); toggleConfirmationModal(); } else { onError(t('app.shared.change_password.wrong_password')); } }).catch(err => { onError(err); }); }; return (
{!isConfirmedPassword && toggleConfirmationModal()}> {t('app.shared.change_password.change_my_password')} } {isConfirmedPassword &&
}
{t('app.shared.change_password.confirm')}
); };