import { useEffect, useState } from "preact/hooks"; import { hideLoading, showLoading } from "utils/ui.ts"; interface LoginFrameProps { mode: "login" | "register"; } export default function LoginFrame(props: LoginFrameProps) { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [emailError, setEmailError] = useState(false); const [passwordError, setPasswordError] = useState(false); const [confirmPasswordError, setConfirmPasswordError] = useState(false); const checkUserLogin = async () => { const resp = await fetch("/api/user/login"); const respJson = await resp.json(); if (respJson.success) { // Redirect to main page if valid location.href = "/"; return true; } return false; }; const doUserLogin = async () => { const resp = await fetch("/api/user/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password, }), }); const respJson = await resp.json(); if (respJson.success) { location.href = "/"; return true; } return false; }; const doUserRegister = async () => { const resp = await fetch("/api/user/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password, }), }); const respJson = await resp.json(); if (respJson.success) { location.href = "/login"; return true; } return false; }; useEffect(() => { props.mode === "login" && checkUserLogin(); }, []); const onSubmit = async () => { showLoading(); // Do request if (email && password && props.mode === "login") { await doUserLogin(); } else if ( email && password && confirmPassword && props.mode === "register" ) { if (password !== confirmPassword) { setConfirmPasswordError(true); } else { await doUserRegister(); } } // Set error if (!email) { setEmailError(true); } if (!password) { setPasswordError(true); } if (!confirmPassword && props.mode === "register") { setConfirmPasswordError(true); } hideLoading(); }; return (