513 lines
14 KiB
TypeScript
513 lines
14 KiB
TypeScript
import { useState } from 'react';
|
||
import {
|
||
View,
|
||
Text,
|
||
TextInput,
|
||
Pressable,
|
||
StyleSheet,
|
||
ScrollView,
|
||
ActivityIndicator,
|
||
FlatList,
|
||
Modal,
|
||
Platform,
|
||
} from 'react-native';
|
||
import { useRouter, useLocalSearchParams } from 'expo-router';
|
||
import { useListsStore } from '@/store/lists';
|
||
import { useItemsStore } from '@/store/items';
|
||
import { useWebSocketActions } from '@/hooks/useWebSocket';
|
||
import { api } from '@/lib/api';
|
||
import type { Item, ListItem } from '@shop/types';
|
||
|
||
/**
|
||
* Add item sheet screen
|
||
* Allows users to: search items, scan barcode, or manually add item
|
||
*/
|
||
export default function AddItemScreen() {
|
||
const router = useRouter();
|
||
const { id } = useLocalSearchParams<{ id: string }>();
|
||
const { lists, updateList } = useListsStore();
|
||
const { items, setItems } = useItemsStore();
|
||
const { notifyItemAdded } = useWebSocketActions();
|
||
|
||
const [tab, setTab] = useState<'search' | 'barcode' | 'manual'>('search');
|
||
const [searchQuery, setSearchQuery] = useState('');
|
||
const [searchResults, setSearchResults] = useState<Item[]>([]);
|
||
const [isSearching, setIsSearching] = useState(false);
|
||
const [barcode, setBarcode] = useState('');
|
||
const [manualName, setManualName] = useState('');
|
||
const [manualQuantity, setManualQuantity] = useState('1');
|
||
const [selectedAisleId, setSelectedAisleId] = useState<string | undefined>();
|
||
const [isAdding, setIsAdding] = useState(false);
|
||
|
||
const list = lists.find((l) => l.id === id);
|
||
|
||
const handleSearch = async (query: string) => {
|
||
setSearchQuery(query);
|
||
if (query.trim().length < 2) {
|
||
setSearchResults([]);
|
||
return;
|
||
}
|
||
|
||
try {
|
||
setIsSearching(true);
|
||
const results = await api.get<Item[]>(`/api/items/search?q=${encodeURIComponent(query)}`);
|
||
setSearchResults(results);
|
||
} catch (err) {
|
||
console.error('Search failed:', err);
|
||
} finally {
|
||
setIsSearching(false);
|
||
}
|
||
};
|
||
|
||
const handleBarcodeSearch = async () => {
|
||
if (!barcode.trim()) return;
|
||
|
||
try {
|
||
setIsSearching(true);
|
||
const item = await api.get<Item | null>(`/api/items/barcode/${barcode}`);
|
||
if (item) {
|
||
setSearchResults([item]);
|
||
} else {
|
||
// Barcode not found, allow manual entry
|
||
setSearchResults([]);
|
||
setManualName('');
|
||
}
|
||
} catch (err) {
|
||
console.error('Barcode search failed:', err);
|
||
} finally {
|
||
setIsSearching(false);
|
||
}
|
||
};
|
||
|
||
const handleAddItem = async (itemId?: string) => {
|
||
if (!list || !id) return;
|
||
|
||
try {
|
||
setIsAdding(true);
|
||
|
||
let finalItemId = itemId;
|
||
|
||
// If no itemId provided, create a new item first
|
||
if (!finalItemId) {
|
||
const newItem = await api.post<Item>('/api/items', {
|
||
name: manualName,
|
||
aisleId: selectedAisleId,
|
||
});
|
||
finalItemId = newItem.id;
|
||
}
|
||
|
||
// Add item to list
|
||
const newListItem: ListItem = {
|
||
id: `li-${Date.now()}`, // Temporary ID
|
||
itemId: finalItemId,
|
||
quantity: parseInt(manualQuantity, 10) || 1,
|
||
checked: false,
|
||
addedBy: 'current-user', // TODO: Get from auth store
|
||
excludedFromPreset: false,
|
||
};
|
||
|
||
await api.post(`/api/lists/${id}/items`, {
|
||
itemId: finalItemId,
|
||
quantity: newListItem.quantity,
|
||
});
|
||
|
||
// Notify other devices via WebSocket
|
||
notifyItemAdded(id, newListItem);
|
||
|
||
// Update local store
|
||
const updatedList = {
|
||
...list,
|
||
items: [...list.items, newListItem],
|
||
};
|
||
updateList(updatedList);
|
||
|
||
// Go back to list
|
||
router.back();
|
||
} catch (err) {
|
||
console.error('Failed to add item:', err);
|
||
} finally {
|
||
setIsAdding(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}>Add Item</Text>
|
||
<View style={{ width: 32 }} />
|
||
</View>
|
||
|
||
{/* Tab buttons */}
|
||
<View style={styles.tabContainer}>
|
||
<TabButton label="Search" active={tab === 'search'} onPress={() => setTab('search')} />
|
||
<TabButton label="Barcode" active={tab === 'barcode'} onPress={() => setTab('barcode')} />
|
||
<TabButton label="Manual" active={tab === 'manual'} onPress={() => setTab('manual')} />
|
||
</View>
|
||
|
||
{/* Tab content */}
|
||
<ScrollView style={styles.content} showsVerticalScrollIndicator={false}>
|
||
{/* Search Tab */}
|
||
{tab === 'search' && (
|
||
<View>
|
||
<TextInput
|
||
style={styles.input}
|
||
placeholder="Search items..."
|
||
value={searchQuery}
|
||
onChangeText={handleSearch}
|
||
placeholderTextColor="#999"
|
||
/>
|
||
{isSearching ? (
|
||
<View style={styles.centerContainer}>
|
||
<ActivityIndicator size="large" color="#007AFF" />
|
||
</View>
|
||
) : (
|
||
<FlatList
|
||
data={searchResults}
|
||
keyExtractor={(item) => item.id}
|
||
scrollEnabled={false}
|
||
renderItem={({ item }) => (
|
||
<Pressable
|
||
style={styles.resultItem}
|
||
onPress={() => {
|
||
setManualQuantity('1');
|
||
handleAddItem(item.id);
|
||
}}
|
||
>
|
||
<View style={styles.resultInfo}>
|
||
<Text style={styles.resultName}>{item.name}</Text>
|
||
{item.aisleId && (
|
||
<Text style={styles.resultAisle}>Aisle: {item.aisleId}</Text>
|
||
)}
|
||
</View>
|
||
<Text style={styles.addButton}>+</Text>
|
||
</Pressable>
|
||
)}
|
||
ListEmptyComponent={
|
||
searchQuery.trim().length >= 2 ? (
|
||
<Text style={styles.emptyText}>No items found</Text>
|
||
) : (
|
||
<Text style={styles.emptyText}>Type to search</Text>
|
||
)
|
||
}
|
||
/>
|
||
)}
|
||
</View>
|
||
)}
|
||
|
||
{/* Barcode Tab */}
|
||
{tab === 'barcode' && (
|
||
<View>
|
||
<Pressable
|
||
style={styles.scanButton}
|
||
onPress={() => router.push(`/(app)/list/${id}/barcode-scan`)}
|
||
>
|
||
<Text style={styles.scanButtonText}>🎥 Scan Barcode</Text>
|
||
</Pressable>
|
||
|
||
<View style={styles.divider}>
|
||
<Text style={styles.dividerText}>OR</Text>
|
||
</View>
|
||
|
||
<TextInput
|
||
style={styles.input}
|
||
placeholder="Enter barcode manually..."
|
||
value={barcode}
|
||
onChangeText={setBarcode}
|
||
placeholderTextColor="#999"
|
||
/>
|
||
<Pressable
|
||
style={styles.scanButton}
|
||
onPress={handleBarcodeSearch}
|
||
disabled={isSearching}
|
||
>
|
||
{isSearching ? (
|
||
<ActivityIndicator size="small" color="#fff" />
|
||
) : (
|
||
<Text style={styles.scanButtonText}>Search Barcode</Text>
|
||
)}
|
||
</Pressable>
|
||
|
||
{searchResults.length > 0 && (
|
||
<FlatList
|
||
data={searchResults}
|
||
keyExtractor={(item) => item.id}
|
||
scrollEnabled={false}
|
||
renderItem={({ item }) => (
|
||
<Pressable
|
||
style={styles.resultItem}
|
||
onPress={() => {
|
||
setManualQuantity('1');
|
||
handleAddItem(item.id);
|
||
}}
|
||
>
|
||
<View style={styles.resultInfo}>
|
||
<Text style={styles.resultName}>{item.name}</Text>
|
||
{item.barcode && (
|
||
<Text style={styles.resultAisle}>Barcode: {item.barcode}</Text>
|
||
)}
|
||
</View>
|
||
<Text style={styles.addButton}>+</Text>
|
||
</Pressable>
|
||
)}
|
||
/>
|
||
)}
|
||
</View>
|
||
)}
|
||
|
||
{/* Manual Tab */}
|
||
{tab === 'manual' && (
|
||
<View>
|
||
<Text style={styles.label}>Item Name</Text>
|
||
<TextInput
|
||
style={styles.input}
|
||
placeholder="e.g., Milk, Bread..."
|
||
value={manualName}
|
||
onChangeText={setManualName}
|
||
placeholderTextColor="#999"
|
||
/>
|
||
|
||
<Text style={styles.label}>Quantity</Text>
|
||
<View style={styles.quantityContainer}>
|
||
<Pressable
|
||
style={styles.quantityButton}
|
||
onPress={() =>
|
||
setManualQuantity(Math.max(1, parseInt(manualQuantity, 10) - 1).toString())
|
||
}
|
||
>
|
||
<Text style={styles.quantityButtonText}>−</Text>
|
||
</Pressable>
|
||
<TextInput
|
||
style={styles.quantityInput}
|
||
value={manualQuantity}
|
||
onChangeText={setManualQuantity}
|
||
keyboardType="numeric"
|
||
/>
|
||
<Pressable
|
||
style={styles.quantityButton}
|
||
onPress={() => setManualQuantity((parseInt(manualQuantity, 10) + 1).toString())}
|
||
>
|
||
<Text style={styles.quantityButtonText}>+</Text>
|
||
</Pressable>
|
||
</View>
|
||
|
||
<Pressable
|
||
style={styles.addButton}
|
||
onPress={() => handleAddItem()}
|
||
disabled={!manualName.trim() || isAdding}
|
||
>
|
||
{isAdding ? (
|
||
<ActivityIndicator size="small" color="#fff" />
|
||
) : (
|
||
<Text style={styles.addButtonText}>Add Item</Text>
|
||
)}
|
||
</Pressable>
|
||
</View>
|
||
)}
|
||
</ScrollView>
|
||
</View>
|
||
);
|
||
}
|
||
|
||
/**
|
||
* Tab button component
|
||
*/
|
||
function TabButton({
|
||
label,
|
||
active,
|
||
onPress,
|
||
}: {
|
||
label: string;
|
||
active: boolean;
|
||
onPress: () => void;
|
||
}) {
|
||
return (
|
||
<Pressable style={[styles.tabButton, active && styles.tabButtonActive]} onPress={onPress}>
|
||
<Text style={[styles.tabButtonText, active && styles.tabButtonTextActive]}>{label}</Text>
|
||
</Pressable>
|
||
);
|
||
}
|
||
|
||
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',
|
||
},
|
||
tabContainer: {
|
||
flexDirection: 'row',
|
||
borderBottomWidth: 1,
|
||
borderBottomColor: '#e0e0e0',
|
||
},
|
||
tabButton: {
|
||
flex: 1,
|
||
paddingVertical: 12,
|
||
alignItems: 'center',
|
||
borderBottomWidth: 2,
|
||
borderBottomColor: 'transparent',
|
||
},
|
||
tabButtonActive: {
|
||
borderBottomColor: '#007AFF',
|
||
},
|
||
tabButtonText: {
|
||
fontSize: 14,
|
||
fontWeight: '500',
|
||
color: '#666',
|
||
},
|
||
tabButtonTextActive: {
|
||
color: '#007AFF',
|
||
},
|
||
content: {
|
||
flex: 1,
|
||
padding: 16,
|
||
},
|
||
centerContainer: {
|
||
justifyContent: 'center',
|
||
alignItems: 'center',
|
||
paddingVertical: 32,
|
||
},
|
||
input: {
|
||
borderWidth: 1,
|
||
borderColor: '#ddd',
|
||
borderRadius: 8,
|
||
paddingHorizontal: 12,
|
||
paddingVertical: 10,
|
||
fontSize: 16,
|
||
marginBottom: 16,
|
||
backgroundColor: '#f9f9f9',
|
||
},
|
||
label: {
|
||
fontSize: 14,
|
||
fontWeight: '600',
|
||
color: '#000',
|
||
marginBottom: 8,
|
||
},
|
||
scanButton: {
|
||
backgroundColor: '#007AFF',
|
||
paddingVertical: 12,
|
||
borderRadius: 8,
|
||
justifyContent: 'center',
|
||
alignItems: 'center',
|
||
marginBottom: 16,
|
||
},
|
||
scanButtonText: {
|
||
color: '#fff',
|
||
fontSize: 16,
|
||
fontWeight: '600',
|
||
},
|
||
quantityContainer: {
|
||
flexDirection: 'row',
|
||
alignItems: 'center',
|
||
marginBottom: 16,
|
||
gap: 12,
|
||
},
|
||
quantityButton: {
|
||
width: 40,
|
||
height: 40,
|
||
borderRadius: 8,
|
||
backgroundColor: '#f0f0f0',
|
||
justifyContent: 'center',
|
||
alignItems: 'center',
|
||
},
|
||
quantityButtonText: {
|
||
fontSize: 20,
|
||
color: '#007AFF',
|
||
fontWeight: '600',
|
||
},
|
||
quantityInput: {
|
||
flex: 1,
|
||
borderWidth: 1,
|
||
borderColor: '#ddd',
|
||
borderRadius: 8,
|
||
paddingHorizontal: 12,
|
||
fontSize: 16,
|
||
textAlign: 'center',
|
||
},
|
||
resultItem: {
|
||
flexDirection: 'row',
|
||
justifyContent: 'space-between',
|
||
alignItems: 'center',
|
||
paddingVertical: 12,
|
||
paddingHorizontal: 12,
|
||
marginVertical: 4,
|
||
backgroundColor: '#f9f9f9',
|
||
borderRadius: 8,
|
||
borderLeftWidth: 4,
|
||
borderLeftColor: '#007AFF',
|
||
},
|
||
resultInfo: {
|
||
flex: 1,
|
||
},
|
||
resultName: {
|
||
fontSize: 16,
|
||
fontWeight: '500',
|
||
color: '#000',
|
||
},
|
||
resultAisle: {
|
||
fontSize: 12,
|
||
color: '#666',
|
||
marginTop: 4,
|
||
},
|
||
addButton: {
|
||
fontSize: 20,
|
||
color: '#007AFF',
|
||
fontWeight: '600',
|
||
marginLeft: 12,
|
||
},
|
||
emptyText: {
|
||
textAlign: 'center',
|
||
color: '#999',
|
||
fontSize: 14,
|
||
marginVertical: 24,
|
||
},
|
||
divider: {
|
||
flexDirection: 'row',
|
||
alignItems: 'center',
|
||
marginVertical: 16,
|
||
},
|
||
dividerLine: {
|
||
flex: 1,
|
||
height: 1,
|
||
backgroundColor: '#ddd',
|
||
},
|
||
dividerText: {
|
||
marginHorizontal: 12,
|
||
fontSize: 12,
|
||
fontWeight: '600',
|
||
color: '#999',
|
||
},
|
||
addButtonText: {
|
||
color: '#fff',
|
||
fontSize: 16,
|
||
fontWeight: '600',
|
||
},
|
||
});
|