PasswordResetRequestForm.tsx 2.4 KB

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