feat: unified search - remove filter tabs, show all results together
- Frontend now searches both CIMA and Parapharmacy simultaneously - Removed filter tabs (Todos/Medicamentos/Parafarmacia) - Results show in a single unified list - Mobile: Shows medicines first, then parapharmacy section - Web: Same unified approach with results summary
This commit is contained in:
@@ -18,7 +18,6 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '', onNavigate
|
||||
const [searchQuery, setSearchQuery] = useState(initialQuery);
|
||||
const [medicines, setMedicines] = useState([]);
|
||||
const [products, setProducts] = useState([]);
|
||||
const [searchMode, setSearchMode] = useState('all');
|
||||
const [selectedMedicine, setSelectedMedicine] = useState(null);
|
||||
const [pharmacies, setPharmacies] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
@@ -88,16 +87,24 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '', onNavigate
|
||||
const query = searchQuery.trim();
|
||||
|
||||
try {
|
||||
// Only search medications from CIMA
|
||||
const medicinesRes = await fetch(`/api/medicines/search?q=${encodeURIComponent(query)}`);
|
||||
// Search both CIMA and Parapharmacy simultaneously
|
||||
const [medicinesRes, productsRes] = await Promise.allSettled([
|
||||
fetch(`/api/medicines/search?q=${encodeURIComponent(query)}`),
|
||||
fetch(`/api/products/parapharmacy/search?q=${encodeURIComponent(query)}`)
|
||||
]);
|
||||
|
||||
// Only update if this search is still the current one
|
||||
if (query !== searchQuery.trim()) return;
|
||||
|
||||
if (medicinesRes.ok) {
|
||||
const medicinesData = await medicinesRes.json();
|
||||
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) {
|
||||
console.error('Search error:', error);
|
||||
} finally {
|
||||
@@ -108,26 +115,6 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '', onNavigate
|
||||
return () => clearTimeout(timeoutId);
|
||||
}, [searchQuery]);
|
||||
|
||||
// Search parapharmacy when user switches to Parafarmacia tab
|
||||
useEffect(() => {
|
||||
if (searchMode !== 'products' || searchQuery.trim().length < 2) return;
|
||||
|
||||
const searchProducts = async () => {
|
||||
const query = searchQuery.trim();
|
||||
try {
|
||||
const response = await fetch(`/api/products/parapharmacy/search?q=${encodeURIComponent(query)}`);
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
setProducts(data.results || []);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Parapharmacy search error:', error);
|
||||
}
|
||||
};
|
||||
|
||||
searchProducts();
|
||||
}, [searchMode, searchQuery]);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchPharmacies = async () => {
|
||||
if (!selectedMedicine) {
|
||||
@@ -310,28 +297,13 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '', onNavigate
|
||||
|
||||
{searchQuery && !selectedMedicine && (
|
||||
<>
|
||||
<div className="filter-tabs">
|
||||
<button
|
||||
className={`filter-tab ${searchMode === 'all' ? 'filter-tab--active' : ''}`}
|
||||
onClick={() => setSearchMode('all')}
|
||||
>
|
||||
Todos ({medicines.length + products.length})
|
||||
</button>
|
||||
<button
|
||||
className={`filter-tab ${searchMode === 'medicines' ? 'filter-tab--active' : ''}`}
|
||||
onClick={() => setSearchMode('medicines')}
|
||||
>
|
||||
Medicamentos ({medicines.length})
|
||||
</button>
|
||||
<button
|
||||
className={`filter-tab ${searchMode === 'products' ? 'filter-tab--active' : ''}`}
|
||||
onClick={() => setSearchMode('products')}
|
||||
>
|
||||
Parafarmacia ({products.length})
|
||||
</button>
|
||||
<div className="results-summary">
|
||||
{(medicines.length + products.length) > 0 && (
|
||||
<span>{medicines.length + products.length} resultados encontrados</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{(searchMode === 'all' || searchMode === 'medicines') && (
|
||||
{medicines.length > 0 && (
|
||||
<MedicineResults
|
||||
medicines={medicines}
|
||||
onSelect={(m) => {
|
||||
@@ -344,9 +316,9 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '', onNavigate
|
||||
/>
|
||||
)}
|
||||
|
||||
{(searchMode === 'all' || searchMode === 'products') && products.length > 0 && (
|
||||
{products.length > 0 && (
|
||||
<div className="products-section">
|
||||
<h3 className="section-subtitle">Parafarmacia y Bebé</h3>
|
||||
<h3 className="section-subtitle">Parafarmacia</h3>
|
||||
<ProductResults
|
||||
products={products}
|
||||
onSelect={(p) => {
|
||||
|
||||
Reference in New Issue
Block a user