import { ChangeEvent, FormEvent, useEffect, useState } from "react"; import toast from "react-hot-toast"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import BASE_URL from "@/app/config"; import { useSendLoginMagicLinkMutation } from "@/app/services/auth"; import { useAppSelector } from "@/app/store"; import Divider from "@/components/Divider"; import Button from "@/components/styled/Button"; import Input from "@/components/styled/Input"; import SocialLoginButtons from "../login/SocialLoginButtons"; import SignInLink from "../reg/SignInLink"; import SentTip from "./SentTip"; export default function SendMagicLinkPage() { const { t } = useTranslation("auth"); const serverName = useAppSelector((store) => store.server.name); const [sent, setSent] = useState(false); const [sendMagicLink, { isSuccess, isLoading, error }] = useSendLoginMagicLinkMutation(); const navigateTo = useNavigate(); const [email, setEmail] = useState(""); useEffect(() => { if (isSuccess) { toast.success("Send Email Successfully!"); setSent(true); } }, [isSuccess]); useEffect(() => { if (error && "status" in error) { switch (error.status) { case "PARSING_ERROR": toast.error(error.data); break; case 401: toast.error("Username or Password Incorrect"); break; case 404: toast.error("Account not exist"); break; default: toast.error("Something Error"); break; } return; } }, [error]); const handlePwdPath = () => { navigateTo("/login"); }; const handleLogin = (evt: FormEvent) => { evt.preventDefault(); sendMagicLink(email); }; const handleInput = (evt: ChangeEvent) => { const { value } = evt.target; setEmail(value); }; const handleReset = () => { setEmail(""); setSent(false); }; return (
{sent ? ( ) : ( <>
logo

{t("enter")} {serverName}{" "}

{t("placeholder_email")}
)}
); }