PasswordResetForm.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Grid, Header, Image, Segment } from 'semantic-ui-react';
  3. import { API, showError, showInfo, showSuccess } from '../helpers';
  4. import Turnstile from 'react-turnstile';
  5. const PasswordResetForm = () => {
  6. const [inputs, setInputs] = useState({
  7. email: '',
  8. });
  9. const { email } = inputs;
  10. const [loading, setLoading] = useState(false);
  11. const [turnstileEnabled, setTurnstileEnabled] = useState(false);
  12. const [turnstileSiteKey, setTurnstileSiteKey] = useState('');
  13. const [turnstileToken, setTurnstileToken] = useState('');
  14. const [disableButton, setDisableButton] = useState(false);
  15. const [countdown, setCountdown] = useState(30);
  16. useEffect(() => {
  17. let countdownInterval = null;
  18. if (disableButton && countdown > 0) {
  19. countdownInterval = setInterval(() => {
  20. setCountdown(countdown - 1);
  21. }, 1000);
  22. } else if (countdown === 0) {
  23. setDisableButton(false);
  24. setCountdown(30);
  25. }
  26. return () => clearInterval(countdownInterval);
  27. }, [disableButton, countdown]);
  28. function handleChange(e) {
  29. const { name, value } = e.target;
  30. setInputs((inputs) => ({ ...inputs, [name]: value }));
  31. }
  32. async function handleSubmit(e) {
  33. setDisableButton(true);
  34. if (!email) return;
  35. if (turnstileEnabled && turnstileToken === '') {
  36. showInfo('请稍后几秒重试,Turnstile 正在检查用户环境!');
  37. return;
  38. }
  39. setLoading(true);
  40. const res = await API.get(
  41. `/api/reset_password?email=${email}&turnstile=${turnstileToken}`,
  42. );
  43. const { success, message } = res.data;
  44. if (success) {
  45. showSuccess('重置邮件发送成功,请检查邮箱!');
  46. setInputs({ ...inputs, email: '' });
  47. } else {
  48. showError(message);
  49. }
  50. setLoading(false);
  51. }
  52. return (
  53. <Grid textAlign='center' style={{ marginTop: '48px' }}>
  54. <Grid.Column style={{ maxWidth: 450 }}>
  55. <Header as='h2' color='' textAlign='center'>
  56. <Image src='/logo.png' /> 密码重置
  57. </Header>
  58. <Form size='large'>
  59. <Segment>
  60. <Form.Input
  61. fluid
  62. icon='mail'
  63. iconPosition='left'
  64. placeholder='邮箱地址'
  65. name='email'
  66. value={email}
  67. onChange={handleChange}
  68. />
  69. {turnstileEnabled ? (
  70. <Turnstile
  71. sitekey={turnstileSiteKey}
  72. onVerify={(token) => {
  73. setTurnstileToken(token);
  74. }}
  75. />
  76. ) : (
  77. <></>
  78. )}
  79. <Button
  80. color='green'
  81. fluid
  82. size='large'
  83. onClick={handleSubmit}
  84. loading={loading}
  85. disabled={disableButton}
  86. >
  87. {disableButton ? `重试 (${countdown})` : '提交'}
  88. </Button>
  89. </Segment>
  90. </Form>
  91. </Grid.Column>
  92. </Grid>
  93. );
  94. };
  95. export default PasswordResetForm;