This commit is contained in:
liangzai 2024-09-20 18:03:41 +08:00
parent 94f2fa94b7
commit c4de918726

View File

@ -9,6 +9,7 @@ import * as Form from "@radix-ui/react-form";
import useFetch from "@/hooks/useFetch"; import useFetch from "@/hooks/useFetch";
import { useTranslations } from "next-intl"; import { useTranslations } from "next-intl";
import Link from "next/link"; import Link from "next/link";
import { useRouter } from "next/navigation";
interface RegisterFormProps { interface RegisterFormProps {
back: () => void; back: () => void;
@ -30,8 +31,9 @@ export default function RegisterForm({ back }: RegisterFormProps) {
const [confirmPassword, setConfirmPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState("");
const [captcha, setCaptcha] = useState(""); const [captcha, setCaptcha] = useState("");
const [captchaTimer, setCaptchaTimer] = useState(0); const [captchaTimer, setCaptchaTimer] = useState(0);
const [captchaEmailTimer, setCaptchaEmailTimer] = useState(0);
const [agreed, setAgreed] = useState(true); // 默认勾选 const [agreed, setAgreed] = useState(true); // 默认勾选
const router = useRouter();
const { addToast } = useToast(); const { addToast } = useToast();
// 获取验证码 // 获取验证码
@ -44,7 +46,15 @@ export default function RegisterForm({ back }: RegisterFormProps) {
url: "/api/send-verification-sms/", url: "/api/send-verification-sms/",
method: "POST", method: "POST",
}); });
const {
fetchData: fetchVerificationEmailCode,
data: verificationEmailData,
loading: verificationEmailLoading,
error: verificationEmailError,
} = useFetch({
url: "/api/send-verification-email/",
method: "POST",
});
// 注册 // 注册
const { const {
fetchData: register, fetchData: register,
@ -55,7 +65,6 @@ export default function RegisterForm({ back }: RegisterFormProps) {
url: "/api/register/", url: "/api/register/",
method: "POST", method: "POST",
}); });
useEffect(() => { useEffect(() => {
let timer: NodeJS.Timeout; let timer: NodeJS.Timeout;
if (captchaTimer > 0) { if (captchaTimer > 0) {
@ -65,7 +74,15 @@ export default function RegisterForm({ back }: RegisterFormProps) {
} }
return () => clearInterval(timer); return () => clearInterval(timer);
}, [captchaTimer]); }, [captchaTimer]);
useEffect(() => {
let timer: NodeJS.Timeout;
if (captchaEmailTimer > 0) {
timer = setInterval(() => {
setCaptchaEmailTimer((prev) => prev - 1);
}, 1000);
}
return () => clearInterval(timer);
}, [captchaEmailTimer]);
const handleGetCaptcha = async () => { const handleGetCaptcha = async () => {
if (selected === "phone" && !isValidPhoneNumber(phone)) { if (selected === "phone" && !isValidPhoneNumber(phone)) {
addToast(t("invalidPhoneNumber"), "error"); addToast(t("invalidPhoneNumber"), "error");
@ -80,17 +97,32 @@ export default function RegisterForm({ back }: RegisterFormProps) {
const data = const data =
selected === "phone" ? { phone_number: phone } : { email: email }; selected === "phone" ? { phone_number: phone } : { email: email };
if (selected === "phone") {
await fetchVerificationCode(data); await fetchVerificationCode(data);
setCaptchaTimer(60);
};
useEffect(() => {
if (verificationData?.code === 200) {
addToast(t("captchaSent"), "success"); addToast(t("captchaSent"), "success");
setCaptchaTimer(60); setCaptchaTimer(60);
} else {
await fetchVerificationEmailCode(data);
addToast(t("captchaSent"), "success");
setCaptchaEmailTimer(60);
} }
}, [verificationData, verificationError]); };
// useEffect(() => {
// if (verificationData?.code === 200) {
// addToast(t("captchaSent"), "success");
// setCaptchaTimer(60);
// return;
// }
// }, [verificationData, verificationError, addToast, t]);
// useEffect(() => {
// console.log("verificationError", verificationError);
// if (!verificationEmailData && !verificationEmailError) {
// addToast(t("captchaSent"), "success");
// setCaptchaEmailTimer(60);
// return;
// }
// }, [verificationEmailData, verificationEmailError, addToast, t]);
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault(); // 阻止表单默认提交行为 event.preventDefault(); // 阻止表单默认提交行为
@ -134,6 +166,8 @@ export default function RegisterForm({ back }: RegisterFormProps) {
// 调用 register 函数发送注册请求 // 调用 register 函数发送注册请求
await register(registerData); await register(registerData);
addToast(t("registerSuccess"), "success");
router.replace("/create");
}; };
return ( return (
@ -254,6 +288,7 @@ export default function RegisterForm({ back }: RegisterFormProps) {
className="h-10 input border-gray-600 bg-gray-800 focus:outline-none w-full rounded-lg text-sm text-gray-300 placeholder-gray-500" className="h-10 input border-gray-600 bg-gray-800 focus:outline-none w-full rounded-lg text-sm text-gray-300 placeholder-gray-500"
placeholder={t("captcha")} placeholder={t("captcha")}
/> />
{selected === "phone" ? (
<button <button
type="button" type="button"
onClick={handleGetCaptcha} onClick={handleGetCaptcha}
@ -265,11 +300,34 @@ export default function RegisterForm({ back }: RegisterFormProps) {
{verificationLoading ? ( {verificationLoading ? (
<span className="loading loading-spinner loading-md text-gray-200"></span> <span className="loading loading-spinner loading-md text-gray-200"></span>
) : captchaTimer > 0 ? ( ) : captchaTimer > 0 ? (
`${captchaTimer}s` <span className="text-gray-200">
{captchaTimer}s
</span>
) : ( ) : (
<span>{t("getCode")}</span> <span>{t("getCode")}</span>
)} )}
</button> </button>
) : (
<button
type="button"
onClick={handleGetCaptcha}
className="btn h-10 btn-sm w-32 ml-2 bg-transparent hover:bg-green-500 hover:text-white text-gray-200 rounded-lg"
disabled={
captchaEmailTimer > 0 ||
verificationEmailLoading
}
>
{verificationEmailLoading ? (
<span className="loading loading-spinner loading-md text-gray-200"></span>
) : captchaEmailTimer > 0 ? (
<span className="text-gray-200">
{captchaEmailTimer}s
</span>
) : (
<span>{t("getCode")}</span>
)}
</button>
)}
</div> </div>
</Form.Field> </Form.Field>
@ -304,7 +362,7 @@ export default function RegisterForm({ back }: RegisterFormProps) {
> >
{t("submit")} {t("submit")}
{registerLoading ? ( {registerLoading ? (
<span className="loading loading-spinner loading-md ml-2"></span> <span className="loading loading-spinner loading-md ml-2 text-gray-200"></span>
) : null} ) : null}
</Form.Submit> </Form.Submit>
</div> </div>