This commit is contained in:
jinjianming 2024-09-23 08:48:47 +08:00 committed by GitHub
commit 2849908c91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,13 +3,13 @@ import { useSelector } from 'react-redux';
import useRegister from 'hooks/useRegister'; import useRegister from 'hooks/useRegister';
import Turnstile from 'react-turnstile'; import Turnstile from 'react-turnstile';
import { useSearchParams } from 'react-router-dom'; import { useSearchParams } from 'react-router-dom';
// import { useSelector } from 'react-redux';
// material-ui // material-ui
import { useTheme } from '@mui/material/styles'; import { useTheme } from '@mui/material/styles';
import { import {
Box, Box,
Button, Button,
CircularProgress,
FormControl, FormControl,
FormHelperText, FormHelperText,
Grid, Grid,
@ -50,6 +50,9 @@ const RegisterForm = ({ ...others }) => {
const [strength, setStrength] = useState(0); const [strength, setStrength] = useState(0);
const [level, setLevel] = useState(); const [level, setLevel] = useState();
const [timer, setTimer] = useState(0);
const [loading, setLoading] = useState(false);
const handleClickShowPassword = () => { const handleClickShowPassword = () => {
setShowPassword(!showPassword); setShowPassword(!showPassword);
}; };
@ -74,11 +77,17 @@ const RegisterForm = ({ ...others }) => {
return; return;
} }
setLoading(true); // Start loading
const { success, message } = await sendVerificationCode(email, turnstileToken); const { success, message } = await sendVerificationCode(email, turnstileToken);
setLoading(false); // Stop loading
if (!success) { if (!success) {
showError(message); showError(message);
return; return;
} }
setTimer(60); // Start the 60-second timer
}; };
useEffect(() => { useEffect(() => {
@ -94,6 +103,17 @@ const RegisterForm = ({ ...others }) => {
} }
}, [siteInfo]); }, [siteInfo]);
useEffect(() => {
let interval;
if (timer > 0) {
interval = setInterval(() => {
setTimer((prevTimer) => prevTimer - 1);
}, 1000);
}
return () => clearInterval(interval);
}, [timer]);
return ( return (
<> <>
<Formik <Formik
@ -240,8 +260,13 @@ const RegisterForm = ({ ...others }) => {
onChange={handleChange} onChange={handleChange}
endAdornment={ endAdornment={
<InputAdornment position="end"> <InputAdornment position="end">
<Button variant="contained" color="primary" onClick={() => handleSendCode(values.email)}> <Button
发送验证码 variant="contained"
color="primary"
onClick={() => handleSendCode(values.email)}
disabled={timer > 0 || loading}
>
{loading ? <CircularProgress size={24} /> : timer > 0 ? `${timer}s` : '发送验证码'}
</Button> </Button>
</InputAdornment> </InputAdornment>
} }