Files
new-api/web/src/components/PasswordResetConfirm.js

114 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-04-22 20:39:27 +08:00
import React, { useEffect, useState } from 'react';
import { Button, Form, Grid, Header, Image, Segment } from 'semantic-ui-react';
2024-03-15 16:05:33 +08:00
import { API, copy, showError, showNotice } from '../helpers';
2023-04-22 20:39:27 +08:00
import { useSearchParams } from 'react-router-dom';
const PasswordResetConfirm = () => {
const [inputs, setInputs] = useState({
email: '',
2024-03-23 21:24:39 +08:00
token: '',
2023-04-22 20:39:27 +08:00
});
const { email, token } = inputs;
const [loading, setLoading] = useState(false);
const [disableButton, setDisableButton] = useState(false);
const [countdown, setCountdown] = useState(30);
const [newPassword, setNewPassword] = useState('');
2023-04-22 20:39:27 +08:00
const [searchParams, setSearchParams] = useSearchParams();
useEffect(() => {
let token = searchParams.get('token');
let email = searchParams.get('email');
setInputs({
token,
2024-03-23 21:24:39 +08:00
email,
2023-04-22 20:39:27 +08:00
});
}, []);
useEffect(() => {
let countdownInterval = null;
if (disableButton && countdown > 0) {
countdownInterval = setInterval(() => {
setCountdown(countdown - 1);
}, 1000);
} else if (countdown === 0) {
setDisableButton(false);
setCountdown(30);
}
2024-03-15 16:05:33 +08:00
return () => clearInterval(countdownInterval);
}, [disableButton, countdown]);
2023-04-22 20:39:27 +08:00
async function handleSubmit(e) {
setDisableButton(true);
2023-04-22 20:39:27 +08:00
if (!email) return;
setLoading(true);
const res = await API.post(`/api/user/reset`, {
email,
2024-03-23 21:24:39 +08:00
token,
2023-04-22 20:39:27 +08:00
});
const { success, message } = res.data;
if (success) {
let password = res.data.data;
setNewPassword(password);
2023-04-22 20:39:27 +08:00
await copy(password);
showNotice(`新密码已复制到剪贴板:${password}`);
2023-04-22 20:39:27 +08:00
} else {
showError(message);
}
setLoading(false);
}
2024-03-15 16:05:33 +08:00
2023-04-22 20:39:27 +08:00
return (
2024-03-23 21:24:39 +08:00
<Grid textAlign='center' style={{ marginTop: '48px' }}>
2023-04-22 20:39:27 +08:00
<Grid.Column style={{ maxWidth: 450 }}>
2024-03-23 21:24:39 +08:00
<Header as='h2' color='' textAlign='center'>
<Image src='/logo.png' /> 密码重置确认
2023-04-22 20:39:27 +08:00
</Header>
2024-03-23 21:24:39 +08:00
<Form size='large'>
2023-04-22 20:39:27 +08:00
<Segment>
<Form.Input
fluid
2024-03-23 21:24:39 +08:00
icon='mail'
iconPosition='left'
placeholder='邮箱地址'
name='email'
2023-04-22 20:39:27 +08:00
value={email}
readOnly
/>
{newPassword && (
<Form.Input
2024-03-15 16:05:33 +08:00
fluid
2024-03-23 21:24:39 +08:00
icon='lock'
iconPosition='left'
placeholder='新密码'
name='newPassword'
2024-03-15 16:05:33 +08:00
value={newPassword}
readOnly
onClick={(e) => {
e.target.select();
navigator.clipboard.writeText(newPassword);
showNotice(`密码已复制到剪贴板:${newPassword}`);
}}
/>
)}
2023-04-22 20:39:27 +08:00
<Button
2024-03-23 21:24:39 +08:00
color='green'
2023-04-22 20:39:27 +08:00
fluid
2024-03-23 21:24:39 +08:00
size='large'
2023-04-22 20:39:27 +08:00
onClick={handleSubmit}
loading={loading}
disabled={disableButton}
2023-04-22 20:39:27 +08:00
>
{disableButton ? `密码重置完成` : '提交'}
2023-04-22 20:39:27 +08:00
</Button>
</Segment>
</Form>
</Grid.Column>
</Grid>
2024-03-15 16:05:33 +08:00
);
2023-04-22 20:39:27 +08:00
};
export default PasswordResetConfirm;