PasswordResetRequestForm.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React, { FC, useState, useCallback } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import Link from 'next/link';
  4. import { apiv3Post } from '~/client/util/apiv3-client';
  5. import { toastSuccess, toastError } from '~/client/util/toastr';
  6. import { useIsMailerSetup } from '~/stores/context';
  7. const PasswordResetRequestForm: FC = () => {
  8. const { t } = useTranslation();
  9. const { data: isMailerSetup } = useIsMailerSetup();
  10. const [email, setEmail] = useState('');
  11. const changeEmail = useCallback((inputValue) => {
  12. setEmail(inputValue);
  13. }, []);
  14. const sendPasswordResetRequestMail = useCallback(async(e) => {
  15. e.preventDefault();
  16. if (email === '') {
  17. toastError(t('forgot_password.email_is_required'));
  18. return;
  19. }
  20. try {
  21. await apiv3Post('/forgot-password', { email });
  22. toastSuccess(t('forgot_password.success_to_send_email'));
  23. }
  24. catch (err) {
  25. toastError(err);
  26. }
  27. }, [t, email]);
  28. return (
  29. <form onSubmit={sendPasswordResetRequestMail}>
  30. {!isMailerSetup ? (
  31. <div className="alert alert-danger">
  32. {t('forgot_password.please_enable_mailer_alert')}
  33. </div>
  34. ) : (
  35. <>
  36. <h1><i className="icon-lock large"></i></h1>
  37. <h1 className="text-center">{ t('forgot_password.forgot_password') }</h1>
  38. <h3>{t('forgot_password.password_reset_request_desc')}</h3>
  39. <div className="form-group">
  40. <div className="input-group">
  41. <input
  42. name="email"
  43. placeholder="E-mail Address"
  44. className="form-control"
  45. type="email"
  46. disabled={!isMailerSetup}
  47. onChange={e => changeEmail(e.target.value)}
  48. />
  49. </div>
  50. </div>
  51. <div className="form-group">
  52. <button
  53. className="btn btn-lg btn-primary btn-block"
  54. type="submit"
  55. disabled={!isMailerSetup}
  56. >
  57. {t('forgot_password.send')}
  58. </button>
  59. </div>
  60. </>
  61. )}
  62. <Link href='/login' prefetch={false}>
  63. <i className="icon-login mr-1" />{t('forgot_password.return_to_login')}
  64. </Link>
  65. </form>
  66. );
  67. };
  68. export default PasswordResetRequestForm;