PasswordResetRequestForm.tsx 2.3 KB

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