291 lines
7.1 KiB
TypeScript
291 lines
7.1 KiB
TypeScript
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<Set<string>>(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<any>(`/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 (
|
|
<View style={styles.container}>
|
|
<Text>List not found</Text>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<View style={styles.container}>
|
|
{/* Header */}
|
|
<View style={styles.header}>
|
|
<Pressable onPress={() => router.back()}>
|
|
<Text style={styles.closeButton}>✕</Text>
|
|
</Pressable>
|
|
<Text style={styles.title}>Reset Preset</Text>
|
|
<View style={{ width: 32 }} />
|
|
</View>
|
|
|
|
{/* Instructions */}
|
|
<View style={styles.instructions}>
|
|
<Text style={styles.instructionsTitle}>Select items to exclude from reset</Text>
|
|
<Text style={styles.instructionsText}>
|
|
These items will NOT be added back to the list after reset
|
|
</Text>
|
|
</View>
|
|
|
|
{/* Items list */}
|
|
<FlatList
|
|
data={listItemsWithDetails}
|
|
keyExtractor={(item) => item.id}
|
|
renderItem={({ item: listItem }) => {
|
|
const isExcluded = selectedExclusions.has(listItem.id);
|
|
return (
|
|
<Pressable
|
|
style={[styles.itemRow, isExcluded && styles.itemRowExcluded]}
|
|
onPress={() => toggleExclusion(listItem.id)}
|
|
>
|
|
<View style={[styles.checkbox, isExcluded && styles.checkboxChecked]}>
|
|
{isExcluded && <Text style={styles.checkmark}>✓</Text>}
|
|
</View>
|
|
<View style={styles.itemInfo}>
|
|
<Text style={[styles.itemName, isExcluded && styles.itemNameExcluded]}>
|
|
{listItem.item?.name || 'Unknown item'}
|
|
</Text>
|
|
</View>
|
|
</Pressable>
|
|
);
|
|
}}
|
|
ListEmptyComponent={<Text style={styles.emptyText}>No items in this list</Text>}
|
|
contentContainerStyle={styles.itemsList}
|
|
/>
|
|
|
|
{/* Footer buttons */}
|
|
<View style={styles.footer}>
|
|
<Pressable style={styles.cancelButton} onPress={() => router.back()}>
|
|
<Text style={styles.cancelButtonText}>Cancel</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
style={[styles.resetButton, isLoading && styles.buttonDisabled]}
|
|
onPress={handleReset}
|
|
disabled={isLoading}
|
|
>
|
|
{isLoading ? (
|
|
<ActivityIndicator size="small" color="#fff" />
|
|
) : (
|
|
<Text style={styles.resetButtonText}>Reset List</Text>
|
|
)}
|
|
</Pressable>
|
|
</View>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
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,
|
|
},
|
|
});
|