import React, { useState } from 'react'; import { Association, Balance } from '../types'; import { api } from '../api'; interface LoginScreenProps { onLogin: (association: Association) => void; } const LoginScreen: React.FC = ({ onLogin }) => { const [isLoginView, setIsLoginView] = useState(true); const [associationName, setAssociationName] = useState(''); const [password, setPassword] = useState(''); const [initialBalances, setInitialBalances] = useState<{ name: string; amount: string }[]>([{ name: '', amount: '' }]); const [error, setError] = useState(''); const handleAddBalance = () => { setInitialBalances([...initialBalances, { name: '', amount: '' }]); }; const handleRemoveBalance = (index: number) => { setInitialBalances(initialBalances.filter((_, i) => i !== index)); }; const handleBalanceChange = (index: number, field: 'name' | 'amount', value: string) => { const newBalances = [...initialBalances]; newBalances[index][field] = value; setInitialBalances(newBalances); }; const validateSignup = () => { if (!associationName.trim() || !password.trim()) { setError('Association name and password are required.'); return false; } if (initialBalances.some(b => !b.name.trim() || !b.amount.trim() || isNaN(parseFloat(b.amount)))) { setError('All balance fields must be filled correctly.'); return false; } return true; }; const handleSignup = async () => { setError(''); if (!validateSignup()) return; try { const newAssociation = await api.signup(associationName.trim(), password, initialBalances.map(b => ({ name: b.name.trim(), amount: b.amount }))); onLogin(newAssociation); } catch (err: any) { setError(err.message || 'Signup failed'); } }; const handleLogin = async () => { setError(''); if (!associationName.trim() || !password.trim()) { setError('Please enter association name and password.'); return; } try { const association = await api.login(associationName.trim(), password); onLogin(association); } catch (err: any) { setError(err.message || 'Login failed'); } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (isLoginView) { handleLogin(); } else { handleSignup(); } } return (

Abacus

Simplified accounting for your association.

{isLoginView ? 'Login' : 'Create Account'}

setAssociationName(e.target.value)} className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-gray-800 transition" /> setPassword(e.target.value)} className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-gray-800 transition" /> {!isLoginView && (

Initial Balances

{initialBalances.map((balance, index) => (
handleBalanceChange(index, 'name', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" /> handleBalanceChange(index, 'amount', e.target.value)} className="w-40 px-3 py-2 border border-gray-300 rounded-lg text-sm" /> {initialBalances.length > 1 && ( )}
))}
)} {error &&

{error}

}

{isLoginView ? "Don't have an account?" : 'Already have an account?'}

); }; export default LoginScreen;