261 lines
6.8 KiB
TypeScript
261 lines
6.8 KiB
TypeScript
import { useEffect, useState } from 'react';
|
||
import { View, Text, FlatList, Pressable, StyleSheet, ActivityIndicator } from 'react-native';
|
||
import { useRouter } from 'expo-router';
|
||
import { useAuthStore } from '@/store/auth';
|
||
import { useListsStore } from '@/store/lists';
|
||
import { api } from '@/lib/api';
|
||
import type { ShoppingList } from '@shop/types';
|
||
|
||
/**
|
||
* Home screen
|
||
* Displays shopping lists and forgotten items
|
||
*/
|
||
export default function HomeScreen() {
|
||
const router = useRouter();
|
||
const { user, logout } = useAuthStore();
|
||
const { lists, setLists, setActiveList } = useListsStore();
|
||
const [isLoading, setIsLoading] = useState(true);
|
||
const [forgottenItems, setForgottenItems] = useState<Array<{ id: string; name: string }>>([]);
|
||
|
||
useEffect(() => {
|
||
loadData();
|
||
}, []);
|
||
|
||
const loadData = async () => {
|
||
try {
|
||
setIsLoading(true);
|
||
// Load shopping lists
|
||
const listsData = await api.get<ShoppingList[]>('/api/lists');
|
||
setLists(listsData);
|
||
|
||
// Load forgotten items
|
||
const forgotten = await api.get<Array<{ id: string; name: string }>>('/api/items/forgotten');
|
||
setForgottenItems(forgotten || []);
|
||
} catch (err) {
|
||
console.error('Failed to load data:', err);
|
||
} finally {
|
||
setIsLoading(false);
|
||
}
|
||
};
|
||
|
||
const handleOpenList = (list: ShoppingList) => {
|
||
setActiveList(list.id);
|
||
router.push(`/(app)/list/${list.id}`);
|
||
};
|
||
|
||
const handleLogout = () => {
|
||
logout();
|
||
};
|
||
|
||
if (isLoading) {
|
||
return (
|
||
<View style={styles.container}>
|
||
<ActivityIndicator size="large" color="#007AFF" />
|
||
</View>
|
||
);
|
||
}
|
||
|
||
const presetLists = lists.filter((l) => l.isPreset);
|
||
const regularLists = lists.filter((l) => !l.isPreset);
|
||
|
||
return (
|
||
<View style={styles.container}>
|
||
<View style={styles.header}>
|
||
<Text style={styles.title}>Bagsy</Text>
|
||
<Pressable style={styles.settingsButton} onPress={handleLogout}>
|
||
<Text style={styles.settingsButtonText}>⚙️</Text>
|
||
</Pressable>
|
||
</View>
|
||
|
||
<FlatList
|
||
data={[
|
||
{ type: 'welcome' },
|
||
{ type: 'preset-header' },
|
||
...presetLists.map((l) => ({ type: 'preset', item: l })),
|
||
{ type: 'lists-header' },
|
||
...regularLists.map((l) => ({ type: 'list', item: l })),
|
||
{ type: 'forgotten-header' },
|
||
...forgottenItems.map((i) => ({ type: 'forgotten', item: i })),
|
||
]}
|
||
renderItem={({ item }: any) => {
|
||
switch (item.type) {
|
||
case 'welcome':
|
||
return (
|
||
<View style={styles.welcomeCard}>
|
||
<Text style={styles.welcomeText}>Welcome, {user?.name || 'User'}!</Text>
|
||
</View>
|
||
);
|
||
case 'preset-header':
|
||
return presetLists.length > 0 ? (
|
||
<Text style={styles.sectionHeader}>Preset Lists</Text>
|
||
) : null;
|
||
case 'preset':
|
||
case 'list':
|
||
return (
|
||
<Pressable style={styles.listCard} onPress={() => handleOpenList(item.item)}>
|
||
<Text style={styles.listName}>{item.item.name}</Text>
|
||
<Text style={styles.listMeta}>{item.item.items.length} items</Text>
|
||
</Pressable>
|
||
);
|
||
case 'lists-header':
|
||
return regularLists.length > 0 ? (
|
||
<Text style={styles.sectionHeader}>Shopping Lists</Text>
|
||
) : null;
|
||
case 'forgotten-header':
|
||
return forgottenItems.length > 0 ? (
|
||
<Text style={styles.sectionHeader}>Don't Forget</Text>
|
||
) : null;
|
||
case 'forgotten':
|
||
return (
|
||
<Pressable style={styles.forgottenCard}>
|
||
<Text style={styles.forgottenName}>{item.item.name}</Text>
|
||
<Text style={styles.forgottenAdd}>Tap to add to list</Text>
|
||
</Pressable>
|
||
);
|
||
default:
|
||
return null;
|
||
}
|
||
}}
|
||
keyExtractor={(item, index) => `${item.type}-${index}`}
|
||
ListEmptyComponent={
|
||
<View style={styles.emptyContainer}>
|
||
<Text style={styles.emptyText}>No lists yet</Text>
|
||
<Text style={styles.emptySubtext}>Create your first shopping list</Text>
|
||
</View>
|
||
}
|
||
contentContainerStyle={styles.listContent}
|
||
/>
|
||
|
||
<Pressable style={styles.fab} onPress={() => router.push('/(app)/list/new')}>
|
||
<Text style={styles.fabText}>+</Text>
|
||
</Pressable>
|
||
</View>
|
||
);
|
||
}
|
||
|
||
const styles = StyleSheet.create({
|
||
container: {
|
||
flex: 1,
|
||
backgroundColor: '#fff',
|
||
},
|
||
header: {
|
||
flexDirection: 'row',
|
||
justifyContent: 'space-between',
|
||
alignItems: 'center',
|
||
paddingHorizontal: 20,
|
||
paddingVertical: 16,
|
||
borderBottomWidth: 1,
|
||
borderBottomColor: '#f0f0f0',
|
||
marginTop: 40,
|
||
},
|
||
title: {
|
||
fontSize: 28,
|
||
fontWeight: 'bold',
|
||
},
|
||
settingsButton: {
|
||
fontSize: 24,
|
||
padding: 8,
|
||
},
|
||
settingsButtonText: {
|
||
fontSize: 24,
|
||
},
|
||
listContent: {
|
||
paddingHorizontal: 12,
|
||
paddingVertical: 12,
|
||
},
|
||
welcomeCard: {
|
||
backgroundColor: '#f0f8ff',
|
||
padding: 16,
|
||
borderRadius: 12,
|
||
marginBottom: 16,
|
||
marginHorizontal: 8,
|
||
},
|
||
welcomeText: {
|
||
fontSize: 18,
|
||
fontWeight: '600',
|
||
color: '#007AFF',
|
||
},
|
||
sectionHeader: {
|
||
fontSize: 16,
|
||
fontWeight: '700',
|
||
color: '#333',
|
||
marginTop: 16,
|
||
marginBottom: 8,
|
||
marginHorizontal: 8,
|
||
},
|
||
listCard: {
|
||
backgroundColor: '#fff',
|
||
borderWidth: 1,
|
||
borderColor: '#eee',
|
||
borderRadius: 12,
|
||
padding: 16,
|
||
marginBottom: 8,
|
||
marginHorizontal: 8,
|
||
},
|
||
listName: {
|
||
fontSize: 16,
|
||
fontWeight: '600',
|
||
color: '#333',
|
||
marginBottom: 4,
|
||
},
|
||
listMeta: {
|
||
fontSize: 13,
|
||
color: '#999',
|
||
},
|
||
forgottenCard: {
|
||
backgroundColor: '#fff8e1',
|
||
borderLeftWidth: 4,
|
||
borderLeftColor: '#ffc107',
|
||
padding: 12,
|
||
marginBottom: 8,
|
||
marginHorizontal: 8,
|
||
borderRadius: 8,
|
||
},
|
||
forgottenName: {
|
||
fontSize: 15,
|
||
fontWeight: '600',
|
||
color: '#333',
|
||
marginBottom: 2,
|
||
},
|
||
forgottenAdd: {
|
||
fontSize: 12,
|
||
color: '#999',
|
||
},
|
||
fab: {
|
||
position: 'absolute',
|
||
bottom: 20,
|
||
right: 20,
|
||
width: 56,
|
||
height: 56,
|
||
borderRadius: 28,
|
||
backgroundColor: '#007AFF',
|
||
justifyContent: 'center',
|
||
alignItems: 'center',
|
||
elevation: 5,
|
||
shadowColor: '#000',
|
||
shadowOffset: { width: 0, height: 2 },
|
||
shadowOpacity: 0.3,
|
||
shadowRadius: 4,
|
||
},
|
||
fabText: {
|
||
fontSize: 32,
|
||
color: '#fff',
|
||
fontWeight: '300',
|
||
},
|
||
emptyContainer: {
|
||
flex: 1,
|
||
justifyContent: 'center',
|
||
alignItems: 'center',
|
||
paddingTop: 60,
|
||
},
|
||
emptyText: {
|
||
fontSize: 18,
|
||
fontWeight: '600',
|
||
color: '#999',
|
||
marginBottom: 4,
|
||
},
|
||
emptySubtext: {
|
||
fontSize: 14,
|
||
color: '#ccc',
|
||
},
|
||
});
|