feat/parapharmacy-baby-products #31

Open
Ichitux wants to merge 24 commits from feat/parapharmacy-baby-products into main
Showing only changes of commit 59edc7fbf1 - Show all commits
+23 -16
View File
@@ -76,7 +76,7 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '', onNavigate
}, [currentUser]); }, [currentUser]);
useEffect(() => { useEffect(() => {
const searchMedicines = async () => { const searchAll = async () => {
if (searchQuery.trim().length < 2) { if (searchQuery.trim().length < 2) {
setMedicines([]); setMedicines([]);
setProducts([]); setProducts([]);
@@ -85,27 +85,34 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '', onNavigate
return; return;
} }
setLoading(true); setLoading(true);
const query = searchQuery.trim();
try { try {
const response = await fetch(`/api/medicines/search?q=${encodeURIComponent(searchQuery)}`); // Run both searches in parallel for speed
const data = await response.json(); const [medicinesRes, productsRes] = await Promise.allSettled([
setMedicines(data); fetch(`/api/medicines/search?q=${encodeURIComponent(query)}`),
fetch(`/api/products/search?q=${encodeURIComponent(query)}`),
]);
// Only update if this search is still the current one
if (query !== searchQuery.trim()) return;
if (medicinesRes.status === 'fulfilled' && medicinesRes.value.ok) {
const medicinesData = await medicinesRes.value.json();
setMedicines(medicinesData);
}
if (productsRes.status === 'fulfilled' && productsRes.value.ok) {
const productsData = await productsRes.value.json();
setProducts(productsData.results || []);
}
} catch (error) { } catch (error) {
console.error('Error searching medicines:', error); console.error('Search error:', error);
} finally { } finally {
setLoading(false); setLoading(false);
} }
try {
const productsResponse = await fetch(`/api/products/search?q=${encodeURIComponent(searchQuery)}`);
if (productsResponse.ok) {
const productsData = await productsResponse.json();
setProducts(productsData.results || []);
}
} catch (err) {
console.error('Product search error:', err);
}
}; };
const timeoutId = setTimeout(searchMedicines, 300); const timeoutId = setTimeout(searchAll, 300);
return () => clearTimeout(timeoutId); return () => clearTimeout(timeoutId);
}, [searchQuery]); }, [searchQuery]);