import { useState, useEffect } from 'react'; import { View, Text, Pressable, StyleSheet, ActivityIndicator, Modal, FlatList, Alert, Platform, } from 'react-native'; import { useRouter, useLocalSearchParams } from 'expo-router'; import { useListsStore } from '@/store/lists'; import { useItemsStore } from '@/store/items'; import { api } from '@/lib/api'; import type { ListItem, Item } from '@shop/types'; /** * Preset list reset modal * Allows user to exclude items from preset reset */ export default function PresetResetScreen() { const router = useRouter(); const { id } = useLocalSearchParams<{ id: string }>(); const { lists, updateList } = useListsStore(); const { items } = useItemsStore(); const [isLoading, setIsLoading] = useState(false); const [selectedExclusions, setSelectedExclusions] = useState>(new Set()); const list = lists.find((l) => l.id === id); const listItemsWithDetails = (list?.items || []).map((li) => ({ ...li, item: items.find((i) => i.id === li.itemId), })); const toggleExclusion = (itemId: string) => { const newExclusions = new Set(selectedExclusions); if (newExclusions.has(itemId)) { newExclusions.delete(itemId); } else { newExclusions.add(itemId); } setSelectedExclusions(newExclusions); }; const handleReset = async () => { if (!list || !id) return; try { setIsLoading(true); const excludedIds = Array.from(selectedExclusions); // Call reset endpoint const response = await api.post(`/api/lists/${id}/reset`, { excludedItemIds: excludedIds, }); // Update local store if (response && typeof response === 'object') { updateList(response as any); } Alert.alert('Success', 'Preset list has been reset', [ { text: 'OK', onPress: () => router.back(), }, ]); } catch (err) { console.error('Failed to reset list:', err); Alert.alert('Error', 'Failed to reset list'); } finally { setIsLoading(false); } }; if (!list) { return ( List not found ); } return ( {/* Header */} router.back()}> ✕ Reset Preset {/* Instructions */} Select items to exclude from reset These items will NOT be added back to the list after reset {/* Items list */} item.id} renderItem={({ item: listItem }) => { const isExcluded = selectedExclusions.has(listItem.id); return ( toggleExclusion(listItem.id)} > {isExcluded && ✓} {listItem.item?.name || 'Unknown item'} ); }} ListEmptyComponent={No items in this list} contentContainerStyle={styles.itemsList} /> {/* Footer buttons */} router.back()}> Cancel {isLoading ? ( ) : ( Reset List )} ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', }, header: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingHorizontal: 16, paddingVertical: 12, marginTop: Platform.OS === 'ios' ? 44 : 12, borderBottomWidth: 1, borderBottomColor: '#e0e0e0', }, title: { fontSize: 18, fontWeight: '600', color: '#000', }, closeButton: { fontSize: 20, color: '#007AFF', }, instructions: { backgroundColor: '#f0f8ff', borderLeftWidth: 4, borderLeftColor: '#007AFF', paddingHorizontal: 12, paddingVertical: 12, marginHorizontal: 8, marginVertical: 12, borderRadius: 8, }, instructionsTitle: { fontSize: 14, fontWeight: '600', color: '#007AFF', marginBottom: 4, }, instructionsText: { fontSize: 12, color: '#666', }, itemsList: { paddingHorizontal: 8, paddingVertical: 8, }, itemRow: { flexDirection: 'row', alignItems: 'center', paddingVertical: 12, paddingHorizontal: 12, marginVertical: 4, backgroundColor: '#f9f9f9', borderRadius: 8, borderLeftWidth: 4, borderLeftColor: '#007AFF', }, itemRowExcluded: { backgroundColor: '#fff3cd', borderLeftColor: '#FFA500', }, checkbox: { width: 24, height: 24, borderRadius: 6, borderWidth: 2, borderColor: '#007AFF', justifyContent: 'center', alignItems: 'center', marginRight: 12, }, checkboxChecked: { backgroundColor: '#FFA500', borderColor: '#FFA500', }, checkmark: { color: '#fff', fontSize: 14, fontWeight: 'bold', }, itemInfo: { flex: 1, }, itemName: { fontSize: 16, fontWeight: '500', color: '#000', }, itemNameExcluded: { color: '#FFA500', fontWeight: '600', }, emptyText: { textAlign: 'center', color: '#999', fontSize: 14, marginVertical: 24, }, footer: { flexDirection: 'row', gap: 8, paddingHorizontal: 16, paddingVertical: 12, borderTopWidth: 1, borderTopColor: '#e0e0e0', }, cancelButton: { flex: 1, paddingVertical: 12, borderRadius: 8, borderWidth: 1, borderColor: '#ddd', justifyContent: 'center', alignItems: 'center', }, cancelButtonText: { fontSize: 16, fontWeight: '600', color: '#666', }, resetButton: { flex: 1, paddingVertical: 12, backgroundColor: '#007AFF', borderRadius: 8, justifyContent: 'center', alignItems: 'center', }, resetButtonText: { color: '#fff', fontSize: 16, fontWeight: '600', }, buttonDisabled: { opacity: 0.6, }, });