PasswordResetRequestForm.tsx 2.4 KB

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