import FormControl from '@material-ui/core/FormControl'; import React from 'react'; import { useIntl } from 'react-intl'; import { useMutation } from 'react-query'; import Client, { ErrorInfo } from '../../../../classes/client'; import Input from '../../../form/input'; import BaseDialog from '../../action-dispatcher/base-dialog'; import { useSelector } from 'react-redux'; import { activeInstance } from '../../../../redux/clientSlice'; type ChangePasswordDialogProps = { onClose: () => void; }; type ChangePasswordModel = { password: string; retryPassword: string; }; const defaultModel: ChangePasswordModel = { password: '', retryPassword: '' }; const ChangePasswordDialog = ({ onClose }: ChangePasswordDialogProps): React.ReactElement => { const client: Client = useSelector(activeInstance); const [model, setModel] = React.useState(defaultModel); const [error, setError] = React.useState(); const intl = useIntl(); const mutation = useMutation( (model: ChangePasswordModel) => { return client.updateAccountPassword(model.password); }, { onSuccess: () => { onClose(); }, onError: (error) => { setError(error); }, } ); const handleOnClose = (): void => { onClose(); setModel(defaultModel); setError(undefined); }; const handleOnSubmit = (event: React.FormEvent): void => { event.preventDefault(); // Check password are equal ... if (model.password != model.retryPassword) { setError({ msg: intl.formatMessage({ id: 'changepwd.password-match', defaultMessage: 'Password do not match. Please, try again.', }), }); return; } mutation.mutate(model); }; const handleOnChange = (event: React.ChangeEvent): void => { event.preventDefault(); const name = event.target.name; const value = event.target.value; setModel({ ...model, [name as keyof ChangePasswordModel]: value }); }; return ( ); }; export default ChangePasswordDialog;