2022-05-10 10:50:41 +02:00
|
|
|
import React, { useEffect } from 'react';
|
2022-04-26 18:05:18 +02:00
|
|
|
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';
|
2022-05-10 10:50:41 +02:00
|
|
|
import MemberAPI from '../../api/member';
|
2022-07-20 10:45:42 +02:00
|
|
|
import { User } from '../../models/user';
|
2022-04-26 18:05:18 +02:00
|
|
|
|
|
|
|
interface ChangePasswordProp<TFieldValues> {
|
|
|
|
register: UseFormRegister<TFieldValues>,
|
|
|
|
onError: (message: string) => void,
|
|
|
|
currentFormPassword: string,
|
|
|
|
formState: FormState<TFieldValues>,
|
2022-07-20 10:45:42 +02:00
|
|
|
user: User,
|
2022-04-26 18:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-07-20 10:45:42 +02:00
|
|
|
export const ChangePassword = <TFieldValues extends FieldValues>({ register, onError, currentFormPassword, formState, user }: ChangePasswordProp<TFieldValues>) => {
|
2022-04-26 18:05:18 +02:00
|
|
|
const { t } = useTranslation('shared');
|
|
|
|
|
|
|
|
const [isModalOpen, setIsModalOpen] = React.useState<boolean>(false);
|
|
|
|
const [isConfirmedPassword, setIsConfirmedPassword] = React.useState<boolean>(false);
|
2022-05-10 10:50:41 +02:00
|
|
|
const [isPrivileged, setIsPrivileged] = React.useState<boolean>(false);
|
2022-04-26 18:05:18 +02:00
|
|
|
|
2022-05-04 15:29:01 +02:00
|
|
|
const { handleSubmit, register: passwordRegister } = useForm<{ password: string }>();
|
2022-04-26 18:05:18 +02:00
|
|
|
|
2022-05-10 10:50:41 +02:00
|
|
|
useEffect(() => {
|
2022-07-20 10:45:42 +02:00
|
|
|
MemberAPI.current().then(operator => {
|
|
|
|
setIsPrivileged((operator.role === 'admin' || operator.role === 'manager') && user.id !== operator.id);
|
2022-05-10 10:50:41 +02:00
|
|
|
}).catch(error => onError(error));
|
|
|
|
}, []);
|
|
|
|
|
2022-04-26 18:05:18 +02:00
|
|
|
/**
|
|
|
|
* Opens/closes the dialog asking to confirm the current password before changing it.
|
|
|
|
*/
|
|
|
|
const toggleConfirmationModal = () => {
|
|
|
|
setIsModalOpen(!isModalOpen);
|
|
|
|
};
|
|
|
|
|
2022-05-10 10:50:41 +02:00
|
|
|
/**
|
|
|
|
* Callback triggered when the user clicks on the "change my password" button
|
|
|
|
*/
|
|
|
|
const handleChangePasswordRequested = () => {
|
|
|
|
if (isPrivileged) {
|
|
|
|
setIsConfirmedPassword(true);
|
|
|
|
} else {
|
|
|
|
toggleConfirmationModal();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-26 18:05:18 +02:00
|
|
|
/**
|
|
|
|
* Callback triggered when the user confirms his current password.
|
|
|
|
*/
|
2022-05-04 15:29:01 +02:00
|
|
|
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
if (event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
return handleSubmit((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);
|
|
|
|
});
|
|
|
|
})(event);
|
2022-04-26 18:05:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="change-password">
|
2022-05-10 10:50:41 +02:00
|
|
|
{!isConfirmedPassword && <FabButton onClick={() => handleChangePasswordRequested()}>
|
2022-04-26 18:05:18 +02:00
|
|
|
{t('app.shared.change_password.change_my_password')}
|
|
|
|
</FabButton>}
|
|
|
|
{isConfirmedPassword && <div className="password-fields">
|
|
|
|
<PasswordInput register={register} currentFormPassword={currentFormPassword} formState={formState} />
|
|
|
|
</div>}
|
2022-05-04 15:29:01 +02:00
|
|
|
<FabModal isOpen={isModalOpen} toggleModal={toggleConfirmationModal} title={t('app.shared.change_password.change_my_password')} closeButton>
|
|
|
|
<form onSubmit={onSubmit}>
|
|
|
|
<FormInput id="password"
|
|
|
|
type="password"
|
|
|
|
register={passwordRegister}
|
|
|
|
rules={{ required: true }}
|
|
|
|
label={t('app.shared.change_password.confirm_current')} />
|
2022-04-26 18:05:18 +02:00
|
|
|
<FabButton type="submit">
|
|
|
|
{t('app.shared.change_password.confirm')}
|
|
|
|
</FabButton>
|
|
|
|
</form>
|
|
|
|
</FabModal>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|