PasswordResetConfirm.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Grid, Header, Image, Segment } from 'semantic-ui-react';
  3. import { API, copy, showError, showInfo, showNotice, showSuccess } from '../helpers';
  4. import { useSearchParams } from 'react-router-dom';
  5. const PasswordResetConfirm = () => {
  6. const [inputs, setInputs] = useState({
  7. email: '',
  8. token: '',
  9. });
  10. const { email, token } = inputs;
  11. const [loading, setLoading] = useState(false);
  12. const [disableButton, setDisableButton] = useState(false);
  13. const [countdown, setCountdown] = useState(30);
  14. const [newPassword, setNewPassword] = useState('');
  15. const [searchParams, setSearchParams] = useSearchParams();
  16. useEffect(() => {
  17. let token = searchParams.get('token');
  18. let email = searchParams.get('email');
  19. setInputs({
  20. token,
  21. email,
  22. });
  23. }, []);
  24. useEffect(() => {
  25. let countdownInterval = null;
  26. if (disableButton && countdown > 0) {
  27. countdownInterval = setInterval(() => {
  28. setCountdown(countdown - 1);
  29. }, 1000);
  30. } else if (countdown === 0) {
  31. setDisableButton(false);
  32. setCountdown(30);
  33. }
  34. return () => clearInterval(countdownInterval);
  35. }, [disableButton, countdown]);
  36. async function handleSubmit(e) {
  37. setDisableButton(true);
  38. if (!email) return;
  39. setLoading(true);
  40. const res = await API.post(`/api/user/reset`, {
  41. email,
  42. token,
  43. });
  44. const { success, message } = res.data;
  45. if (success) {
  46. let password = res.data.data;
  47. setNewPassword(password);
  48. await copy(password);
  49. showNotice(`新密码已复制到剪贴板:${password}`);
  50. } else {
  51. showError(message);
  52. }
  53. setLoading(false);
  54. }
  55. return (
  56. <Grid textAlign='center' style={{ marginTop: '48px' }}>
  57. <Grid.Column style={{ maxWidth: 450 }}>
  58. <Header as='h2' color='' textAlign='center'>
  59. <Image src='/logo.png' /> 密码重置确认
  60. </Header>
  61. <Form size='large'>
  62. <Segment>
  63. <Form.Input
  64. fluid
  65. icon='mail'
  66. iconPosition='left'
  67. placeholder='邮箱地址'
  68. name='email'
  69. value={email}
  70. readOnly
  71. />
  72. {newPassword && (
  73. <Form.Input
  74. fluid
  75. icon='lock'
  76. iconPosition='left'
  77. placeholder='新密码'
  78. name='newPassword'
  79. value={newPassword}
  80. readOnly
  81. onClick={(e) => {
  82. e.target.select();
  83. navigator.clipboard.writeText(newPassword);
  84. showNotice(`密码已复制到剪贴板:${newPassword}`);
  85. }}
  86. />
  87. )}
  88. <Button
  89. color='green'
  90. fluid
  91. size='large'
  92. onClick={handleSubmit}
  93. loading={loading}
  94. disabled={disableButton}
  95. >
  96. {disableButton ? `密码重置完成` : '提交'}
  97. </Button>
  98. </Segment>
  99. </Form>
  100. </Grid.Column>
  101. </Grid>
  102. );
  103. };
  104. export default PasswordResetConfirm;