import { useState } from 'react'; import { View, Text, TextInput, Pressable, StyleSheet, ActivityIndicator, ScrollView, } from 'react-native'; import { useRouter } from 'expo-router'; import { useAuthStore } from '@/store/auth'; import { api } from '@/lib/api'; /** * Household setup screen * Allows user to create a new household or join existing by code */ export default function HouseholdScreen() { const router = useRouter(); const { user } = useAuthStore(); const [mode, setMode] = useState<'create' | 'join'>('create'); const [householdName, setHouseholdName] = useState(''); const [joinCode, setJoinCode] = useState(''); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const handleCreateHousehold = async () => { if (!householdName.trim()) { setError('Household name is required'); return; } try { setIsLoading(true); setError(''); await api.post('/api/households', { name: householdName, }); // After creating household, go to app router.replace('/(app)'); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to create household'; setError(message); console.error('Create household error:', err); } finally { setIsLoading(false); } }; const handleJoinHousehold = async () => { if (!joinCode.trim()) { setError('Join code is required'); return; } try { setIsLoading(true); setError(''); await api.post('/api/households/join', { code: joinCode.toUpperCase(), }); // After joining household, go to app router.replace('/(app)'); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to join household'; setError(message); console.error('Join household error:', err); } finally { setIsLoading(false); } }; return ( Set Up Your Household {user?.email} { setMode('create'); setError(''); }} disabled={isLoading} > Create { setMode('join'); setError(''); }} disabled={isLoading} > Join {error && {error}} {mode === 'create' ? ( <> Household Name {isLoading ? ( ) : ( Create Household )} You'll be able to invite family members once your household is created. ) : ( <> Join Code setJoinCode(text.toUpperCase())} editable={!isLoading} autoCapitalize="characters" maxLength={6} /> {isLoading ? ( ) : ( Join Household )} Ask the household admin for the 6-character join code. )} ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', }, content: { padding: 20, paddingVertical: 40, }, title: { fontSize: 28, fontWeight: 'bold', marginBottom: 8, }, subtitle: { fontSize: 14, color: '#666', marginBottom: 24, }, tabContainer: { flexDirection: 'row', marginBottom: 24, borderBottomWidth: 1, borderBottomColor: '#eee', }, tab: { flex: 1, paddingVertical: 12, alignItems: 'center', borderBottomWidth: 3, borderBottomColor: 'transparent', }, tabActive: { borderBottomColor: '#007AFF', }, tabText: { fontSize: 16, fontWeight: '600', color: '#999', }, tabTextActive: { color: '#007AFF', }, label: { fontSize: 14, fontWeight: '600', marginBottom: 8, color: '#333', }, input: { borderWidth: 1, borderColor: '#ddd', borderRadius: 8, padding: 12, marginBottom: 16, fontSize: 16, }, codeInput: { textAlign: 'center', fontSize: 24, letterSpacing: 2, fontWeight: 'bold', }, button: { paddingVertical: 12, paddingHorizontal: 24, borderRadius: 8, alignItems: 'center', justifyContent: 'center', marginVertical: 8, }, primaryButton: { backgroundColor: '#007AFF', }, buttonText: { color: '#fff', fontSize: 16, fontWeight: '600', }, infoBox: { backgroundColor: '#f0f8ff', padding: 16, borderRadius: 8, marginTop: 24, }, infoText: { fontSize: 14, color: '#333', lineHeight: 20, }, error: { color: '#ff3333', fontSize: 14, marginBottom: 12, textAlign: 'center', padding: 12, backgroundColor: '#ffe5e5', borderRadius: 6, }, });