bagsy/apps/mobile/app/(auth)/household.tsx

272 lines
6.7 KiB
TypeScript

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 (
<ScrollView style={styles.container}>
<View style={styles.content}>
<Text style={styles.title}>Set Up Your Household</Text>
<Text style={styles.subtitle}>{user?.email}</Text>
<View style={styles.tabContainer}>
<Pressable
style={[styles.tab, mode === 'create' && styles.tabActive]}
onPress={() => {
setMode('create');
setError('');
}}
disabled={isLoading}
>
<Text style={[styles.tabText, mode === 'create' && styles.tabTextActive]}>Create</Text>
</Pressable>
<Pressable
style={[styles.tab, mode === 'join' && styles.tabActive]}
onPress={() => {
setMode('join');
setError('');
}}
disabled={isLoading}
>
<Text style={[styles.tabText, mode === 'join' && styles.tabTextActive]}>Join</Text>
</Pressable>
</View>
{error && <Text style={styles.error}>{error}</Text>}
{mode === 'create' ? (
<>
<Text style={styles.label}>Household Name</Text>
<TextInput
style={styles.input}
placeholder="e.g. Smith Family"
value={householdName}
onChangeText={setHouseholdName}
editable={!isLoading}
autoCapitalize="words"
/>
<Pressable
style={[styles.button, styles.primaryButton]}
onPress={handleCreateHousehold}
disabled={isLoading}
>
{isLoading ? (
<ActivityIndicator color="#fff" />
) : (
<Text style={styles.buttonText}>Create Household</Text>
)}
</Pressable>
<View style={styles.infoBox}>
<Text style={styles.infoText}>
You'll be able to invite family members once your household is created.
</Text>
</View>
</>
) : (
<>
<Text style={styles.label}>Join Code</Text>
<TextInput
style={[styles.input, styles.codeInput]}
placeholder="Enter 6-char code"
value={joinCode}
onChangeText={(text) => setJoinCode(text.toUpperCase())}
editable={!isLoading}
autoCapitalize="characters"
maxLength={6}
/>
<Pressable
style={[styles.button, styles.primaryButton]}
onPress={handleJoinHousehold}
disabled={isLoading}
>
{isLoading ? (
<ActivityIndicator color="#fff" />
) : (
<Text style={styles.buttonText}>Join Household</Text>
)}
</Pressable>
<View style={styles.infoBox}>
<Text style={styles.infoText}>
Ask the household admin for the 6-character join code.
</Text>
</View>
</>
)}
</View>
</ScrollView>
);
}
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,
},
});