import { fetchApi } from "@/lib/server-api-util"; import { User } from "@/types/user"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { ActivityIndicator, TextInput, TouchableOpacity, View } from "react-native"; import { ThemedText } from "../ThemedText"; interface LoginProps { setIsSignUp?: (isSignUp: string) => void; closeModal?: () => void; updateUrlParam?: (status: string, value: string) => void; setError: (error: string) => void; } const ForgetPwd = ({ setIsSignUp, updateUrlParam, setError }: LoginProps) => { const { t } = useTranslation(); const [loading, setLocading] = useState(false); // 发送邮箱后把按钮变为disabled const [isDisabled, setIsDisabled] = useState(false); const [email, setEmail] = useState(''); const [countdown, setCountdown] = useState(0); // 倒计时效果 useEffect(() => { if (countdown > 0) { const timer = setTimeout(() => setCountdown(countdown - 1), 1000); return () => clearTimeout(timer); } else if (countdown === 0 && isDisabled) { setIsDisabled(false); } }, [countdown, isDisabled]); // 发送邮件 const handleSubmit = () => { if (!email) { setError(t('auth.forgetPwd.emailPlaceholder', { ns: 'login' })); return; } setLocading(true); const body = { email: email, } // 调接口确定邮箱是否正确,是否有该用户邮箱权限 fetchApi('/iam/reset-password-session', { method: 'POST', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' } }) .then((_) => { // console.log("Password reset email sent successfully"); setIsDisabled(true); setCountdown(60); // 开始60秒倒计时 }) .catch((error) => { // console.error('Failed to send reset email:', error); setError(t('auth.forgetPwd.sendEmailError', { ns: 'login' })); }) .finally(() => { setLocading(false); }); }; // 返回登陆 const handleBackToLogin = () => { if (setIsSignUp) { setIsSignUp('login'); } if (updateUrlParam) { updateUrlParam('status', 'login'); } } return {/* 邮箱输入框 */} {t('auth.forgetPwd.title', { ns: 'login' })} {/* 发送重置密码邮件 */} {loading ? ( ) : ( {isDisabled ? `${t("auth.forgetPwd.sendEmailBtnDisabled", { ns: "login" })} (${countdown}s)` : t("auth.forgetPwd.sendEmailBtn", { ns: "login" })} )} {/* 返回登陆 */} {t('auth.forgetPwd.goback', { ns: 'login' })} } export default ForgetPwd