import React, { useState, useEffect } from 'react'; import { createAccount, checkCredentials, getSettings } from '../backend/database'; import Settings from './settings/Settings'; // Import the Settings component import { importDatabase } from './settings/settingUtils'; const Login: React.FC = () => { // State to handle popup visibility const [showLoginPopup, setShowLoginPopup] = useState(false); const [showSignUpPopup, setShowSignUpPopup] = useState(false); const [isLoggedIn, setIsLoggedIn] = useState(false); const [showSettingsPopup, setShowSettingsPopup] = useState(false); // Credentials state const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [accountName, setAccountName] = useState(''); const [newAccountEmail, setNewAccountEmail] = useState(''); const [newAccountPassword, setNewAccountPassword] = useState(''); const [newAccountName, setNewAccountName] = useState(''); // On component mount, check if there are credentials in localStorage useEffect(() => { let savedAccountName: string | null; let savedAccountEmail: string | null; let savedAccountPassword: string | null; if (typeof localStorage !== 'undefined') { savedAccountName = localStorage.getItem('accountName'); savedAccountEmail = localStorage.getItem('accountEmail'); savedAccountPassword = localStorage.getItem('accountPassword'); // If credentials are found in localStorage, log the user in if (savedAccountName && savedAccountEmail && savedAccountPassword) { setAccountName(savedAccountName); setEmail(savedAccountEmail); setPassword(savedAccountPassword); const check = async () => { if (savedAccountName !== null && savedAccountPassword !== null) { const success = await checkCredentials(savedAccountName, savedAccountPassword); setIsLoggedIn(success); // Automatically log in const useName = localStorage.getItem("accountName"); const usePassword = localStorage.getItem("accountPassword"); if (useName && usePassword) { await importDatabase(useName, usePassword); } } }; check(); } } }, []); // Function to toggle the login popup const toggleLoginPopup = () => setShowLoginPopup(!showLoginPopup); // Function to toggle the sign-up popup const toggleSignUpPopup = () => { setShowSignUpPopup(!showSignUpPopup); setShowLoginPopup(false); }; // Function to handle login const handleLogin = async () => { if (accountName && password) { const success = await checkCredentials(accountName, password); if (success) { setIsLoggedIn(true); // Successful login const data = await getSettings(accountName, password) if (data) { if (typeof localStorage !== 'undefined') { localStorage.setItem("dataFromServer", data) } } setShowLoginPopup(false); // Close the login popup const useName = localStorage.getItem("accountName"); const usePassword = localStorage.getItem("accountPassword"); if (useName && usePassword) { await importDatabase(useName, usePassword); } window.location.reload(); } else { alert('Incorrect credentials'); } } else { alert('Incorrect credentials'); } }; // Function to handle account creation const handleCreateAccount = async () => { if (newAccountName !== "" && newAccountEmail !== "" && newAccountPassword !== "") { const success = await createAccount(newAccountName, newAccountEmail, newAccountPassword); if (success) { alert('Account created successfully! You can now log in.'); toggleSignUpPopup(); // Close sign-up popup } else { alert('Account creation failed. Please try again.'); } } else { alert('Account creation failed. Please do not leave any field empty.'); } }; // Function to toggle the settings popup const toggleSettingsPopup = () => setShowSettingsPopup(!showSettingsPopup); return (
{/* Login or Settings Button */} {/* Conditional rendering of the Login Popup */} {showLoginPopup && (

Log In

{/* Close Button */} {/* Name or Email Input */}
{ const input = e.target.value; setEmail(input); // Update both email and accountName states setAccountName(input); }} />
{/* Password Input */}
setPassword(e.target.value)} />
{/* Log In Button */}
{/* Text for creating an account */}

Don't have an account yet? Create one{' '} here

)} {/* Conditional rendering of the Sign-Up Popup */} {showSignUpPopup && (

Create Account

{/* New Account Email Input */}
setNewAccountEmail(e.target.value)} />
{/* New Account Name Input */}
setNewAccountName(e.target.value)} />
{/* New Account Password Input */}
setNewAccountPassword(e.target.value)} />
{/* Create Account Button */}
{/* Close Button */}
)} {/* Conditional rendering of the Settings Popup */} {showSettingsPopup && }
); }; export default Login;