PasswordResetRequestForm.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. {/* lock-icon large */}
  37. <h1><span className="material-symbols-outlined">lock</span></h1>
  38. <h1 className="text-center">{ t('forgot_password.forgot_password') }</h1>
  39. <h3>{t('forgot_password.password_reset_request_desc')}</h3>
  40. <div>
  41. <div className="input-group">
  42. <input
  43. name="email"
  44. placeholder="E-mail Address"
  45. className="form-control"
  46. type="email"
  47. disabled={!isMailerSetup}
  48. onChange={e => changeEmail(e.target.value)}
  49. />
  50. </div>
  51. </div>
  52. <div>
  53. <button
  54. className="btn btn-lg btn-primary"
  55. type="submit"
  56. disabled={!isMailerSetup}
  57. >
  58. {t('forgot_password.send')}
  59. </button>
  60. </div>
  61. </>
  62. )}
  63. <Link href="/login" prefetch={false}>
  64. <span className="material-symbols-outlined">login</span>{t('forgot_password.return_to_login')}
  65. </Link>
  66. </form>
  67. );
  68. };
  69. export default PasswordResetRequestForm;