import { View, Text, TextInput, ScrollView, TouchableOpacity, ActivityIndicator, Image, Alert } from 'react-native' import React, { useState, useEffect } from 'react' import TabBar from '../themes/TabBar' import { ChevronLeftIcon, MagnifyingGlassIcon, XMarkIcon } from 'react-native-heroicons/outline' import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen'; import { useNavigation } from '@react-navigation/native'; import { fetchProducts, getCurrentUser, getUserRecentSearches, saveUserRecentSearch, removeUserRecentSearch, clearAllUserRecentSearches } from '../api'; import { Ionicons } from '@expo/vector-icons'; const SearchScreen = () => { const [searchText, setSearchText] = useState(''); const [recentSearches, setRecentSearches] = useState([]); const [loading, setLoading] = useState(false); const [errorMsg, setErrorMsg] = useState(''); const [products, setProducts] = useState([]); const [user, setUser] = useState(null); const navigation = useNavigation(); useEffect(() => { loadCurrentUser(); loadRecentSearches(); }, []); const loadCurrentUser = async () => { try { const currentUser = await getCurrentUser(); setUser(currentUser); } catch (error) { console.error('Error loading current user:', error); } }; const loadRecentSearches = async () => { try { const searches = await getUserRecentSearches(); setRecentSearches(Array.isArray(searches) ? searches : []); } catch (error) { console.error('Error loading recent searches:', error); setRecentSearches([]); } }; const handleClearRecentSearch = async (term) => { try { if (!term) return; const updatedSearches = await removeUserRecentSearch(term); setRecentSearches(Array.isArray(updatedSearches) ? updatedSearches : []); } catch (error) { console.error('Error clearing recent search:', error); } }; const handleClearAllRecentSearches = async () => { try { await clearAllUserRecentSearches(); setRecentSearches([]); } catch (error) { console.error('Error clearing all recent searches:', error); } }; const handleSearch = async (searchTerm = searchText) => { if (!searchTerm || !searchTerm.trim()) return; setLoading(true); setErrorMsg(''); try { const result = await fetchProducts(searchTerm); setProducts(result); // Save to recent searches await saveUserRecentSearch(searchTerm); // Refresh recent searches loadRecentSearches(); navigation.navigate('SearchResult', { products: result, searchTerm: searchTerm }); } catch (error) { setErrorMsg('Error searching products. Please try again.'); console.error('Search error:', error); } finally { setLoading(false); } }; const handleRecentSearchPress = (term) => { if (!term) return; setSearchText(term); handleSearch(term); }; return ( } titleStyle={{ color: "black", fontWeight: "bold", fontSize: 20 }} prefixStyle={{ backgroundColor: "transparent" }} suffixStyle={{ backgroundColor: "white", borderWidth: 1, borderColor: "black" }} containerStyle="" suffixAction={() => navigation.goBack()} /> handleSearch()} returnKeyType="search" autoFocus /> {errorMsg && ( {errorMsg} )} Recent Clear All {loading ? ( ) : ( {recentSearches.length === 0 ? ( No recent searches ) : ( recentSearches.map((item, index) => ( handleRecentSearchPress(item)} className="flex-1" > {item} handleClearRecentSearch(item)}> )) )} )} ); }; export default SearchScreen;